am 7890eeff: am 882ba89b: am 50694917: am 7e566239: Merge "docs: Updated Building your First App tutorial. Bug: 13429905" into klp-docs
* commit '7890eeff3adc862160b0da257a9524c540fc1fa7': docs: Updated Building your First App tutorial. Bug: 13429905
|
Before Width: | Height: | Size: 65 KiB After Width: | Height: | Size: 48 KiB |
|
Before Width: | Height: | Size: 79 KiB After Width: | Height: | Size: 55 KiB |
|
Before Width: | Height: | Size: 54 KiB After Width: | Height: | Size: 43 KiB |
|
Before Width: | Height: | Size: 7.2 KiB After Width: | Height: | Size: 6.5 KiB |
|
Before Width: | Height: | Size: 7.2 KiB After Width: | Height: | Size: 6.6 KiB |
|
Before Width: | Height: | Size: 36 KiB After Width: | Height: | Size: 27 KiB |
@@ -75,16 +75,16 @@ content of the text field to another activity.</p>
|
||||
|
||||
<h2 id="LinearLayout">Create a Linear Layout</h2>
|
||||
|
||||
<p>Open the <code>activity_main.xml</code> file from the <code>res/layout/</code>
|
||||
<p>Open the <code>fragment_main.xml</code> file from the <code>res/layout/</code>
|
||||
directory.</p>
|
||||
|
||||
<p class="note"><strong>Note:</strong> In Eclipse, when you open a layout file, you’re first shown
|
||||
the Graphical Layout editor. This is an editor that helps you build layouts using WYSIWYG tools. For this
|
||||
lesson, you’re going to work directly with the XML, so click the <em>activity_main.xml</em> tab at
|
||||
lesson, you’re going to work directly with the XML, so click the <em>fragment_main.xml</em> tab at
|
||||
the bottom of the screen to open the XML editor.</p>
|
||||
|
||||
<p>The BlankActivity template you chose when you created this project includes the
|
||||
<code>activity_main.xml</code> file with a {@link
|
||||
<code>fragment_main.xml</code> file with a {@link
|
||||
android.widget.RelativeLayout} root view and a {@link android.widget.TextView} child view.</p>
|
||||
|
||||
<p>First, delete the {@link android.widget.TextView <TextView>} element and change the {@link
|
||||
@@ -95,7 +95,6 @@ android:orientation}</a> attribute and set it to <code>"horizontal"</code>.
|
||||
The result looks like this:</p>
|
||||
|
||||
<pre>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
|
||||
@@ -120,8 +120,8 @@ devices.</li>
|
||||
<strong>Finish</strong>.</li>
|
||||
</ol>
|
||||
|
||||
<p>Your Android project is now set up with some default files and you’re ready to begin
|
||||
building the app. Continue to the <a href="running-app.html">next lesson</a>.</p>
|
||||
<p>Your Android project is now a basic "Hello World" app that contains some default files.
|
||||
To run the app, continue to the <a href="running-app.html">next lesson</a>.</p>
|
||||
|
||||
|
||||
|
||||
@@ -155,8 +155,8 @@ and replace
|
||||
projects.</p></li>
|
||||
</ol>
|
||||
|
||||
<p>Your Android project is now set up with several default configurations and you’re ready to begin
|
||||
building the app. Continue to the <a href="running-app.html">next lesson</a>.</p>
|
||||
<p>Your Android project is now a basic "Hello World" app that contains some default files.
|
||||
To run the app, continue to the <a href="running-app.html">next lesson</a>.</p>
|
||||
|
||||
<p class="note"><strong>Tip:</strong> Add the <code>platform-tools/</code> as well as the
|
||||
<code>tools/</code> directory to your <code>PATH</code> environment variable.</p>
|
||||
|
||||
@@ -62,7 +62,7 @@ href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code andro
|
||||
attributes. For your first app, it should look like this:</p>
|
||||
<pre>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ... >
|
||||
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
|
||||
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" />
|
||||
...
|
||||
</manifest>
|
||||
</pre>
|
||||
|
||||
@@ -45,7 +45,7 @@ starts a new activity when the user clicks the Send button.</p>
|
||||
|
||||
<h2 id="RespondToButton">Respond to the Send Button</h2>
|
||||
|
||||
<p>To respond to the button's on-click event, open the <code>activity_main.xml</code>
|
||||
<p>To respond to the button's on-click event, open the <code>fragment_main.xml</code>
|
||||
layout file and add the <a
|
||||
href="{@docRoot}reference/android/view/View.html#attr_android:onClick">{@code android:onClick}</a>
|
||||
attribute to the {@link android.widget.Button <Button>} element:</p>
|
||||
@@ -73,14 +73,6 @@ public void sendMessage(View view) {
|
||||
}
|
||||
</pre>
|
||||
|
||||
<p>This requires that you import the {@link android.view.View} class:</p>
|
||||
<pre>
|
||||
import android.view.View;
|
||||
</pre>
|
||||
|
||||
<p class="note"><strong>Tip:</strong> In Eclipse, press Ctrl + Shift + O to import missing classes
|
||||
(Cmd + Shift + O on Mac).</p>
|
||||
|
||||
<p>In order for the system to match this method to the method name given to <a
|
||||
href="{@docRoot}reference/android/view/View.html#attr_android:onClick">{@code android:onClick}</a>,
|
||||
the signature must be exactly as shown. Specifically, the method must:</p>
|
||||
@@ -111,6 +103,14 @@ an activity called {@code DisplayMessageActivity}:</p>
|
||||
Intent intent = new Intent(this, DisplayMessageActivity.class);
|
||||
</pre>
|
||||
|
||||
<p>This requires that you import the {@link android.content.Intent} class:</p>
|
||||
<pre>
|
||||
import android.content.Intent;
|
||||
</pre>
|
||||
|
||||
<p class="note"><strong>Tip:</strong> In Eclipse, press Ctrl + Shift + O to import missing classes
|
||||
(Cmd + Shift + O on Mac).</p>
|
||||
|
||||
<p>The constructor used here takes two parameters:</p>
|
||||
<ul>
|
||||
<li>A {@link
|
||||
@@ -151,9 +151,8 @@ intent.putExtra(EXTRA_MESSAGE, message);
|
||||
</pre>
|
||||
|
||||
<p class="note"><strong>Note:</strong>
|
||||
You now need import statements for <code>android.content.Intent</code>
|
||||
and <code>android.widget.EditText</code>. You'll define the <code>EXTRA_MESSAGE</code>
|
||||
constant in a moment.</p>
|
||||
You now need an import statement for <code>android.widget.EditText</code>.
|
||||
You'll define the <code>EXTRA_MESSAGE</code> constant in a moment.</p>
|
||||
|
||||
<p>An {@link android.content.Intent} can carry a collection of various data types as key-value
|
||||
pairs called <em>extras</em>. The {@link android.content.Intent#putExtra putExtra()} method takes the
|
||||
@@ -165,7 +164,7 @@ public constant. So add the {@code EXTRA_MESSAGE} definition to the top of the {
|
||||
MainActivity} class:</p>
|
||||
|
||||
<pre>
|
||||
public class MainActivity extends Activity {
|
||||
public class MainActivity extends ActionBarActivity {
|
||||
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
|
||||
...
|
||||
}
|
||||
@@ -223,6 +222,7 @@ work.</p>
|
||||
<li><strong>Project</strong>: MyFirstApp</li>
|
||||
<li><strong>Activity Name</strong>: DisplayMessageActivity</li>
|
||||
<li><strong>Layout Name</strong>: activity_display_message</li>
|
||||
<li><strong>Fragment Layout Name</strong>: fragment_display_message</li>
|
||||
<li><strong>Title</strong>: My Message</li>
|
||||
<li><strong>Hierarchial Parent</strong>: com.example.myfirstapp.MainActivity</li>
|
||||
<li><strong>Navigation Type</strong>: None</li>
|
||||
@@ -240,49 +240,65 @@ activity:</p>
|
||||
<ul>
|
||||
<li>The class
|
||||
already includes an implementation of the required {@link android.app.Activity#onCreate onCreate()}
|
||||
method.</li>
|
||||
method. You will update the implementation of this method later.</li>
|
||||
<li>There's also an implementation of the {@link android.app.Activity#onCreateOptionsMenu
|
||||
onCreateOptionsMenu()} method, but
|
||||
you won't need it for this app so you can remove it.</li>
|
||||
<li>There's also an implementation of {@link android.app.Activity#onOptionsItemSelected
|
||||
onOptionsItemSelected()} which handles the behavior for the action bar's <em>Up</em> behavior.
|
||||
Keep this one the way it is.</li>
|
||||
<li>There's also a <code>PlaceholderFragment</code> class that extends
|
||||
{@link android.app.Fragment}. You will not need this class in the final version of this
|
||||
activity.</li>
|
||||
</ul>
|
||||
|
||||
<p>Because the {@link android.app.ActionBar} APIs are available only on {@link
|
||||
android.os.Build.VERSION_CODES#HONEYCOMB} (API level 11) and higher, you must add a condition
|
||||
around the {@link android.app.Activity#getActionBar()} method to check the current platform version.
|
||||
Additionally, you must add the {@code @SuppressLint("NewApi")} tag to the
|
||||
{@link android.app.Activity#onCreate onCreate()} method to avoid <a
|
||||
href="{@docRoot}tools/help/lint.html">lint</a> errors.</p>
|
||||
<p>Fragments decompose application functionality and UI into reusable modules. For more
|
||||
information on fragments, see the <a href="{@docRoot}guide/components/fragments.html">Fragments
|
||||
API Guide</a>. The final version of this activity does not use fragments.</p>
|
||||
|
||||
<p>The {@code DisplayMessageActivity} class should now look like this:</p>
|
||||
|
||||
<pre>
|
||||
public class DisplayMessageActivity extends Activity {
|
||||
public class DisplayMessageActivity extends ActionBarActivity {
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_display_message);
|
||||
|
||||
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
||||
// Show the Up button in the action bar.
|
||||
getActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
if (savedInstanceState == null) {
|
||||
getSupportFragmentManager().beginTransaction()
|
||||
.add(R.id.container, new PlaceholderFragment()).commit();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case android.R.id.home:
|
||||
NavUtils.navigateUpFromSameTask(this);
|
||||
// Handle action bar item clicks here. The action bar will
|
||||
// automatically handle clicks on the Home/Up button, so long
|
||||
// as you specify a parent activity in AndroidManifest.xml.
|
||||
int id = item.getItemId();
|
||||
if (id == R.id.action_settings) {
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
/**
|
||||
* A placeholder fragment containing a simple view.
|
||||
*/
|
||||
public static class PlaceholderFragment extends Fragment {
|
||||
|
||||
public PlaceholderFragment() { }
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View rootView = inflater.inflate(R.layout.fragment_display_message,
|
||||
container, false);
|
||||
return rootView;
|
||||
}
|
||||
}
|
||||
}
|
||||
</pre>
|
||||
|
||||
@@ -422,7 +438,7 @@ public void onCreate(Bundle savedInstanceState) {
|
||||
|
||||
<img src="{@docRoot}images/training/firstapp/firstapp.png" />
|
||||
<p class="img-caption"><strong>Figure 2.</strong> Both activities in the final app, running
|
||||
on Android 4.0.
|
||||
on Android 4.4.
|
||||
|
||||
<p>That's it, you've built your first Android app!</p>
|
||||
|
||||
|
||||