am 882ba89b: am 50694917: am 7e566239: Merge "docs: Updated Building your First App tutorial. Bug: 13429905" into klp-docs

* commit '882ba89bf4aa6046b8f69460f1fe3ad87325e6d8':
  docs: Updated Building your First App tutorial. Bug: 13429905
This commit is contained in:
Ricardo Cervera
2014-03-14 16:32:38 +00:00
committed by Android Git Automerger
10 changed files with 54 additions and 39 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 65 KiB

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 79 KiB

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 54 KiB

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.2 KiB

After

Width:  |  Height:  |  Size: 6.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.2 KiB

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

After

Width:  |  Height:  |  Size: 27 KiB

View File

@@ -75,16 +75,16 @@ content of the text field to another activity.</p>
<h2 id="LinearLayout">Create a Linear Layout</h2> <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> directory.</p>
<p class="note"><strong>Note:</strong> In Eclipse, when you open a layout file, youre first shown <p class="note"><strong>Note:</strong> In Eclipse, when you open a layout file, youre first shown
the Graphical Layout editor. This is an editor that helps you build layouts using WYSIWYG tools. For this the Graphical Layout editor. This is an editor that helps you build layouts using WYSIWYG tools. For this
lesson, youre going to work directly with the XML, so click the <em>activity_main.xml</em> tab at lesson, youre 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> the bottom of the screen to open the XML editor.</p>
<p>The BlankActivity template you chose when you created this project includes the <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> android.widget.RelativeLayout} root view and a {@link android.widget.TextView} child view.</p>
<p>First, delete the {@link android.widget.TextView &lt;TextView>} element and change the {@link <p>First, delete the {@link android.widget.TextView &lt;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> The result looks like this:</p>
<pre> <pre>
&lt;?xml version="1.0" encoding="utf-8"?>
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"

View File

@@ -120,8 +120,8 @@ devices.</li>
<strong>Finish</strong>.</li> <strong>Finish</strong>.</li>
</ol> </ol>
<p>Your Android project is now set up with some default files and youre ready to begin <p>Your Android project is now a basic "Hello World" app that contains some default files.
building the app. Continue to the <a href="running-app.html">next lesson</a>.</p> 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> projects.</p></li>
</ol> </ol>
<p>Your Android project is now set up with several default configurations and youre ready to begin <p>Your Android project is now a basic "Hello World" app that contains some default files.
building the app. Continue to the <a href="running-app.html">next lesson</a>.</p> 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 <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> <code>tools/</code> directory to your <code>PATH</code> environment variable.</p>

View File

@@ -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> attributes. For your first app, it should look like this:</p>
<pre> <pre>
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" ... > &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" ... >
&lt;uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> &lt;uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" />
... ...
&lt;/manifest> &lt;/manifest>
</pre> </pre>

View File

@@ -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> <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 layout file and add the <a
href="{@docRoot}reference/android/view/View.html#attr_android:onClick">{@code android:onClick}</a> href="{@docRoot}reference/android/view/View.html#attr_android:onClick">{@code android:onClick}</a>
attribute to the {@link android.widget.Button &lt;Button>} element:</p> attribute to the {@link android.widget.Button &lt;Button>} element:</p>
@@ -73,14 +73,6 @@ public void sendMessage(View view) {
} }
</pre> </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 <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>, 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> 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); Intent intent = new Intent(this, DisplayMessageActivity.class);
</pre> </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> <p>The constructor used here takes two parameters:</p>
<ul> <ul>
<li>A {@link <li>A {@link
@@ -151,9 +151,8 @@ intent.putExtra(EXTRA_MESSAGE, message);
</pre> </pre>
<p class="note"><strong>Note:</strong> <p class="note"><strong>Note:</strong>
You now need import statements for <code>android.content.Intent</code> You now need an import statement for <code>android.widget.EditText</code>.
and <code>android.widget.EditText</code>. You'll define the <code>EXTRA_MESSAGE</code> You'll define the <code>EXTRA_MESSAGE</code> constant in a moment.</p>
constant in a moment.</p>
<p>An {@link android.content.Intent} can carry a collection of various data types as key-value <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 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> MainActivity} class:</p>
<pre> <pre>
public class MainActivity extends Activity { public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"; public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
... ...
} }
@@ -223,6 +222,7 @@ work.</p>
<li><strong>Project</strong>: MyFirstApp</li> <li><strong>Project</strong>: MyFirstApp</li>
<li><strong>Activity Name</strong>: DisplayMessageActivity</li> <li><strong>Activity Name</strong>: DisplayMessageActivity</li>
<li><strong>Layout Name</strong>: activity_display_message</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>Title</strong>: My Message</li>
<li><strong>Hierarchial Parent</strong>: com.example.myfirstapp.MainActivity</li> <li><strong>Hierarchial Parent</strong>: com.example.myfirstapp.MainActivity</li>
<li><strong>Navigation Type</strong>: None</li> <li><strong>Navigation Type</strong>: None</li>
@@ -240,49 +240,65 @@ activity:</p>
<ul> <ul>
<li>The class <li>The class
already includes an implementation of the required {@link android.app.Activity#onCreate onCreate()} 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 <li>There's also an implementation of the {@link android.app.Activity#onCreateOptionsMenu
onCreateOptionsMenu()} method, but onCreateOptionsMenu()} method, but
you won't need it for this app so you can remove it.</li> 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 <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. onOptionsItemSelected()} which handles the behavior for the action bar's <em>Up</em> behavior.
Keep this one the way it is.</li> 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> </ul>
<p>Because the {@link android.app.ActionBar} APIs are available only on {@link <p>Fragments decompose application functionality and UI into reusable modules. For more
android.os.Build.VERSION_CODES#HONEYCOMB} (API level 11) and higher, you must add a condition information on fragments, see the <a href="{@docRoot}guide/components/fragments.html">Fragments
around the {@link android.app.Activity#getActionBar()} method to check the current platform version. API Guide</a>. The final version of this activity does not use fragments.</p>
Additionally, you must add the {@code &#64;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>The {@code DisplayMessageActivity} class should now look like this:</p> <p>The {@code DisplayMessageActivity} class should now look like this:</p>
<pre> <pre>
public class DisplayMessageActivity extends Activity { public class DisplayMessageActivity extends ActionBarActivity {
&#64;SuppressLint("NewApi")
&#64;Override &#64;Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message); setContentView(R.layout.activity_display_message);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs if (savedInstanceState == null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { getSupportFragmentManager().beginTransaction()
// Show the Up button in the action bar. .add(R.id.container, new PlaceholderFragment()).commit();
getActionBar().setDisplayHomeAsUpEnabled(true);
} }
} }
&#64;Override &#64;Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { // Handle action bar item clicks here. The action bar will
case android.R.id.home: // automatically handle clicks on the Home/Up button, so long
NavUtils.navigateUpFromSameTask(this); // as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true; return true;
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() { }
&#64;Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_display_message,
container, false);
return rootView;
}
}
} }
</pre> </pre>
@@ -422,7 +438,7 @@ public void onCreate(Bundle savedInstanceState) {
<img src="{@docRoot}images/training/firstapp/firstapp.png" /> <img src="{@docRoot}images/training/firstapp/firstapp.png" />
<p class="img-caption"><strong>Figure 2.</strong> Both activities in the final app, running <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> <p>That's it, you've built your first Android app!</p>