From d83a98f4ce9cfa908f5c54bbd70f03eec07e7553 Mon Sep 17 00:00:00 2001 From: The Android Open Source Project Date: Tue, 3 Mar 2009 18:28:45 -0800 Subject: auto import from //depot/cupcake/@135843 --- docs/html/guide/tutorials/hello-android.jd | 473 --------------- docs/html/guide/tutorials/hello-world.jd | 500 ---------------- docs/html/guide/tutorials/images/hello_world_0.png | Bin 30614 -> 0 bytes docs/html/guide/tutorials/images/hello_world_1.png | Bin 38174 -> 0 bytes docs/html/guide/tutorials/images/hello_world_2.png | Bin 67830 -> 0 bytes docs/html/guide/tutorials/images/hello_world_3.png | Bin 60750 -> 0 bytes docs/html/guide/tutorials/images/hello_world_4.png | Bin 61711 -> 0 bytes docs/html/guide/tutorials/images/hello_world_5.png | Bin 6244 -> 0 bytes docs/html/guide/tutorials/images/hello_world_8.png | Bin 10993 -> 0 bytes docs/html/guide/tutorials/images/hello_world_9.png | Bin 25933 -> 0 bytes docs/html/guide/tutorials/index.html | 8 - .../tutorials/notepad/codelab/NotepadCodeLab.zip | Bin 431473 -> 0 bytes docs/html/guide/tutorials/notepad/index.jd | 142 ----- docs/html/guide/tutorials/notepad/notepad-ex1.jd | 589 ------------------- docs/html/guide/tutorials/notepad/notepad-ex2.jd | 647 --------------------- docs/html/guide/tutorials/notepad/notepad-ex3.jd | 358 ------------ .../tutorials/notepad/notepad-extra-credit.jd | 70 --- docs/html/guide/tutorials/notepad/notepad-index.jd | 143 ----- .../guide/tutorials/views/hello-autocomplete.jd | 116 ---- .../html/guide/tutorials/views/hello-datepicker.jd | 151 ----- docs/html/guide/tutorials/views/hello-formstuff.jd | 262 --------- docs/html/guide/tutorials/views/hello-gallery.jd | 135 ----- docs/html/guide/tutorials/views/hello-gridview.jd | 128 ---- .../guide/tutorials/views/hello-linearlayout.jd | 130 ----- docs/html/guide/tutorials/views/hello-listview.jd | 90 --- docs/html/guide/tutorials/views/hello-mapview.jd | 243 -------- .../guide/tutorials/views/hello-relativelayout.jd | 75 --- docs/html/guide/tutorials/views/hello-spinner.jd | 106 ---- .../guide/tutorials/views/hello-tablelayout.jd | 118 ---- docs/html/guide/tutorials/views/hello-tabwidget.jd | 124 ---- .../html/guide/tutorials/views/hello-timepicker.jd | 159 ----- docs/html/guide/tutorials/views/hello-webview.jd | 118 ---- docs/html/guide/tutorials/views/images/android.png | Bin 693 -> 0 bytes .../guide/tutorials/views/images/androidmarker.png | Bin 702 -> 0 bytes .../tutorials/views/images/hello-autocomplete.png | Bin 4601 -> 0 bytes .../tutorials/views/images/hello-datepicker.png | Bin 7322 -> 0 bytes .../tutorials/views/images/hello-formstuff.png | Bin 4258 -> 0 bytes .../guide/tutorials/views/images/hello-gallery.png | Bin 5593 -> 0 bytes .../tutorials/views/images/hello-gridview.png | Bin 21768 -> 0 bytes .../tutorials/views/images/hello-linearlayout.png | Bin 4207 -> 0 bytes .../tutorials/views/images/hello-listview.png | Bin 6926 -> 0 bytes .../guide/tutorials/views/images/hello-mapview.png | Bin 16922 -> 0 bytes .../views/images/hello-relativelayout.png | Bin 2399 -> 0 bytes .../guide/tutorials/views/images/hello-spinner.png | Bin 2513 -> 0 bytes .../tutorials/views/images/hello-tablelayout.png | Bin 3446 -> 0 bytes .../tutorials/views/images/hello-tabwidget.png | Bin 2117 -> 0 bytes .../tutorials/views/images/hello-timepicker.png | Bin 5644 -> 0 bytes .../guide/tutorials/views/images/hello-webview.png | Bin 5874 -> 0 bytes docs/html/guide/tutorials/views/index.jd | 118 ---- 49 files changed, 5003 deletions(-) delete mode 100644 docs/html/guide/tutorials/hello-android.jd delete mode 100644 docs/html/guide/tutorials/hello-world.jd delete mode 100644 docs/html/guide/tutorials/images/hello_world_0.png delete mode 100644 docs/html/guide/tutorials/images/hello_world_1.png delete mode 100644 docs/html/guide/tutorials/images/hello_world_2.png delete mode 100644 docs/html/guide/tutorials/images/hello_world_3.png delete mode 100644 docs/html/guide/tutorials/images/hello_world_4.png delete mode 100644 docs/html/guide/tutorials/images/hello_world_5.png delete mode 100644 docs/html/guide/tutorials/images/hello_world_8.png delete mode 100644 docs/html/guide/tutorials/images/hello_world_9.png delete mode 100644 docs/html/guide/tutorials/index.html delete mode 100644 docs/html/guide/tutorials/notepad/codelab/NotepadCodeLab.zip delete mode 100644 docs/html/guide/tutorials/notepad/index.jd delete mode 100644 docs/html/guide/tutorials/notepad/notepad-ex1.jd delete mode 100644 docs/html/guide/tutorials/notepad/notepad-ex2.jd delete mode 100644 docs/html/guide/tutorials/notepad/notepad-ex3.jd delete mode 100644 docs/html/guide/tutorials/notepad/notepad-extra-credit.jd delete mode 100644 docs/html/guide/tutorials/notepad/notepad-index.jd delete mode 100644 docs/html/guide/tutorials/views/hello-autocomplete.jd delete mode 100644 docs/html/guide/tutorials/views/hello-datepicker.jd delete mode 100644 docs/html/guide/tutorials/views/hello-formstuff.jd delete mode 100644 docs/html/guide/tutorials/views/hello-gallery.jd delete mode 100644 docs/html/guide/tutorials/views/hello-gridview.jd delete mode 100644 docs/html/guide/tutorials/views/hello-linearlayout.jd delete mode 100644 docs/html/guide/tutorials/views/hello-listview.jd delete mode 100644 docs/html/guide/tutorials/views/hello-mapview.jd delete mode 100644 docs/html/guide/tutorials/views/hello-relativelayout.jd delete mode 100644 docs/html/guide/tutorials/views/hello-spinner.jd delete mode 100644 docs/html/guide/tutorials/views/hello-tablelayout.jd delete mode 100644 docs/html/guide/tutorials/views/hello-tabwidget.jd delete mode 100644 docs/html/guide/tutorials/views/hello-timepicker.jd delete mode 100644 docs/html/guide/tutorials/views/hello-webview.jd delete mode 100755 docs/html/guide/tutorials/views/images/android.png delete mode 100755 docs/html/guide/tutorials/views/images/androidmarker.png delete mode 100755 docs/html/guide/tutorials/views/images/hello-autocomplete.png delete mode 100755 docs/html/guide/tutorials/views/images/hello-datepicker.png delete mode 100755 docs/html/guide/tutorials/views/images/hello-formstuff.png delete mode 100755 docs/html/guide/tutorials/views/images/hello-gallery.png delete mode 100755 docs/html/guide/tutorials/views/images/hello-gridview.png delete mode 100755 docs/html/guide/tutorials/views/images/hello-linearlayout.png delete mode 100755 docs/html/guide/tutorials/views/images/hello-listview.png delete mode 100755 docs/html/guide/tutorials/views/images/hello-mapview.png delete mode 100755 docs/html/guide/tutorials/views/images/hello-relativelayout.png delete mode 100755 docs/html/guide/tutorials/views/images/hello-spinner.png delete mode 100755 docs/html/guide/tutorials/views/images/hello-tablelayout.png delete mode 100644 docs/html/guide/tutorials/views/images/hello-tabwidget.png delete mode 100755 docs/html/guide/tutorials/views/images/hello-timepicker.png delete mode 100755 docs/html/guide/tutorials/views/images/hello-webview.png delete mode 100644 docs/html/guide/tutorials/views/index.jd (limited to 'docs/html/guide/tutorials') diff --git a/docs/html/guide/tutorials/hello-android.jd b/docs/html/guide/tutorials/hello-android.jd deleted file mode 100644 index c37d6f9..0000000 --- a/docs/html/guide/tutorials/hello-android.jd +++ /dev/null @@ -1,473 +0,0 @@ -page.title=Hello, Android! -@jd:body - -

First impressions matter, and as a developer you know that the first impression -you get of a development framework is how easy it is to write "Hello, -World!" Well, in Android, it's pretty easy. Here's how it looks:

- - - -

The sections below spell it all out in detail.

- - -

Let's jump in!

- - - -

Create the Project

- -

Creating the project is as simple as can be. An Eclipse -plugin is available making Android development a snap.

- -

You'll need to have a development computer with the Eclipse IDE installed (see System and Software Requirements), and -you'll need to install the Android Eclipse Plugin (ADT). Once you have those ready, come back here.

- -

First, here's a high-level summary of how to build "Hello, World!":

- -
    -
  1. - Create a new "Android Project" via the File > New > Project menu. -
  2. -
  3. - Fill out the project details in the New Android Project dialog. -
  4. -
  5. - Edit the auto-generated source code template to display some output. -
  6. -
-

That's it! Next, let's go through each step above in detail.

-
    -
  1. Create a new Android Project -

    From Eclipse, select the File > New > Project menu item. If the Android - Plugin for Eclipse has been successfully installed, the resulting dialog - should have a folder labeled "Android" which should contain a single entry: - "Android Project".

    - -

    - -

    Once you've selected "Android Project", click the Next button.

    -
  2. - -
  3. Fill out the project details -

    The next screen allows you to enter the relevant details for your project. - Here's an example:

    - -

    - -

    Here's what each field on this screen means:

    - - - - - - - - - - - - - - - - - - - - - -
    Project Name This is the name of the directory or folder on your computer that you - want to contain the project.
    Package Name This is the package namespace (following the same rules as for - packages in the Java programming language) that you want all your source code to - reside under. This also sets the package name under which the stub - Activity will be generated.

    - The package name you use in your application must be unique across - all packages installed on the system; for this reason, it's very - important to use a standard domain-style package for your - applications. In the example above, we used the - package domain "com.android"; you should use a - different one appropriate to your organization.

    Activity Name This is the name for the class stub that will be generated by the plugin. - This will be a subclass of Android's Activity class. An Activity is simply a - class that can run and do work. It can create a UI if it chooses, but it - doesn't need to.
    Application Name This is the human-readable title for your application.
    - -

    The checkbox for toggling "Use default location" allows you to change the -location on disk where the project's files will be generated and stored.

    -
  4. - -
  5. Edit the auto-generated source code - -

    After the plugin runs, you'll have a class named HelloAndroid -(found in your package, HelloAndroid > src > com.android.hello). It should look like -this:

    - -
    public class HelloAndroid extends Activity {
    -    /** Called when the activity is first created. */
    -    @Override
    -    public void onCreate(Bundle savedInstanceState) {
    -        super.onCreate(savedInstanceState);
    -        setContentView(R.layout.main);
    -    }
    -}
    - -

    Now, you could run this right away, but let's go a little further, -so we understand more about what's happening. -So, the next step is to modify some code!

    -
  6. -
- - - -

Construct the UI

- -

Take a look at this revised code, below, and make the same changes to your HelloAndroid.java file. We'll dissect -it line by line:

- -
-package com.android.hello;
-
-import android.app.Activity;
-import android.os.Bundle;
-import android.widget.TextView;
-
-public class HelloAndroid extends Activity {
-   /** Called when the activity is first created. */
-   @Override
-   public void onCreate(Bundle savedInstanceState) {
-       super.onCreate(savedInstanceState);
-       TextView tv = new TextView(this);
-       tv.setText("Hello, Android");
-       setContentView(tv);
-   }
-}
- -

Tip: If you forgot to import the TextView package, try this: -press Ctrl-Shift-O (Cmd-Shift-O, on Mac). This is an Eclipse -shortcut to organize imports—it identifies missing packages and adds them for you.

- -

In Android, user interfaces are composed of hierarchies of classes called -Views. A View is simply a drawable object, such as a radio button, an -animation, or (in our case) a text label. The specific name for the View -subclass that handles text is simply TextView.

- -

Here's how you construct a TextView:

- -
TextView tv = new TextView(this);
- -

The argument to TextView's constructor is an Android Context instance. The -Context is simply a handle to the system; it provides services like -resolving resources, obtaining access to databases and preferences, and so -on. The Activity class inherits from Context. Since our -HelloAndroid class is a subclass of Activity, it is also a Context, and so we can -pass the this reference to the TextView.

- -

Once we've constructed the TextView, we need to tell it what to display:

- -
tv.setText("Hello, Android");
- -

Nothing too surprising there.

- -

At this point, we've constructed a TextView and told it what text to -display. The final step is to connect this TextView with the on-screen -display, like so:

- -
setContentView(tv);
- -

The setContentView() method on Activity indicates to the system which -View should be associated with the Activity's UI. If an Activity doesn't -call this method, no UI is present at all and the system will display a blank -screen. For our purposes, all we want is to display some text, so we pass it -the TextView we just created.

- -

There it is — "Hello, World" in Android! The next step, of course, is -to see it running.

- - - -

Run the Code: Hello, Android

- -

The Eclipse plugin makes it very easy to run your applications. Begin by -selecting the Run > Open Run Dialog menu entry (in Eclipse 3.4, it's -Run > Run Configurations). You should see a dialog -like this:

- -
- -
- -

Next, highlight the "Android Application" entry, and then click the icon in the -top left corner (the one depicting a sheet of paper with a plus sign in the -corner) or simply double-click the "Android Application" entry. You should -have a new launcher entry named "New_configuration".

- -
- -
- -

Change the name to something expressive, like "Hello, Android", and then pick -your project by clicking the Browse button. (If you have more than one -Android project open in Eclipse, be sure to pick the right one.) The -plugin will automatically scan your project for Activity subclasses, and add -each one it finds to the drop-down list under the "Activity:" label. Since -your "Hello, Android" project only has one, it will be the default, and you can -simply continue.

- -

Click the "Apply" button. Here's an example:

- -
- -
- -

That's it — you're done! Click the Run button, and the Android Emulator -should start. Once it's booted up your application will appear. When all is said and done, you should -see something like this:

- -
- -
- -

That's "Hello, World" in Android. Pretty straightforward, eh? -The next sections of the tutorial offer more detailed information that you may find valuable as you -learn more about Android.

- - - -

Upgrading the UI to an XML Layout

- -

The "Hello, World" example you just completed uses what we call "programmatic" -UI layout. This means that you construct and build your application's UI -directly in source code. If you've done much UI programming, you're -probably familiar with how brittle that approach can sometimes be: small -changes in layout can result in big source-code headaches. It's also very -easy to forget to properly connect Views together, which can result in errors in -your layout and wasted time debugging your code.

- -

That's why Android provides an alternate UI construction model: XML-based -layout files. The easiest way to explain this concept is to show an -example. Here's an XML layout file that is identical in behavior to the -programmatically-constructed example you just completed:

- -
<?xml version="1.0" encoding="utf-8"?>
-<TextView xmlns:android="http://schemas.android.com/apk/res/android"
-  android:layout_width="fill_parent"
-  android:layout_height="fill_parent"
-  android:text="Hello, Android"/>
- -

The general structure of an Android XML layout file is simple. It's a tree -of tags, where each tag is the name of a View class. In this example, it's -a very simple tree of one element, a TextView. You can use the -name of any class that extends View as a tag name in your XML layouts, -including custom View classes you define in your own code. This -structure makes it very easy to quickly build up UIs, using a much simpler -structure and syntax than you would in source code. This model is inspired -by the web development model, where you can separate the presentation of your -application (its UI) from the application logic used to fetch and fill in data.

- -

In this example, there are also four XML attributes. Here's a summary of what -they mean:

- - - - - - - - - - - - - - - - - - - - - - - - -
- Attribute - - Meaning -
- xmlns:android - - This is an XML namespace declaration that tells the Android tools that you are going to refer to common attributes defined in the Android namespace. The outermost tag in every Android layout file must have this attribute.
-
- android:layout_width - - This attribute defines how much of the available width on the screen this View should consume. In this case, it's our only View so we want it to take up the entire screen, which is what a value of "fill_parent" means.
-
- android:layout_height - - This is just like android:layout_width, except that it refers to available screen height. -
- android:text - - This sets the text that the TextView should contain. In this example, it's our usual "Hello, Android" message. -
- - -

So, that's what the XML layout looks like, but where do you put it? Under the /res/layout directory in your project. The "res" is -short for "resources" and that directory contains all the non-code assets that -your application requires. This includes things like images, localized -strings, and XML layout files.

- -

The Eclipse plugin creates one of these XML files for you. In our example -above, we simply never used it. In the Package Explorer, expand the -folder /res/layout, and edit the file main.xml. Replace its contents with -the text above and save your changes.

- -

Now open the file named R.java in your source code folder in the Package -Explorer. You'll see that it now looks something like this:

- -
-public final class R {
-    public static final class attr {
-    };
-    public static final class drawable {
-        public static final int icon=0x7f020000;
-    };
-    public static final class layout {
-        public static final int main=0x7f030000;
-    };
-    public static final class string {
-        public static final int app_name=0x7f040000;
-    };
-};
-
- -

A project's R.java file is an index into all the resources defined in the -file. You use this class in your source code as a sort of short-hand -way to refer to resources you've included in your project. This is -particularly powerful with the code-completion features of IDEs like Eclipse -because it lets you quickly and interactively locate the specific reference -you're looking for.

- -

The important thing to notice for now is the inner class named "layout", and its -member field "main". The Eclipse plugin noticed that you added a new XML -layout file and then regenerated this R.java file. As you add other -resources to your projects you'll see R.java change to keep up.

- -

The last thing you need to do is modify your HelloAndroid source code to use the new -XML version of your UI, instead of the hard-coded version. Here's what -your new class will look like. As you can see, the source code becomes much -simpler:

- -
package com.android.hello;
-
-import android.app.Activity;
-import android.os.Bundle;
-
-public class HelloAndroid extends Activity {
-    /** Called when the activity is first created. */
-    @Override
-    public void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-        setContentView(R.layout.main);
-    }
-}
- -

When you make this change, don't just copy-and-paste it in. Try out the code-completion feature on that R class. You'll probably find that it helps a lot.

- -

Now that you've made this change, go ahead and re-run your application — all -you need to do is click the green Run arrow icon, or select -Run > Run History > Hello, Android from the menu. You should see.... well, exactly the same thing -you saw before! After all, the point was to show that the two different -layout approaches produce identical results.

- -

There's a lot more to creating these XML layouts, but that's as far as we'll go -here. Read the Implementing a User Interface documentation for more -information on the power of this approach.

- - - -

Debugging Your Project

- -

The Android Plugin for Eclipse also has excellent integration with the Eclipse -debugger. To demonstrate this, let's introduce a bug into -our code. Change your HelloAndroid source code to look like this:

- -
-package com.android.hello;
-
-import android.app.Activity;
-import android.os.Bundle;
-
-public class HelloAndroid extends Activity {
-    /** Called when the activity is first created. */
-    @Override
-    public void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-        Object o = null;
-        o.toString();
-        setContentView(R.layout.main);
-    }
-}
- -

This change simply introduces a NullPointerException into your code. If -you run your application again, you'll eventually see this:

- -
- -
- -

Press "Force Quit" to terminate the application and close the emulator window.

- -

To find out more about the error, set a breakpoint in your source code -on the line Object o = null; (double-click on the marker bar next to the source code line). Then select Run > Debug History > Hello, -Android from the menu to enter debug mode. Your app will restart in the -emulator, but this time it will suspend when it reaches the breakpoint you -set. You can then step through the code in Eclipse's Debug Perspective, -just as you would for any other application.

- -
- -
- - - -

Creating the Project without Eclipse

- -

If you don't use Eclipse (such as if you prefer another IDE, or simply use text -editors and command line tools) then the Eclipse plugin can't help you. -Don't worry though — you don't lose any functionality just because you don't -use Eclipse.

- -

The Android Plugin for Eclipse is really just a wrapper around a set of tools -included with the Android SDK. (These tools, like the emulator, aapt, adb, -ddms, and others are documented elsewhere.) Thus, it's possible to -wrap those tools with another tool, such as an 'ant' build file.

- -

The Android SDK includes a Python script named "activitycreator.py" that can be -used to create all the source code and directory stubs for your project, as well -as an ant-compatible build.xml file. This allows you to build your project -from the command line, or integrate it with the IDE of your choice.

- -

For example, to create a HelloAndroid project similar to the one we just created -via Eclipse, you'd use this command:

- -
activitycreator.py --out HelloAndroid com.android.hello.HelloAndroid
- -

To build the project, you'd then run the command 'ant'. When that command -successfully completes, you'll be left with a file named HelloAndroid.apk under -the 'bin' directory. That .apk file is an Android Package, and can be -installed and run in your emulator using the 'adb' tool.

- -

For more information on how to use these tools, please read the documentation -cited above.

diff --git a/docs/html/guide/tutorials/hello-world.jd b/docs/html/guide/tutorials/hello-world.jd deleted file mode 100644 index 7b9287f..0000000 --- a/docs/html/guide/tutorials/hello-world.jd +++ /dev/null @@ -1,500 +0,0 @@ -page.title=Hello, World -@jd:body - -

As a developer, you know that the first impression -of a development framework is how easy it is to write "Hello, -World." Well, on Android, it's pretty easy. -It's particularly easy if you're using Eclipse as your IDE, because we've provided a -great plugin that handles your project creation and management to greatly speed up your -development cycles.

- -

If you're not using Eclipse, that's okay. Familiarize yourself with -Developing in Other IDEs. -You can then come back here and ignore anything about Eclipse.

- -

Before you start, you should already have the latest SDK installed, and if you're using -Eclipse, you should have installed the ADT plugin as well. See -Installing the Android SDK to get these -installed.

- -

Note: -In some cases, you might want to click the screenshots below to get a bigger view. -

- -

Create the Project

- -
    -
  1. Open a new Android Project. -

    From Eclipse, select the File > New > Project menu item. If the Android - Plugin for Eclipse has been successfully installed, the resulting dialog - should have a folder labeled "Android" which should contain a single entry: - "Android Project".

    -

    Selected "Android Project" and click Next.

    - - - -
  2. - -
  3. Fill out the project details. -

    The next screen allows you to enter the relevant details for your project:

    -
      -
    • Project name: HelloAndroid
    • -
    • Package name: com.example.hello (or your own private namespace)
    • -
    • Activity name: HelloAndroid
    • -
    • Application name: Hello, Android
    • -
    -

    Click Finish.

    - - -

    Here's what each field on this screen means:

    - -
    -
    Project Name
    -
    This is the name of the directory or folder on your computer that you - want to contain the project.
    -
    Package Name
    -
    This is the package namespace (following the same rules as for - packages in the Java programming language) that you want all your source code to - reside under. This also sets the package name under which the stub - Activity will be generated. -

    The package name you use in your application must be unique across - all packages installed on the system; for this reason, it's very - important to use a standard domain-style package for your - applications. In the example above, we used the - package domain "com.android"; you should use a - different one appropriate to your organization.

    -
    Activity Name
    -
    This is the name for the class stub that will be generated by the plugin. - This will be a subclass of Android's Activity class. An Activity is simply a - class that can run and do work. It can create a UI if it chooses, but it - doesn't need to.
    -
    Application Name
    -
    This is the human-readable title for your application.
    -
    - -

    The checkbox for toggling "Use default location" allows you to change - the location on disk where the project's files will be generated and stored.

    - -
  4. - -
  5. View the auto-generated source code. -

    After the plugin completes your project creations, you'll have a class named HelloAndroid -(found in your project package, HelloAndroid > src > com.android.hello). It should look like -this:

    - -
    -package com.example.hello;
    -
    -import android.app.Activity;
    -import android.os.Bundle;
    -
    -public class HelloAndroid extends Activity {
    -    /** Called when the activity is first created. */
    -    @Override
    -    public void onCreate(Bundle savedInstanceState) {
    -        super.onCreate(savedInstanceState);
    -        setContentView(R.layout.main);
    -    }
    -}
    - -

    Now let's modify some code!

    -
  6. -
- - -

Construct the UI

- -

Take a look at this revised code, below, and make the same changes to your HelloAndroid.java file. We'll dissect -it line by line:

- -
-package com.android.hello;
-
-import android.app.Activity;
-import android.os.Bundle;
-import android.widget.TextView;
-
-public class HelloAndroid extends Activity {
-   /** Called when the activity is first created. */
-   @Override
-   public void onCreate(Bundle savedInstanceState) {
-       super.onCreate(savedInstanceState);
-       TextView tv = new TextView(this);
-       tv.setText("Hello, Android");
-       setContentView(tv);
-   }
-}
- -

Tip: If you forgot to import the TextView package, try this: -press Ctrl-Shift-O (Cmd-Shift-O, on Mac). This is an Eclipse -shortcut to organize imports—it identifies missing packages and adds them for you.

- -

Notice that our class is based on the {@link android.app.Activity} class. An Activity is a -single application entity that is used to perform actions. An application may have many, but the user -interacts with them only one at a time. An Activity is not required to actually have a user interface, -but usually will.

- -

An Android user interface is composed of hierarchies of objects called -Views. A {@link android.view.View} is simply a drawable object, such as a radio button, an -animation, or (in our case) a text label. The specific name for the View -subclass that handles text, which we use here, is {@link android.widget.TextView}.

- -

Here's how you construct a TextView:

- -
TextView tv = new TextView(this);
- -

The argument to TextView's constructor is an Android {@link android.content.Context} instance. The -Context is simply a handle to the system; it provides services like -resolving resources, obtaining access to databases and preferences, and so -on. The Activity class inherits from Context. Since our -HelloAndroid class is a subclass of Activity, it is also a Context, and so we can -pass the this reference to the TextView.

- -

Once we've constructed the TextView, we need to tell it what to display:

- -
tv.setText("Hello, Android");
- -

Nothing too surprising there.

- -

At this point, we've constructed a TextView and told it what text to -display. The final step is to connect this TextView with the on-screen -display, like so:

- -
setContentView(tv);
- -

The setContentView() method on Activity indicates to the system which -View should be associated with the Activity's UI. If an Activity doesn't -call this method, no UI is present at all and the system will display a blank -screen. For our purposes, all we want is to display some text, so we pass it -the TextView we just created.

- -

There it is — "Hello, World" in Android! The next step, of course, is -to see it running.

- - - -

Run the Code: Hello, Android

- -

The Eclipse plugin makes it very easy to run your applications. Begin by -selecting the Run > Open Run Dialog menu entry (in Eclipse 3.4, it's -Run > Run Configurations). You should see a dialog -like this:

- - - -

Next, highlight the "Android Application" entry, and then click the icon in the -top left corner (the one depicting a sheet of paper with a plus sign in the -corner) or simply double-click the "Android Application" entry. You should -have a new launcher entry named "New_configuration".

- - - -

Change the name to something expressive, like "Hello, Android", and then pick -your project by clicking the Browse button. (If you have more than one -Android project open in Eclipse, be sure to pick the right one.) The -plugin will automatically scan your project for Activity subclasses, and add -each one it finds to the drop-down list under the "Activity:" label. Since -your "Hello, Android" project only has one, it will be the default, and you can -simply continue.

- -

Click the "Apply" button. Here's an example:

- - - -

Now click Run, and the Android Emulator should start and open the application. -Once it's booted up your application will appear. (Once booted, you may need to unlock the emulator's phone screen -by pressing the device MENU key.) When all is said and done, you should -see something like this:

- - - - -

That's "Hello, World" in Android. Pretty straightforward, eh? -The next sections of the tutorial offer more detailed information that you may find valuable as you -learn more about Android.

- - - - -

Upgrading the UI to an XML Layout

- -

The "Hello, World" example you just completed uses what we call "programmatic" -UI layout. This means that you construct and build your application's UI -directly in source code. If you've done much UI programming, you're -probably familiar with how brittle that approach can sometimes be: small -changes in layout can result in big source-code headaches. It's also very -easy to forget to properly connect Views together, which can result in errors in -your layout and wasted time debugging your code.

- -

That's why Android provides an alternate UI construction model: XML-based -layout files. The easiest way to explain this concept is to show an -example. Here's an XML layout file that is identical in behavior to the -programmatically-constructed example you just completed:

- -
<?xml version="1.0" encoding="utf-8"?>
-<TextView xmlns:android="http://schemas.android.com/apk/res/android"
-  android:layout_width="fill_parent"
-  android:layout_height="fill_parent"
-  android:text="@string/hello"/>
- -

The general structure of an Android XML layout file is simple: it's a tree -of XML elements, where each element is the name of a View class -(this example, however, is just one element). You can use the -name of any class that extends {@link android.view.View} as an element in your XML layouts, -including custom View classes you define in your own code. This -structure makes it very easy to quickly build up UIs, using a more simple -structure and syntax than you would in source code. This model is inspired -by the web development model, where you can separate the presentation of your -application (its UI) from the application logic used to fetch and fill in data.

- -

In this example, there's just one View element, the TextView, -which has four XML attributes. Here's a summary of what they mean:

- - - - - - - - - - - - - - - - - - - - - - - - -
- Attribute - - Meaning -
- xmlns:android - - This is an XML namespace declaration that tells the Android tools that you are going to refer to common attributes defined in the Android namespace. The outermost tag in every Android layout file must have this attribute.
-
- android:layout_width - - This attribute defines how much of the available width on the screen this View should consume. In this case, it's our only View so we want it to take up the entire screen, which is what a value of "fill_parent" means.
-
- android:layout_height - - This is just like android:layout_width, except that it refers to available screen height. -
- android:text - - This sets the text that the TextView should display. In this example, we use a string - resource instead of a hard-coded string value. - The hello string is defined in the res/values/strings.xml file. This is the - recommended practice for inserting strings to your application, because it makes the localization - of your application to other languages graceful, without need to hard-code changes to the layout file. - For more information, see Resources - and Internationalization. -
- - -

These layout files belong in the res/layout/ directory in your project. The "res" is -short for "resources" and that directory contains all the non-code assets that -your application requires. Resources also include things like images, localized -strings, and XML layout files.

- - - -

The Eclipse plugin automatically creates one of these layout files for you (main.xml). In our example -above, we just ignored it and created our layout programmatically. We did so just to teach you a little more -about the framework, but you should almost always define your layout in an XML file instead of in your code.

- -

So, let's put it to use and change the "Hello, Android" sample to use the XML layout.

- -
    -
  1. In the Eclipse Package Explorer, expand the -folder res/layout/, and open the file main.xml (once opened, you might need to click -the "main.xml" tab at the bottom to see the XML source). Replace its contents with -the following XML: -
    <?xml version="1.0" encoding="utf-8"?>
    -<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    -  android:layout_width="fill_parent"
    -  android:layout_height="fill_parent"
    -  android:text="@string/hello"/>
    -

    Save the file.

    -
  2. - -
  3. Inside the project folder res/values/, open the file strings.xml. -This is where you should save all default text strings for your user interface. If you're using Eclipse, then -ADT will have started you with two strings, hello and app_name. -Revise hello to something else. Perhaps "Hello, Android! I am a string resource!" -The entire file should now look like this: -
    -<?xml version="1.0" encoding="utf-8"?>
    -<resources>
    -    <string name="hello">Hello, Android! I am a string resource!</string>
    -    <string name="app_name">Hello, Android</string>
    -</resources>
    -
    -
  4. - -
  5. Now open and modify your HelloAndroid class source code to read the -XML layout, instead of the hard-coded version. Edit the file to look like this: -
    -package com.example.hello;
    -
    -import android.app.Activity;
    -import android.os.Bundle;
    -
    -public class HelloAndroid extends Activity {
    -    /** Called when the activity is first created. */
    -    @Override
    -    public void onCreate(Bundle savedInstanceState) {
    -        super.onCreate(savedInstanceState);
    -        setContentView(R.layout.main);
    -    }
    -}
    - -

    When you make this change, type it by hand to try the code-completion feature on that R class. You'll probably find that it helps a lot.

    - -

    Now, instead of passing setContentView() a View object, we give it a reference to our layout resource. -The resource is identified as R.layout.main, which is actually a compiled object representation of -the layout defined in layout/main.xml. The Eclipse plugin automatically creates this reference for -us inside the project's R.java class. If you're not using Eclipse, then the R.java class will be generated for you -when you run Ant to build the application. (More about the R class in a moment.)

    -
  6. -
- -

Now that you've made this change, go ahead and re-run your application — all -you need to do is click the green Run arrow icon, or select -Run > Run History > Hello, Android from the menu. You should see pretty much the same thing -you saw before! After all, the point was to show that the two different -layout approaches produce identical results.

- -

Tip: Use the shortcut Ctrl-Shift-F11 -(Cmd-Shift-F11, on Mac) to run your currently visible application.

- -

You've just completed your first Android application! Continue reading for an introduction -to debugging and a little more information on using other IDEs. Once you're ready to move on, -please begin by reading Application -Fundamentals. Also refer to the Developer's Guide -introduction page for an overview of the Dev Guide documentation.

- - -
-

R class

-

In Eclipse, open the file named R.java in your source code folder in the Package -Explorer. It should look something like this:

- -
-public final class R {
-    public static final class attr {
-    }
-    public static final class drawable {
-        public static final int icon=0x7f020000;
-    }
-    public static final class layout {
-        public static final int main=0x7f030000;
-    }
-    public static final class string {
-        public static final int app_name=0x7f040001;
-        public static final int hello=0x7f040000;
-    }
-}
-
- -

A project's R.java file is an index into all the resources defined in the -file. You use this class in your source code as a sort of short-hand -way to refer to resources you've included in your project. This is -particularly powerful with the code-completion features of IDEs like Eclipse -because it lets you quickly and interactively locate the specific reference -you're looking for.

- -

It's possible yours looks slighly different than this (perhaps the hexadecimal values are different). -For now, notice the inner class named "layout", and its -member field "main". The Eclipse plugin noticed the XML -layout file named main.xml and generated a class for it here. As you add other -resources to your project (such as strings in the res/values/string.xml file or drawables inside -the res/drawable/ direcory) you'll see R.java change to keep up.

-

When not using Eclipse, this class file will be generated for you at build time (with the Ant tool).

-

You should never edit this file by hand.

-
- -

Debugging Your Project

- -

The Android Plugin for Eclipse also has excellent integration with the Eclipse -debugger. To demonstrate this, let's introduce a bug into -our code. Change your HelloAndroid source code to look like this:

- -
-package com.android.hello;
-
-import android.app.Activity;
-import android.os.Bundle;
-
-public class HelloAndroid extends Activity {
-    /** Called when the activity is first created. */
-    @Override
-    public void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-        Object o = null;
-        o.toString();
-        setContentView(R.layout.main);
-    }
-}
- -

This change simply introduces a NullPointerException into your code. If -you run your application again, you'll eventually see this:

- - - -

Press "Force Quit" to terminate the application and close the emulator window.

- -

To find out more about the error, set a breakpoint in your source code -on the line Object o = null; (double-click on the marker bar next to the source code line). Then select Run > Debug History > Hello, -Android from the menu to enter debug mode. Your app will restart in the -emulator, but this time it will suspend when it reaches the breakpoint you -set. You can then step through the code in Eclipse's Debug Perspective, -just as you would for any other application.

- - - - -

Creating the Project without Eclipse

- -

If you don't use Eclipse (such as if you prefer another IDE, or simply use text - editors and command line tools) then the Eclipse plugin can't help you. - Don't worry though — you don't lose any functionality just because you don't - use Eclipse.

- -

The Android Plugin for Eclipse is really just a wrapper around a set of tools - included with the Android SDK. (These tools, like the emulator, aapt, adb, - ddms, and others are documented elsewhere.) Thus, it's possible to - wrap those tools with another tool, such as an 'ant' build file.

- -

The Android SDK includes a Python script named "activitycreator.py" that can be - used to create all the source code and directory stubs for your project, as well - as an ant-compatible build.xml file. This allows you to build your project - from the command line, or integrate it with the IDE of your choice.

- -

For example, to create a HelloAndroid project similar to the one we just created - via Eclipse, you'd use this command:

- -
activitycreator.py --out HelloAndroid com.android.hello.HelloAndroid
- -

To build the project, you'd then run the command 'ant'. When that command - successfully completes, you'll be left with a file named HelloAndroid.apk under - the 'bin' directory. That .apk file is an Android Package, and can be - installed and run in your emulator using the 'adb' tool.

- -

For more information on how to use these tools, please read the documentation - cited above.

diff --git a/docs/html/guide/tutorials/images/hello_world_0.png b/docs/html/guide/tutorials/images/hello_world_0.png deleted file mode 100644 index c174fba..0000000 Binary files a/docs/html/guide/tutorials/images/hello_world_0.png and /dev/null differ diff --git a/docs/html/guide/tutorials/images/hello_world_1.png b/docs/html/guide/tutorials/images/hello_world_1.png deleted file mode 100644 index f08438a..0000000 Binary files a/docs/html/guide/tutorials/images/hello_world_1.png and /dev/null differ diff --git a/docs/html/guide/tutorials/images/hello_world_2.png b/docs/html/guide/tutorials/images/hello_world_2.png deleted file mode 100644 index 58f5703..0000000 Binary files a/docs/html/guide/tutorials/images/hello_world_2.png and /dev/null differ diff --git a/docs/html/guide/tutorials/images/hello_world_3.png b/docs/html/guide/tutorials/images/hello_world_3.png deleted file mode 100644 index d2d2ff6..0000000 Binary files a/docs/html/guide/tutorials/images/hello_world_3.png and /dev/null differ diff --git a/docs/html/guide/tutorials/images/hello_world_4.png b/docs/html/guide/tutorials/images/hello_world_4.png deleted file mode 100644 index 5c41e80..0000000 Binary files a/docs/html/guide/tutorials/images/hello_world_4.png and /dev/null differ diff --git a/docs/html/guide/tutorials/images/hello_world_5.png b/docs/html/guide/tutorials/images/hello_world_5.png deleted file mode 100644 index 96b830a..0000000 Binary files a/docs/html/guide/tutorials/images/hello_world_5.png and /dev/null differ diff --git a/docs/html/guide/tutorials/images/hello_world_8.png b/docs/html/guide/tutorials/images/hello_world_8.png deleted file mode 100644 index 07db360..0000000 Binary files a/docs/html/guide/tutorials/images/hello_world_8.png and /dev/null differ diff --git a/docs/html/guide/tutorials/images/hello_world_9.png b/docs/html/guide/tutorials/images/hello_world_9.png deleted file mode 100644 index e172e63..0000000 Binary files a/docs/html/guide/tutorials/images/hello_world_9.png and /dev/null differ diff --git a/docs/html/guide/tutorials/index.html b/docs/html/guide/tutorials/index.html deleted file mode 100644 index 4881acf..0000000 --- a/docs/html/guide/tutorials/index.html +++ /dev/null @@ -1,8 +0,0 @@ - - - - - -click here if you are not redirected. - - \ No newline at end of file diff --git a/docs/html/guide/tutorials/notepad/codelab/NotepadCodeLab.zip b/docs/html/guide/tutorials/notepad/codelab/NotepadCodeLab.zip deleted file mode 100644 index c7dd989..0000000 Binary files a/docs/html/guide/tutorials/notepad/codelab/NotepadCodeLab.zip and /dev/null differ diff --git a/docs/html/guide/tutorials/notepad/index.jd b/docs/html/guide/tutorials/notepad/index.jd deleted file mode 100644 index 6319fad..0000000 --- a/docs/html/guide/tutorials/notepad/index.jd +++ /dev/null @@ -1,142 +0,0 @@ -page.title=Notepad Tutorial -@jd:body - - -

This tutorial on writing a notepad application gives you a "hands-on" introduction -to the Android framework and the tools you use to build applications on it. -Starting from a preconfigured project file, it guides you through the process of -developing a simple notepad application and provides concrete examples of how to -set up the project, develop the application logic and user interface, and then -compile and run the application.

- -

The tutorial presents the application development as a set of -exercises (see below), each consisting of several steps. You should follow -the steps in each exercise to gradually build and refine your -application. The exercises explain each step in detail and provide all the -sample code you need to complete the application.

- -

When you are finished with the tutorial, you will have created a functioning -Android application and will have learned many of the most important -concepts in Android development. If you want to add more complex features to -your application, you can examine the code in an alternative implementation -of a Note Pad application, in the -Sample Code section.

- - - -

Who Should Use this Tutorial

- -

This tutorial is designed for experienced developers, especially those with -knowledge of the Java programming language. If you haven't written Java -applications before, you can still use the tutorial, but you might need to work -at a slower pace.

- -

Also note that this tutorial uses -the Eclipse development environment, with the Android plugin installed. If you -are not using Eclipse, you can follow the exercises and build the application, -but you will need to determine how to accomplish the Eclipse-specific -steps in your environment.

- - -

Preparing for the Exercises

- -

The tutorial assumes that you have some familiarity with basic Android -application concepts and terminology. If you are not, you -should read Application -Fundamentals before continuing.

- -

This tutorial also builds on the introductory information provided in the -Hello World -tutorial, which explains how to set up your Eclipse environment -for building Android applications. We recommend you complete the Hello World -tutorial before starting this one.

- -

To prepare for this lesson:

- -
    -
  1. Download the project - exercises archive (.zip).
  2. -
  3. Unpack the archive file to a suitable location on your machine.
  4. -
  5. Open the NotepadCodeLab folder.
  6. -
- -

Inside the NotepadCodeLab folder, you should see six project -files: Notepadv1, - Notepadv2, Notepadv3, - Notepadv1Solution, Notepadv2Solution - and Notepadv3Solution. The Notepadv# projects are -the starting points for each of the exercises, while the -Notepadv#Solution projects are the exercise - solutions. If you are having trouble with a particular exercise, you - can compare your current work against the exercise solution.

- - -

Exercises

- -

The table below lists the tutorial exercises and describes the development -areas that each covers. Each exercise assumes that you have completed any -previous exercises.

- - - - - - - - - - - - - - - - - - -
Exercise -1Start here. Construct a simple notes list that lets the user add new notes but not -edit them. Demonstrates the basics of ListActivity and creating -and handling - menu options. Uses a SQLite database to store the notes.
Exercise 2Add a second Activity to the -application. Demonstrates constructing a -new Activity, adding it to the Android manifest, passing data between the -activities, and using more advanced screen layout. Also shows how to -invoke another Activity to return a result, using -startActivityForResult().
Exercise 3Add handling of life-cycle events to -the application, to let it -maintain application state across the life cycle.
Extra -CreditDemonstrates how to use the Eclipse -debugger and how you can use it to -view life-cycle events as they are generated. This section is optional but -highly recommended.
- - - -

Other Resources and Further Learning

- diff --git a/docs/html/guide/tutorials/notepad/notepad-ex1.jd b/docs/html/guide/tutorials/notepad/notepad-ex1.jd deleted file mode 100644 index b7f42bf..0000000 --- a/docs/html/guide/tutorials/notepad/notepad-ex1.jd +++ /dev/null @@ -1,589 +0,0 @@ -page.title=Notepad Exercise 1 -parent.title=Notepad Tutorial -parent.link=index.html -@jd:body - - -

In this exercise, you will construct a simple notes list that lets the -user add new notes but not edit them. The exercise demonstrates:

- - -
- - [Exercise 1] - [Exercise 2] - [Exercise 3] - [Extra Credit] -
- - - -

Step 1

- -

Open up the Notepadv1 project in Eclipse.

- -

Notepadv1 is a project that is provided as a starting point. It - takes care of some of the boilerplate work that you have already seen if you - followed the Hello, - World tutorial.

- -
    -
  1. - Start a new Android Project by clicking File > - New > Android Project.
  2. -
  3. - In the New Android Project dialog, select Create project from existing source.
  4. -
  5. - Click Browse and navigate to where you copied the NotepadCodeLab - (downloaded during setup). Select - Notepadv1 and click Choose.
  6. -
  7. - You should see Notepadv1 in the Project name and also see the Location - filled in with the path you selected.
  8. -
  9. - Click Finish. The Notepadv1 project should open and be - visible in your Eclipse package explorer.
  10. -
- -

If you see an error about AndroidManifest.xml, or some - problems related to an Android zip file, right click on the project and - select Android Tools > Fix Project Properties. - (The project is looking in the wrong location for the library file, - this will fix it for you.)

- -

Step 2

- - - -

Take a look at the NotesDbAdapter class — this class is provided to - encapsulate data access to a SQLite database that will hold our notes data - and allow us to update it.

-

At the top of the class are some constant definitions that will be used in the application - to look up data from the proper field names in the database. There is also a database creation - string defined, which is used to create a new database schema if one doesn't exist already.

-

Our database will have the name data, and have a single table called - notes, which in turn has three fields: _id, title and - body. The _id is named with an underscore convention used in a number of - places inside the Android SDK and helps keep a track of state. The _id - usually has to be specified when querying or updating the database (in the column projections - and so on). The other two fields are simple text fields that will store data. -

-

The constructor for NotesDbAdapter takes a Context, which allows it to communicate with aspects - of the Android operating system. This is quite common for classes that need to touch the - Android system in some way. The Activity class implements the Context class, so usually you will just pass - this from your Activity, when needing a Context.

-

The open() method calls up an instance of DatabaseHelper, which is our local - implementation of the SQLiteOpenHelper class. It calls getWritableDatabase(), - which handles creating/opening a database for us.

-

close() just closes the database, releasing resources related to the - connection.

-

createNote() takes strings for the title and body of a new note, - then creates that note in the database. Assuming the new note is created successfully, the - method also returns the row _id value for the newly created note.

-

deleteNote() takes a rowId for a particular note, and deletes that note from - the database.

- -

fetchAllNotes() issues a query to return a {@link android.database.Cursor} over all notes in the - database. The query() call is worth examination and understanding. The first field is the - name of the database table to query (in this case DATABASE_TABLE is "notes"). - The next is the list of columns we want returned, in this case we want the _id, - title and body columns so these are specified in the String array. - The remaining fields are, in order: selection, - selectionArgs, groupBy, having and orderBy. - Having these all null means we want all data, need no grouping, and will take the default - order. See {@link android.database.sqlite.SQLiteDatabase SQLiteDatabase} for more details.

-

Note: A Cursor is returned rather than a collection of rows. This allows - Android to use resources efficiently -- instead of putting lots of data straight into memory - the cursor will retrieve and release data as it is needed, which is much more efficient for - tables with lots of rows.

- -

fetchNote() is similar to fetchAllNotes() but just gets one note - with the rowId we specify. It uses a slightly different version of the - {@link android.database.sqlite.SQLiteDatabase} query() method. - The first parameter (set true) indicates that we are interested - in one distinct result. The selection parameter (the fourth parameter) has been specified to search - only for the row "where _id =" the rowId we passed in. So we are returned a Cursor on - the one row.

-

And finally, updateNote() takes a rowId, title and body, and uses a - {@link android.content.ContentValues ContentValues} instance to update the note of the given - rowId.

- -

Step 3

- - - -

Open the notepad_list.xml file in res/layout -and - take a look at it. (You may have to - hit the xml tab, at the bottom, in order to view the XML markup.)

- -

This is a mostly-empty layout definition file. Here are some - things you should know about a layout file:

- - - - -

Step 4

-

We need to create the layout to hold our list. Add code inside - of the LinearLayout element so the whole file looks like this:

-
-<?xml version="1.0" encoding="utf-8"?>
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="wrap_content"
-    android:layout_height="wrap_content">
-
-  <ListView android:id="@android:id/list"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"/>
-  <TextView android:id="@android:id/empty"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:text="@string/no_notes"/>
-
-</LinearLayout>
-
- - -

Step 5

- - -

To make the list of notes in the ListView, we also need to define a View for each row:

-
    -
  1. - Create a new file under res/layout called - notes_row.xml.
  2. -
  3. - Add the following contents (note: again the XML header is used, and the - first node defines the Android XML namespace)
    -
    -<?xml version="1.0" encoding="utf-8"?>
    -<TextView android:id="@+id/text1"
    -    xmlns:android="http://schemas.android.com/apk/res/android"
    -    android:layout_width="wrap_content"
    -    android:layout_height="wrap_content"/>
    -

    - This is the View that will be used for each notes title row — it has only - one text field in it.

    -

    In this case we create a new id called text1. The - + after the @ in the id string indicates that the id should - be automatically created as a resource if it does not already exist, so we are defining - text1 on the fly and then using it.

    -
  4. -
  5. Save the file.
  6. -
-

Open the R.java class in the - project and look at it, you should see new definitions for - notes_row and text1 (our new definitions) - meaning we can now gain access to these from the our code.

- -

Step 6

-

Next, open the Notepadv1 class in the source. In the following steps, we are going to - alter this class to become a list adapter and display our notes, and also - allow us to add new notes.

- -

Notepadv1 will inherit from a subclass - of Activity called a ListActivity, - which has extra functionality to accommodate the kinds of - things you might want to do with a list, for - example: displaying an arbitrary number of list items in rows on the screen, - moving through the list items, and allowing them to be selected.

- -

Take a look through the existing code in Notepadv1 class. - There is a currently an unused private field called mNoteNumber that - we will use to create numbered note titles.

-

There are also three override methods defined: - onCreate, onCreateOptionsMenu and - onOptionsItemSelected; we need to fill these - out:

- - -

Step 7

-

Change the inheritance of Notepadv1 from -Activity - to ListActivity:

-
public class Notepadv1 extends ListActivity
-

Note: you will have to import ListActivity into the -Notepadv1 - class using Eclipse, ctrl-shift-O on Windows or Linux, or - cmd-shift-O on the Mac (organize imports) will do this for you - after you've written the above change.

- -

Step 8

-

Fill out the body of the onCreate() method.

-

Here we will set the title for the Activity (shown at the top of the - screen), use the notepad_list layout we created in XML, - set up the NotesDbAdapter instance that will - access notes data, and populate the list with the available note - titles:

-
    -
  1. - In the onCreate method, call super.onCreate() with the - savedInstanceState parameter that's passed in.
  2. -
  3. - Call setContentView() and pass R.layout.notepad_list.
  4. -
  5. - At the top of the class, create a new private class field called mDbHelper of class - NotesDbAdapter. -
  6. -
  7. - Back in the onCreate method, construct a new -NotesDbAdapter - instance and assign it to the mDbHelper field (pass - this into the constructor for DBHelper) -
  8. -
  9. - Call the open() method on mDbHelper to open (or create) the - database. -
  10. -
  11. - Finally, call a new method fillData(), which will get the data and - populate the ListView using the helper — we haven't defined this method yet.
  12. -
-

- onCreate() should now look like this:

-
-    @Override
-    public void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-        setContentView(R.layout.notepad_list);
-        mDbHelper = new NotesDbAdapter(this);
-        mDbHelper.open();
-        fillData();
-    }
-

And be sure you have the mDbHelper field definition (right - under the mNoteNumber definition):

-
    private NotesDbAdapter mDbHelper;
- -

Step 9

- - - -

Fill out the body of the onCreateOptionsMenu() method.

- -

We will now create the "Add Item" button that can be accessed by pressing the menu -button on the device. We'll specify that it occupy the first position in the menu.

- -
    -
  1. - In strings.xml resource (under res/values), add - a new string named "menu_insert" with its value set to Add Item: -
    <string name="menu_insert">Add Item</string>
    -

    Then save the file and return to Notepadv1.

    -
  2. -
  3. Create a menu position constant at the top of the class: -
    public static final int INSERT_ID = Menu.FIRST;
    -
  4. -
  5. In the onCreateOptionsMenu() method, change the - super call so we capture the boolean return as result. We'll return this value at the end.
  6. -
  7. Then add the menu item with menu.add().
  8. -
-

The whole method should now look like this: -

-    @Override
-    public boolean onCreateOptionsMenu(Menu menu) {
-        boolean result = super.onCreateOptionsMenu(menu);
-        menu.add(0, INSERT_ID, 0, R.string.menu_insert);
-        return result;
-    }
-

The arguments passed to add() indicate: a group identifier for this menu (none, - in this case), a unique ID (defined above), the order of the item (zero indicates no preference), - and the resource of the string to use for the item.

- -

Step 10

-

Fill out the body of the onOptionsItemSelected() method:

-

This is going - to handle our new "Add Note" menu item. When this is selected, the - onOptionsItemSelected() method will be called with the - item.getId() set to INSERT_ID (the constant we - used to identify the menu item). We can detect this, and take the - appropriate actions:

-
    -
  1. - The super.onOptionsItemSelected(item) method call goes at the - end of this method — we want to catch our events first!
  2. -
  3. - Write a switch statement on item.getItemId(). -

    In the case of INSERT_ID, call a new method, createNote(), - and return true, because we have handled this event and do not want to - propagate it through the system.

    -
  4. -
  5. Return the result of the superclass' onOptionsItemSelected() - method at the end.
  6. -
-

- The whole onOptionsItemSelect() method should now look like - this:

-
-    @Override
-    public boolean onOptionsItemSelected(MenuItem item) {
-        switch (item.getItemId()) {
-        case INSERT_ID:
-            createNote();
-            return true;
-        }
-       
-        return super.onOptionsItemSelected(item);
-    }
- -

Step 11

-

Add a new createNote() method:

-

In this first version of - our application, createNote() is not going to be very useful. -We will simply - create a new note with a title assigned to it based on a counter ("Note 1", - "Note 2"...) and with an empty body. At present we have no way of editing - the contents of a note, so for now we will have to be content making one - with some default values:

-
    -
  1. Construct the name using "Note" and the counter we defined in the class: - String noteName = "Note " + mNoteNumber++
  2. -
  3. - Call mDbHelper.createNote() using noteName as the - title and "" for the body -
  4. -
  5. - Call fillData() to populate the list of notes (inefficient but - simple) — we'll create this method next.
  6. -
-

- The whole createNote() method should look like this:

-
-    private void createNote() {
-        String noteName = "Note " + mNoteNumber++;
-        mDbHelper.createNote(noteName, "");
-        fillData();
-    }
- - -

Step 12

- - -

Define the fillData() method:

-

This - method uses SimpleCursorAdapter, which takes a database Cursor - and binds it to fields provided in the layout. These fields define the row elements of our list - (in this case we use the text1 field in our - notes_row.xml layout), so this allows us to easily populate the list with - entries from our database.

-

To do this we have to provide a mapping from the title field in the returned Cursor, to - our text1 TextView, which is done by defining two arrays: the first a string array - with the list of columns to map from (just "title" in this case, from the constant - NotesDbAdapter.KEY_TITLE) and, the second, an int array - containing references to the views that we'll bind the data into - (the R.id.text1 TextView).

-

This is a bigger chunk of code, so let's first take a look at it:

- -
-    private void fillData() {
-        // Get all of the notes from the database and create the item list
-        Cursor c = mDbHelper.fetchAllNotes();
-        startManagingCursor(c);
-
-        String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
-        int[] to = new int[] { R.id.text1 };
-        
-        // Now create an array adapter and set it to display using our row
-        SimpleCursorAdapter notes =
-            new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
-        setListAdapter(notes);
-    }
- -

Here's what we've done:

-
    -
  1. - After obtaining the Cursor from mDbHelper.fetchAllNotes(), we - use an Activity method called - startManagingCursor() that allows Android to take care of the - Cursor lifecycle instead of us needing to worry about it. (We will cover the implications - of the lifecycle in exercise 3, but for now just know that this allows Android to do some - of our resource management work for us.)
  2. -
  3. - Then we create a string array in which we declare the column(s) we want - (just the title, in this case), and an int array that defines the View(s) - to which we'd like to bind the columns (these should be in order, respective to - the string array, but here we only have one for each).
  4. -
  5. - Next is the SimpleCursorAdapter instantiation. - Like many classes in Android, the SimpleCursorAdapter needs a Context in order to do its - work, so we pass in this for the context (since subclasses of Activity - implement Context). We pass the notes_row View we created as the receptacle - for the data, the Cursor we just created, and then our arrays.
  6. -
-

- In the future, remember that the mapping between the from columns and to resources - is done using the respective ordering of the two arrays. If we had more columns we wanted - to bind, and more Views to bind them in to, we would specify them in order, for example we - might use { NotesDbAdapter.KEY_TITLE, NotesDbAdapter.KEY_BODY } and - { R.id.text1, R.id.text2 } to bind two fields into the row (and we would also need - to define text2 in the notes_row.xml, for the body text). This is how you can bind multiple fields - into a single row (and get a custom row layout as well).

-

- If you get compiler errors about classes not being found, ctrl-shift-O or - (cmd-shift-O on the mac) to organize imports. -

- -

Step 13

-

Run it! -

    -
  1. - Right click on the Notepadv1 project.
  2. -
  3. - From the popup menu, select Run As > - Android Application.
  4. -
  5. - If you see a dialog come up, select Android Launcher as the way of running - the application (you can also use the link near the top of the dialog to - set this as your default for the workspace; this is recommended as it will - stop the plugin from asking you this every time).
  6. -
  7. Add new notes by hitting the menu button and selecting Add - Item from the menu.
  8. -
- -

Solution and Next Steps

-

You can see the solution to this class in Notepadv1Solution -from -the zip file to compare with your own.

- -

Once you are ready, move on to Tutorial -Exercise 2 to add the ability to create, edit and delete notes.

- diff --git a/docs/html/guide/tutorials/notepad/notepad-ex2.jd b/docs/html/guide/tutorials/notepad/notepad-ex2.jd deleted file mode 100644 index 3b8fa0b..0000000 --- a/docs/html/guide/tutorials/notepad/notepad-ex2.jd +++ /dev/null @@ -1,647 +0,0 @@ -page.title=Notepad Exercise 2 -parent.title=Notepad Tutorial -parent.link=index.html -@jd:body - - -

In this exercise, you will add a second Activity to your notepad application, to let the user -create and edit notes. You will also allow the user to delete existing notes through a context menu. -The new Activity assumes responsibility for creating new notes by -collecting user input and packing it into a return Bundle provided by the intent. This exercise -demonstrates:

- - -
- [Exercise 1] - - [Exercise 2] - - [Exercise 3] - [Extra Credit] -
- -

Step 1

- -

Create a new Android project using the sources from Notepadv2 under the -NotepadCodeLab folder, just like you did for the first exercise. If you see an error about -AndroidManifest.xml, or some problems related to an -android.zip file, right click on the project and select Android -Tools > Fix Project Properties.

- -

Open the Notepadv2 project and take a look around:

- - - -

Step 2

- - - -

First, let's create the context menu that will allow users to delete individual notes. -Open the Notepadv2 class.

- -
    -
  1. In order for each list item in the ListView to register for the context menu, we call - registerForContextMenu() and pass it our ListView. So, at the very end of - the onCreate() method add this line: -
    registerForContextMenu(getListView());
    -

    Because our Activity extends the ListActivity class, getListView() will return us - the local ListView object for the Activity. Now, each list item in this ListView will activate the - context menu. -

  2. - Now fill in the onCreateContextMenu() method. This callback is similar to the other - menu callback used for the options menu. Here, we add just one line, which will add a menu item - to delete a note. Call menu.add() like so: -
    -public boolean onCreateContextMenu(Menu menu, View v
    -        ContextMenuInfo menuInfo) {
    -    super.onCreateContextMenu(menu, v, menuInfo);
    -    menu.add(0, DELETE_ID, 0, R.string.menu_delete);
    -}
    -

    The onCreateContextMenu() callback some passes other information in addition to the Menu object, - such as the View that has been triggered for the menu and - an extra object that may contain additional information about the object selected. However, we don't care about - these here, because we only have one kind of object in the Activity that uses context menus. In the next - step, we'll handle the menu item selection.

    -
  3. -
- -

Step 3

-

Now that the we've registered our ListView for a context menu and defined our context menu item, we need - to handle the callback when it is selected. For this, we need to identify the list ID of the - selected item, then delete it. So fill in the - onContextItemSelected() method like this:

-
-public boolean onContextItemSelected(MenuItem item) {
-    switch(item.getItemId()) {
-    case DELETE_ID:
-        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
-        mDbHelper.deleteNote(info.id);
-        fillData();
-        return true;
-    }
-    return super.onContextItemSelected(item);
-}
-

Here, we retrieve the {@link android.widget.AdapterView.AdapterContextMenuInfo AdapterContextMenuInfo} -with {@link android.view.MenuItem#getMenuInfo()}. The id field of this object tells us -the position of the item in the ListView. We then pass this to the deleteNote() -method of our NotesDbAdapter and the note is deleted. That's it for the context menu — notes -can now be deleted.

- -

Step 4

- - -

Fill in the body of the createNote() method: -

Create a new Intent to create a note - (ACTIVITY_CREATE) using the NoteEdit class. - Then fire the Intent using the startActivityForResult() method - call:

-
-Intent i = new Intent(this, NoteEdit.class);
-startActivityForResult(i, ACTIVITY_CREATE);
-

This form of the Intent call targets a specific class in our Activity, in this case - NoteEdit. Since the Intent class will need to communicate with the Android - operating system to route requests, we also have to provide a Context (this).

-

The startActivityForResult() method fires the Intent in a way that causes a method - in our Activity to be called when the new Activity is completed. The method in our Activity - that receives the callback is called - onActivityResult() and we will implement it in a later step. The other way - to call an Activity is using startActivity() but this is a "fire-and-forget" way - of calling it — in this manner, our Activity is not informed when the Activity is completed, and there is - no way to return result information from the called Activity with startActivity(). -

Don't worry about the fact that NoteEdit doesn't exist yet, - we will fix that soon.

- - - -

Step 5

- -

Fill in the body of the onListItemClick() override.

-

onListItemClick() is a callback method that we'll override. It is called when - the user selects an item from the list. It is passed four parameters: the - ListView object it was invoked from, the View - inside the ListView that was clicked on, the - position in the list that was clicked, and the - mRowId of the item that was clicked. In this instance we can - ignore the first two parameters (we only have one ListView it - could be), and we ignore the mRowId as well. All we are - interested in is the position that the user selected. We use - this to get the data from the correct row, and bundle it up to send to - the NoteEdit Activity.

-

In our implementation of the callback, the method creates an - Intent to edit the note using - the NoteEdit class. It then adds data into the extras Bundle of - the Intent, which will be passed to the called Activity. We use it - to pass in the title and body text, and the mRowId for the note we are - editing. Finally, it will fire the Intent using the - startActivityForResult() method call. Here's the code that - belongs in onListItemClick():

-
-super.onListItemClick(l, v, position, id);
-Cursor c = mNotesCursor;
-c.moveToPosition(position);
-Intent i = new Intent(this, NoteEdit.class);
-i.putExtra(NotesDbAdapter.KEY_ROWID, id);
-i.putExtra(NotesDbAdapter.KEY_TITLE, c.getString(
-        c.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
-i.putExtra(NotesDbAdapter.KEY_BODY, c.getString(
-        c.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
-startActivityForResult(i, ACTIVITY_EDIT);
- -

Note: We assign the mNotesCursor field to a local variable at the - start of the method. This is done as an optimization of the Android code. Accessing a local - variable is much more efficient than accessing a field in the Dalvik VM, so by doing this - we make only one access to the field, and five accesses to the local variable, making the - routine much more efficient. It is recommended that you use this optimization when possible.

- - -

Step 6

- -

The above createNote() and onListItemClick() - methods use an asynchronous Intent invocation. We need a handler for the callback, so here we fill - in the body of the onActivityResult().

-

onActivityResult() is the overridden method - which will be called when an Activity returns with a result. (Remember, an Activity - will only return a result if launched with startActivityForResult.) The parameters provided - to the callback are:

- -

The combination of startActivityForResult() and - onActivityResult() can be thought of as an asynchronous RPC - (remote procedure call) and forms the recommended way for an Activity to invoke - another and share services.

-

Here's the code that belongs in your onActivityResult():

-
-super.onActivityResult(requestCode, resultCode, intent);
-Bundle extras = intent.getExtras();
-
-switch(requestCode) {
-case ACTIVITY_CREATE:
-    String title = extras.getString(NotesDbAdapter.KEY_TITLE);
-    String body = extras.getString(NotesDbAdapter.KEY_BODY);
-    mDbHelper.createNote(title, body);
-    fillData();
-    break;
-case ACTIVITY_EDIT:
-    Long mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
-    if (mRowId != null) {
-        String editTitle = extras.getString(NotesDbAdapter.KEY_TITLE);
-        String editBody = extras.getString(NotesDbAdapter.KEY_BODY);
-        mDbHelper.updateNote(mRowId, editTitle, editBody);
-    }
-    fillData();
-    break;
-}
- - - - -

Step 7

- - - -

Open the file note_edit.xml that has been provided and take a - look at it. This is the UI code for the Note Editor.

-

This is the most - sophisticated UI we have dealt with yet. The file is given to you to avoid - problems that may sneak in when typing the code. (The XML is very strict - about case sensitivity and structure, mistakes in these are the usual cause - of problems with layout.)

-

There is a new parameter used - here that we haven't seen before: android:layout_weight (in - this case set to use the value 1 in each case).

-

layout_weight is used in LinearLayouts - to assign "importance" to Views within the layout. All Views have a default - layout_weight of zero, meaning they take up only as much room - on the screen as they need to be displayed. Assigning a value higher than - zero will split up the rest of the available space in the parent View, according - to the value of each View's layout_weight and its ratio to the - overall layout_weight specified in the current layout for this - and other View elements.

-

To give an example: let's say we have a text label - and two text edit elements in a horizontal row. The label has no - layout_weight specified, so it takes up the minimum space - required to render. If the layout_weight of each of the two - text edit elements is set to 1, the remaining width in the parent layout will - be split equally between them (because we claim they are equally important). - If the first one has a layout_weight of 1 - and the second has a layout_weight of 2, then one third of the - remaining space will be given to the first, and two thirds to the - second (because we claim the second one is more important).

-

This layout also demonstrates how to nest multiple layouts - inside each other to achieve a more complex and pleasant layout. In this - example, a horizontal linear layout is nested inside the vertical one to - allow the title label and text field to be alongside each other, - horizontally.

- - -

Step 8

- -

Create a NoteEdit class that extends - android.app.Activity.

-

This is the first time we will have - created an Activity without the Android Eclipse plugin doing it for us. When - you do so, the onCreate() method is not automatically - overridden for you. It is hard to imagine an Activity that doesn't override - the onCreate() method, so this should be the first thing you do.

-
    -
  1. Right click on the com.android.demo.notepad2 package - in the Package Explorer, and select New > Class from the popup - menu.
  2. -
  3. Fill in NoteEdit for the Name: field in the - dialog.
  4. -
  5. In the Superclass: field, enter - android.app.Activity (you can also just type Activity and hit - Ctrl-Space on Windows and Linux or Cmd-Space on the Mac, to invoke code - assist and find the right package and class).
  6. -
  7. Click Finish.
  8. -
  9. In the resulting NoteEdit class, right click in the editor - window and select Source > Override/Implement Methods...
  10. -
  11. Scroll down through the checklist in the dialog until you see - onCreate(Bundle) — and check the box next to it.
  12. -
  13. Click OK.

    The method should now appear in your class.

  14. -
- -

Step 9

- -

Fill in the body of the onCreate() method for NoteEdit.

- -

This will set the title of our new Activity to say "Edit Note" (one - of the strings defined in strings.xml). It will also set the - content view to use our note_edit.xml layout file. We can then - grab handles to the title and body text edit views, and the confirm button, - so that our class can use them to set and get the note title and body, - and attach an event to the confirm button for when it is pressed by the - user.

-

We can then unbundle the values that were passed in to the Activity - with the extras Bundle attached to the calling Intent. We'll use them to pre-populate - the title and body text edit views so that the user can edit them. - Then we will grab and store the mRowId so we can keep - track of what note the user is editing.

- -
    -
  1. - Inside onCreate(), set up the layout:
    -
    setContentView(R.layout.note_edit);
    -
  2. -
  3. - Find the edit and button components we need: -

    These are found by the - IDs associated to them in the R class, and need to be cast to the right - type of View (EditText for the two text views, - and Button for the confirm button):

    -
    -mTitleText = (EditText) findViewById(R.id.title);
    -mBodyText = (EditText) findViewById(R.id.body);
    -Button confirmButton = (Button) findViewById(R.id.confirm);
    -

    Note that mTitleText and mBodyText are member - fields (you need to declare them at the top of the class definition).

    -
  4. -
  5. At the top of the class, declare a Long mRowId private field to store - the current mRowId being edited (if any). -
  6. -
  7. Continuing inside onCreate(), - add code to initialize the title, body and - mRowId from the extras Bundle in - the Intent (if it is present):
    -
    -mRowId = null;
    -Bundle extras = getIntent().getExtras();
    -if (extras != null) {
    -    String title = extras.getString(NotesDbAdapter.KEY_TITLE);
    -    String body = extras.getString(NotesDbAdapter.KEY_BODY);
    -    mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
    -           
    -    if (title != null) {
    -        mTitleText.setText(title);
    -    }
    -    if (body != null) {
    -        mBodyText.setText(body);
    -    }
    -}
    -
      -
    • - We are pulling the title and - body out of the - extras Bundle that was set from the - Intent invocation. -
    • - We also null-protect the text field setting (i.e., we don't want to set - the text fields to null accidentally).
    • -
    -
  8. -
  9. - Create an onClickListener() for the button: -

    Listeners can be one of the more confusing aspects of UI - implementation, but - what we are trying to achieve in this case is simple. We want an - onClick() method to be called when the user presses the - confirm button, and use that to do some work and return the values - of the edited note to the Intent caller. We do this using something called - an anonymous inner class. This is a bit confusing to look at unless you - have seen them before, but all you really need to take away from this is - that you can refer to this code in the future to see how to create a - listener and attach it to a button. (Listeners are a common idiom - in Java development, particularly for user interfaces.) Here's the empty listener:
    -

    -confirmButton.setOnClickListener(new View.OnClickListener() {
    -
    -    public void onClick(View view) {
    -               
    -    }
    -           
    -});
    -
  10. -
-

Step 10

- -

Fill in the body of the onClick() method of the OnClickListener created in the last step.

- -

This is the code that will be run when the user clicks on the - confirm button. We want this to grab the title and body text from the edit - text fields, and put them into the return Bundle so that they can be passed - back to the Activity that invoked this NoteEdit Activity. If the - operation is an edit rather than a create, we also want to put the - mRowId into the Bundle so that the - Notepadv2 class can save the changes back to the correct - note.

-
    -
  1. - Create a Bundle and put the title and body text into it using the - constants defined in Notepadv2 as keys:
    -
    -Bundle bundle = new Bundle();
    -      
    -bundle.putString(NotesDbAdapter.KEY_TITLE, mTitleText.getText().toString());
    -bundle.putString(NotesDbAdapter.KEY_BODY, mBodyText.getText().toString());
    -if (mRowId != null) {
    -    bundle.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
    -}
    -
  2. -
  3. - Set the result information (the Bundle) in a new Intent and finish the Activity: -
    -Intent mIntent = new Intent();
    -mIntent.putExtras(bundle);
    -setResult(RESULT_OK, mIntent);
    -finish();
    -
      -
    • The Intent is simply our data carrier that carries our Bundle - (with the title, body and mRowId).
    • -
    • The setResult() method is used to set the result - code and return Intent to be passed back to the - Intent caller. In this case everything worked, so we return RESULT_OK for the - result code.
    • -
    • The finish() call is used to signal that the Activity - is done (like a return call). Anything set in the Result will then be - returned to the caller, along with execution control.
    • -
    -
  4. -
-

The full onCreate() method (plus supporting class fields) should - now look like this:

-
-private EditText mTitleText;
-private EditText mBodyText;
-private Long mRowId;
-
-@Override
-protected void onCreate(Bundle savedInstanceState) {
-    super.onCreate(savedInstanceState);
-    setContentView(R.layout.note_edit);
-   
-    mTitleText = (EditText) findViewById(R.id.title);
-    mBodyText = (EditText) findViewById(R.id.body);
-  
-    Button confirmButton = (Button) findViewById(R.id.confirm);
-   
-    mRowId = null;
-    Bundle extras = getIntent().getExtras();
-    if (extras != null) {
-        String title = extras.getString(NotesDbAdapter.KEY_TITLE);
-        String body = extras.getString(NotesDbAdapter.KEY_BODY);
-        mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
-      
-        if (title != null) {
-            mTitleText.setText(title);
-        }
-        if (body != null) {
-            mBodyText.setText(body);
-        }
-    }
-   
-    confirmButton.setOnClickListener(new View.OnClickListener() {
-
-        public void onClick(View view) {
-            Bundle bundle = new Bundle();
-           
-            bundle.putString(NotesDbAdapter.KEY_TITLE, mTitleText.getText().toString());
-            bundle.putString(NotesDbAdapter.KEY_BODY, mBodyText.getText().toString());
-            if (mRowId != null) {
-                bundle.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
-            }
-
-            Intent mIntent = new Intent();
-            mIntent.putExtras(bundle);
-            setResult(RESULT_OK, mIntent);
-            finish();
-        }
-    });
-}
- - - -

Step 11

- - - -

Finally, the new Activity has to be defined in the manifest file:

-

Before the new Activity can be seen by Android, it needs its own - Activity entry in the AndroidManifest.xml file. This is to let - the system know that it is there and can be called. We could also specify - which IntentFilters the activity implements here, but we are going to skip - this for now and just let Android know that the Activity is - defined.

-

There is a Manifest editor included in the Eclipse plugin that makes it much easier - to edit the AndroidManifest file, and we will use this. If you prefer to edit the file directly - or are not using the Eclipse plugin, see the box at the end for information on how to do this - without using the new Manifest editor.

-

    -
  1. Double click on the AndroidManifest.xml file in the package explorer to open it. -
  2. -
  3. Click the Application tab at the bottom of the Manifest editor.
  4. -
  5. Click Add... in the Application Nodes section. -

    If you see a dialog with radiobuttons at the top, select the top radio button: - "Create a new element at the top level, in Application".

  6. -
  7. Make sure "(A) Activity" is selected in the selection pane of the dialog, and click OK.
  8. -
  9. Click on the new "Activity" node, in the Application Nodes section, then - type .NoteEdit into the Name* - field to the right. Press Return/Enter.
  10. -
-

The Android Manifest editor helps you add more complex entries into the AndroidManifest.xml - file, have a look around at some of the other options available (but be careful not to select - them otherwise they will be added to your Manifest). This editor should help you understand - and alter the AndroidManifest.xml file as you move on to more advanced Android applications.

- -

If you prefer to edit this file directly, simply open the - AndroidManifest.xml file and look at the source (use the - AndroidManifest.xml tab in the eclipse editor to see the source code directly). - Then edit the file as follows:
- <activity android:name=".NoteEdit"></activity>

- This should be placed just below the line that reads:
- </activity> for the .Notepadv2 activity.

- -

Step 12

- -

Now Run it!

-

You should now be able to add real notes from -the menu, as well as delete an existing one. Notice that in order to delete, you must -first use the directional controls on the device to highlight the note. -Furthermore, selecting a note title from the list should bring up the note -editor to let you edit it. Press confirm when finished to save the changes -back to the database. - -

Solution and Next Steps

- -

You can see the solution to this exercise in Notepadv2Solution -from the zip file to compare with your own.

-

Now try editing a note, and then hitting the back button on the emulator -instead of the confirm button (the back button is below the menu button). You -will see an error come up. Clearly our application still has some problems. -Worse still, if you did make some changes and hit the back button, when you go -back into the notepad to look at the note you changed, you will find that all -your changes have been lost. In the next exercise we will fix these -problems.

- -

-Once you are ready, move on to Tutorial -Exercise 3 where you will fix the problems with the back button and lost -edits by introducing a proper life cycle into the NoteEdit Activity.

- - diff --git a/docs/html/guide/tutorials/notepad/notepad-ex3.jd b/docs/html/guide/tutorials/notepad/notepad-ex3.jd deleted file mode 100644 index 8737280..0000000 --- a/docs/html/guide/tutorials/notepad/notepad-ex3.jd +++ /dev/null @@ -1,358 +0,0 @@ -page.title=Notepad Exercise 3 -parent.title=Notepad Tutorial -parent.link=index.html -@jd:body - - -

In this exercise, you will use life-cycle event callbacks to store and -retrieve application state data. This exercise demonstrates:

- - -
- [Exercise 1] - [Exercise 2] - - [Exercise 3] - - [Extra Credit] -
- -

Step 1

- -

Import Notepadv3 into Eclipse. If you see an error about -AndroidManifest.xml, or some problems related to an Android zip -file, right click on the project and select Android Tools > -Fix Project Properties from the popup menu. The starting point for this exercise is -exactly where we left off at the end of the Notepadv2.

-

The current application has some problems — hitting the back button when editing -causes a crash, and anything else that happens during editing will cause the -edits to be lost.

-

To fix this, we will move most of the functionality for creating and editing -the note into the NoteEdit class, and introduce a full life cycle for editing -notes.

- -
    -
  1. Remove the code in NoteEdit that parses the title and body - from the extras Bundle. -

    Instead, we are going to use the DBHelper class - to access the notes from the database directly. All we need passed into the - NoteEdit Activity is a mRowId (but only if we are editing, if creating we pass - nothing). Remove these lines:

    -
    -String title = extras.getString(NotesDbAdapter.KEY_TITLE);
    -String body = extras.getString(NotesDbAdapter.KEY_BODY);
    -
  2. -
  3. We will also get rid of the properties that were being passed in - the extras Bundle, which we were using to set the title - and body text edit values in the UI. So delete: -
    -if (title != null) {
    -    mTitleText.setText(title);
    -}
    -if (body != null) {
    -    mBodyText.setText(body);
    -}
    -
  4. -
- -

Step 2

- -

Create a class field for a NotesDbAdapter at the top of the NoteEdit class:

-
    private NotesDbAdapter mDbHelper;
-

Also add an instance of NotesDbAdapter in the - onCreate() method (right below the super.onCreate() call):

-
-    mDbHelper = new NotesDbAdapter(this);
-    mDbHelper.open();
- -

Step 3

- -

In NoteEdit, we need to check the savedInstanceState for the -mRowId, in case the note - editing contains a saved state in the Bundle, which we should recover (this would happen - if our Activity lost focus and then restarted).

-
    -
  1. - Replace the code that currently initializes the mRowId:
    -
    -        mRowId = null;
    -
    -        Bundle extras = getIntent().getExtras();
    -        if (extras != null) {
    -            mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
    -        }
    -        
    - with this: -
    -        mRowId = savedInstanceState != null ? savedInstanceState.getLong(NotesDbAdapter.KEY_ROWID) 
    -                                            : null;
    -        if (mRowId == null) {
    -            Bundle extras = getIntent().getExtras();            
    -            mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID) 
    -                                    : null;
    -        }
    -        
    -
  2. -
  3. - Note the null check for savedInstanceState, and we still need to load up - mRowId from the extras Bundle if it is not - provided by the savedInstanceState. This is a ternary operator shorthand - to safely either use the value or null if it is not present. -
  4. -
- -

Step 4

- -

Next, we need to populate the fields based on the mRowId if we - have it:

-
populateFields();
-

This goes before the confirmButton.setOnClickListener() line. - We'll define this method in a moment.

- -

Step 5

- -

Get rid of the Bundle creation and Bundle value settings from the - onClick() handler method. The Activity no longer needs to - return any extra information to the caller. And because we no longer have - an Intent to return, we'll use the shorter version - of setResult():

-
-public void onClick(View view) {
-    setResult(RESULT_OK);
-    finish();
-}
-

We will take care of storing the updates or new notes in the database - ourselves, using the life-cycle methods.

- -

The whole onCreate() method should now look like this:

-
-super.onCreate(savedInstanceState);
- 
-mDbHelper = new NotesDbAdapter(this);
-mDbHelper.open();
- 
-setContentView(R.layout.note_edit);
- 
-mTitleText = (EditText) findViewById(R.id.title);
-mBodyText = (EditText) findViewById(R.id.body);
- 
-Button confirmButton = (Button) findViewById(R.id.confirm);
- 
-mRowId = savedInstanceState != null ? savedInstanceState.getLong(NotesDbAdapter.KEY_ROWID) 
-                                    : null;
-if (mRowId == null) {
-    Bundle extras = getIntent().getExtras();
-    mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID) 
-                            : null;
-}
- 
-populateFields();
- 
-confirmButton.setOnClickListener(new View.OnClickListener() {
-
-    public void onClick(View view) {
-        setResult(RESULT_OK);
-        finish();
-    }
-     
-});
- -

Step 6

- -

Define the populateFields() method.

-
-private void populateFields() {
-    if (mRowId != null) {
-        Cursor note = mDbHelper.fetchNote(mRowId);
-        startManagingCursor(note);
-        mTitleText.setText(note.getString(
-	            note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
-        mBodyText.setText(note.getString(
-                note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
-    }
-}
-

This method uses the NotesDbAdapter.fetchNote() method to find the right note to -edit, then it calls startManagingCursor() from the Activity class, which -is an Android convenience method provided to take care of the Cursor life-cycle. This will release -and re-create resources as dictated by the Activity life-cycle, so we don't need to worry about -doing that ourselves. After that, we just look up the title and body values from the Cursor -and populate the View elements with them.

- - -

Step 7

- - - -

Still in the NoteEdit class, we now override the methods - onSaveInstanceState(), onPause() and - onResume(). These are our life-cycle methods - (along with onCreate() which we already have).

- -

onSaveInstanceState() is called by Android if the - Activity is being stopped and may be killed before it is - resumed! This means it should store any state necessary to - re-initialize to the same condition when the Activity is restarted. It is - the counterpart to the onCreate() method, and in fact the - savedInstanceState Bundle passed in to onCreate() is the same - Bundle that you construct as outState in the - onSaveInstanceState() method.

- -

onPause() and onResume() are also - complimentary methods. onPause() is always called when the - Activity ends, even if we instigated that (with a finish() call for example). - We will use this to save the current note back to the database. Good - practice is to release any resources that can be released during an - onPause() as well, to take up less resources when in the - passive state. onResume() will call our populateFields() method - to read the note out of the database again and populate the fields.

- -

So, add some space after the populateFields() method - and add the following life-cycle methods:

-
    -
  1. - onSaveInstanceState(): -
    -    @Override
    -    protected void onSaveInstanceState(Bundle outState) {
    -        super.onSaveInstanceState(outState);
    -        outState.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
    -    }
    -
  2. -
  3. - onPause(): -
    -    @Override
    -    protected void onPause() {
    -        super.onPause();
    -        saveState();
    -    }
    -

    We'll define saveState() next.

    -
  4. -
  5. - onResume(): -
    -    @Override
    -    protected void onResume() {
    -        super.onResume();
    -        populateFields();
    -    }
    -
  6. -
- - -

Step 8

- -

Define the saveState() method to put the data out to the -database.

-
-     private void saveState() {
-        String title = mTitleText.getText().toString();
-        String body = mBodyText.getText().toString();
-
-        if (mRowId == null) {
-            long id = mDbHelper.createNote(title, body);
-            if (id > 0) {
-                mRowId = id;
-            }
-        } else {
-            mDbHelper.updateNote(mRowId, title, body);
-        }
-    }
-

Note that we capture the return value from createNote() and if a valid row ID is - returned, we store it in the mRowId field so that we can update the note in future - rather than create a new one (which otherwise might happen if the life-cycle events are - triggered).

- - -

Step 9

- -

Now pull out the previous handling code from the - onActivityResult() method in the Notepadv3 - class.

-

All of the note retrieval and updating now happens within the - NoteEdit life cycle, so all the onActivityResult() - method needs to do is update its view of the data, no other work is - necessary. The resulting method should look like this:

-
-@Override
-protected void onActivityResult(int requestCode, int resultCode, 
-                                Intent intent) {
-    super.onActivityResult(requestCode, resultCode, intent);
-    fillData();
-}
- -

Because the other class now does the work, all this has to do is refresh - the data.

- -

Step 10

- -

Also remove the lines which set the title and body from the - onListItemClick() method (again they are no longer needed, - only the mRowId is):

-
-    Cursor c = mNotesCursor;
-    c.moveToPosition(position);
-
-and also remove: -
-
-    i.putExtra(NotesDbAdapter.KEY_TITLE, c.getString(
-                    c.getColumnIndex(NotesDbAdapter.KEY_TITLE)));
-    i.putExtra(NotesDbAdapter.KEY_BODY, c.getString(
-                    c.getColumnIndex(NotesDbAdapter.KEY_BODY)));
-
-so that all that should be left in that method is: -
-
-    super.onListItemClick(l, v, position, id);
-    Intent i = new Intent(this, NoteEdit.class);
-    i.putExtra(NotesDbAdapter.KEY_ROWID, id);
-    startActivityForResult(i, ACTIVITY_EDIT);
- -

You can also now remove the mNotesCursor field from the class, and set it back to using - a local variable in the fillData() method: -

-    Cursor notesCursor = mDbHelper.fetchAllNotes();

-

Note that the m in mNotesCursor denotes a member field, so when we - make notesCursor a local variable, we drop the m. Remember to rename the - other occurrences of mNotesCursor in your fillData() method. - -

-Run it! (use Run As -> Android Application on the project right -click menu again)

- -

Solution and Next Steps

- -

You can see the solution to this exercise in Notepadv3Solution -from -the zip file to compare with your own.

-

-When you are ready, move on to the Tutorial -Extra Credit exercise, where you can use the Eclipse debugger to -examine the life-cycle events as they happen.

diff --git a/docs/html/guide/tutorials/notepad/notepad-extra-credit.jd b/docs/html/guide/tutorials/notepad/notepad-extra-credit.jd deleted file mode 100644 index 0d59b56..0000000 --- a/docs/html/guide/tutorials/notepad/notepad-extra-credit.jd +++ /dev/null @@ -1,70 +0,0 @@ -page.title=Notepad Extra Credit -parent.title=Notepad Tutorial -parent.link=index.html -@jd:body - - -

In this exercise, you will use the debugger to look at the work you did -in Exercise 3. This exercise demonstrates:

- - -
- - [Exercise 1] - [Exercise 2] - [Exercise 3] - - [Extra Credit] - -
- -

Step 1

- -

Using the working Notepadv3, put breakpoints in the code at the - beginning of the onCreate(), onPause(), - onSaveInstanceState() and onResume() methods in the - NoteEdit class (if you are not familiar with Eclipse, just - right click in the narrow grey border on the left of the edit window at the - line you want a breakpoint, and select Toggle Breakpoint, you -should see a blue dot appear).

- -

Step 2

- -

Now start the notepad demo in debug mode:

- -
    -
  1. - Right click on the Notepadv3 project and from the Debug menu - select Debug As -> Android Application.
  2. -
  3. - The Android emulator should say "waiting for debugger to connect" - briefly and then run the application.
  4. -
  5. - If it gets stuck on the waiting... screen, quit the emulator and Eclipse, - from the command line do an adb kill-server, and then restart -Eclipse and try again.
- -

Step 3

- -

When you edit or create a new note you should see the breakpoints getting - hit and the execution stopping.

- -

Step 4

- -

Hit the Resume button to let execution continue (yellow rectangle with a -green triangle to its right in the Eclipse toolbars near the top).

- -

Step 5

- -

Experiment a bit with the confirm and back buttons, and try pressing Home and - making other mode changes. Watch what life-cycle events are generated and -when.

- -

The Android Eclipse plugin not only offers excellent debugging support for -your application development, but also superb profiling support. You can also -try using Traceview to profile your application. If your application is running too slow, this can help you -find the bottlenecks and fix them.

- diff --git a/docs/html/guide/tutorials/notepad/notepad-index.jd b/docs/html/guide/tutorials/notepad/notepad-index.jd deleted file mode 100644 index 151c50d..0000000 --- a/docs/html/guide/tutorials/notepad/notepad-index.jd +++ /dev/null @@ -1,143 +0,0 @@ -page.title=Notepad Tutorial -@jd:body - - -

The tutorial in this section gives you a "hands-on" introduction -to the Android framework and the tools you use to build applications on it. -Starting from a preconfigured project file, it guides you through the process of -developing a simple notepad application and provides concrete examples of how to -set up the project, develop the application logic and user interface, and then -compile and run the application.

- -

The tutorial presents the notepad application development as a set of -exercises (see below), each consisting of several steps. You can follow along -with the steps in each exercise and gradually build up and refine your -application. The exercises explain each step in detail and provide all the -sample code you need to complete the application.

- -

When you are finished with the tutorial, you will have created a functioning -Android application and learned in depth about many of the most important -concepts in Android development. If you want to add more complex features to -your application, you can examine the code in an alternative implementation -of a notepad application, in the -Sample Code documentation.

- - - -

Who Should Use this Tutorial

- -

This tutorial is designed for experienced developers, especially those with -knowledge of the Java programming language. If you haven't written Java -applications before, you can still use the tutorial, but you might need to work -at a slower pace.

- -

The tutorial assumes that you have some familiarity with the basic Android -application concepts and terminology. If you aren't yet familiar with those, you -should read Overview of an Android -Application before continuing.

- -

Also note that this tutorial uses -the Eclipse development environment, with the Android plugin installed. If you -are not using Eclipse, you can follow the exercises and build the application, -but you will need to determine how to accomplish the Eclipse-specific -steps in your environment.

- - -

Preparing for the Exercises

- -

This tutorial builds on the information provided in the Installing the SDK and Hello Android -documents, which explain in detail how to set up your development environment -for building Android applications. Before you start this tutorial, you should -read both these documents, have the SDK installed, and your work environment set up.

- -

To prepare for this lesson:

- -
    -
  1. Download the project - exercises archive (.zip)
  2. -
  3. Unpack the archive file to a suitable location on your machine
  4. -
  5. Open the NotepadCodeLab folder
  6. -
- -

Inside the NotepadCodeLab folder, you should see six project -files: Notepadv1, - Notepadv2, Notepadv3, - Notepadv1Solution, Notepadv2Solution - and Notepadv3Solution. The Notepadv# projects are -the starting points for each of the exercises, while the -Notepadv#Solution projects are the exercise - solutions. If you are having trouble with a particular exercise, you - can compare your current work against the exercise solution.

- - -

Exercises

- -

The table below lists the tutorial exercises and describes the development -areas that each covers. Each exercise assumes that you have completed any -previous exercises.

- - - - - - - - - - - - - - - - - - -
Exercise -1Start here. Construct a simple notes list that lets the user add new notes but not -edit them. Demonstrates the basics of ListActivity and creating -and handling - menu options. Uses a SQLite database to store the notes.
Exercise 2Add a second Activity to the -application. Demonstrates constructing a -new Activity, adding it to the Android manifest, passing data between the -activities, and using more advanced screen layout. Also shows how to -invoke another Activity to return a result, using -startActivityForResult().
Exercise 3Add handling of life-cycle events to -the application, to let it -maintain application state across the life cycle.
Extra -CreditDemonstrates how to use the Eclipse -debugger and how you can use it to -view life-cycle events as they are generated. This section is optional but -highly recommended.
- - - -

Other Resources and Further Learning

- diff --git a/docs/html/guide/tutorials/views/hello-autocomplete.jd b/docs/html/guide/tutorials/views/hello-autocomplete.jd deleted file mode 100644 index de3ba29..0000000 --- a/docs/html/guide/tutorials/views/hello-autocomplete.jd +++ /dev/null @@ -1,116 +0,0 @@ -page.title=Hello, AutoCompleteTextView -parent.title=Hello, Views -parent.link=index.html -@jd:body - -

{@link android.widget.AutoCompleteTextView} is an implementation of the EditText widget that will provide -auto-complete suggestions as the user types. The suggestions are extracted from a collection of strings.

- - -
    -
  1. Start a new project/Activity called HelloAutoComplete.
  2. -
  3. Open the layout file. - Make it like so: -
    -<?xml version="1.0" encoding="utf-8"?>
    -<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    -    android:orientation="horizontal"
    -    android:layout_width="fill_parent" 
    -    android:layout_height="wrap_content">
    -
    -    <TextView
    -        android:layout_width="wrap_content"
    -        android:layout_height="wrap_content"
    -        android:text="Country" />
    -
    -    <AutoCompleteTextView android:id="@+id/edit"
    -        android:layout_width="fill_parent"
    -        android:layout_height="wrap_content"/>
    -
    -</LinearLayout>
    -
    -
  4. - -
  5. Open HelloAutoComplete.java and insert the following as the onCreate method: -
    -@Override
    -protected void onCreate(Bundle savedInstanceState) {
    -    super.onCreate(savedInstanceState);
    -    setContentView(R.layout.main);
    -
    -    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.edit);
    -    ArrayAdapter adapter = new ArrayAdapter(this,
    -            android.R.layout.simple_dropdown_item_1line, R.array.planets);
    -    textView.setAdapter(adapter);
    -}
    -
    -

    Here, we create an AutoComplteteTextView from our layout. We then - create an {@link android.widget.ArrayAdapter} that binds a simple_dropdown_item_1line - layout item to each entry in the COUNTRIES array (which we'll add next). - The last part sets the ArrayAdapter to associate with our AutoCompleteTextView.

    -
  6. - -
  7. After the onCreate() method, add the String array: -
    -static final String[] COUNTRIES = new String[] {
    -  "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
    -  "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
    -  "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
    -  "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",
    -  "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia",
    -  "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory",
    -  "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
    -  "Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde",
    -  "Cayman Islands", "Central African Republic", "Chad", "Chile", "China",
    -  "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo",
    -  "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic",
    -  "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic",
    -  "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea",
    -  "Estonia", "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji", "Finland",
    -  "Former Yugoslav Republic of Macedonia", "France", "French Guiana", "French Polynesia",
    -  "French Southern Territories", "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar",
    -  "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau",
    -  "Guyana", "Haiti", "Heard Island and McDonald Islands", "Honduras", "Hong Kong", "Hungary",
    -  "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica",
    -  "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos",
    -  "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg",
    -  "Macau", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands",
    -  "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova",
    -  "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia",
    -  "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand",
    -  "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Marianas",
    -  "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru",
    -  "Philippines", "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar",
    -  "Reunion", "Romania", "Russia", "Rwanda", "Sqo Tome and Principe", "Saint Helena",
    -  "Saint Kitts and Nevis", "Saint Lucia", "Saint Pierre and Miquelon",
    -  "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Saudi Arabia", "Senegal",
    -  "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
    -  "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "South Korea",
    -  "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden",
    -  "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Bahamas",
    -  "The Gambia", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey",
    -  "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda",
    -  "Ukraine", "United Arab Emirates", "United Kingdom",
    -  "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan",
    -  "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Wallis and Futuna", "Western Sahara",
    -  "Yemen", "Yugoslavia", "Zambia", "Zimbabwe"
    -};
    -
    -

    This is the list of suggestions that will be offered as the user types into the - AutoCompleteTextView.

    -
  8. - -
  9. Now run it.
  10. -
-

As you type, you should see something like this:

- - - -

References

- - - diff --git a/docs/html/guide/tutorials/views/hello-datepicker.jd b/docs/html/guide/tutorials/views/hello-datepicker.jd deleted file mode 100644 index fcd43f3..0000000 --- a/docs/html/guide/tutorials/views/hello-datepicker.jd +++ /dev/null @@ -1,151 +0,0 @@ -page.title=Hello, DatePicker -parent.title=Hello, Views -parent.link=index.html -@jd:body - -

A {@link android.widget.DatePicker} is a widget that allows the user to select a month, day and year.

- - -
    -
  1. Start a new project/Activity called HelloDatePicker.
  2. -
  3. Open the layout file and make it like so: -
    -<?xml version="1.0" encoding="utf-8"?>
    -<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    -    android:layout_width="wrap_content"
    -    android:layout_height="wrap_content"
    -    android:orientation="vertical">
    -
    -    <TextView android:id="@+id/dateDisplay"
    -            android:layout_width="wrap_content"
    -            android:layout_height="wrap_content"
    -            android:text=""/>
    -
    -    <Button android:id="@+id/pickDate"
    -            android:layout_width="wrap_content"
    -            android:layout_height="wrap_content"
    -            android:text="Change the date"/>
    -
    -</LinearLayout>
    -
    -

    For the layout, we're using a vertical LinearLayout, with a {@link android.widget.TextView} that - will display the date and a {@link android.widget.Button} that will initiate the DatePicker dialog. - With this layout, the TextView will sit above the Button. - The text value in the TextView is set empty, as it will be filled - with the current date when our Activity runs.

    -
  4. - -
  5. Open HelloDatePicker.java. Insert the following to the HelloDatePicker class: -
    -    private TextView mDateDisplay;
    -    private Button mPickDate;
    -
    -    private int mYear;
    -    private int mMonth;
    -    private int mDay;
    -
    -    static final int DATE_DIALOG_ID = 0;
    -
    -    @Override
    -    protected void onCreate(Bundle savedInstanceState) {
    -        super.onCreate(savedInstanceState);
    -        setContentView(R.layout.main);
    -
    -        // capture our View elements
    -        mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
    -        mPickDate = (Button) findViewById(R.id.pickDate);
    -
    -        // add a click listener to the button
    -        mPickDate.setOnClickListener(new View.OnClickListener() {
    -            public void onClick(View v) {
    -                showDialog(DATE_DIALOG_ID);
    -            }
    -        });
    -
    -        // get the current date
    -        final Calendar c = Calendar.getInstance();
    -        mYear = c.get(Calendar.YEAR);
    -        mMonth = c.get(Calendar.MONTH);
    -        mDay = c.get(Calendar.DAY_OF_MONTH);
    -
    -        // display the current date
    -        updateDisplay();
    -    }
    -
    -

    Tip: Press Ctrl(or Cmd) + Shift + O to import all needed packages.

    -

    We start by instantiating variables for our Views and date fields. - The DATE_DIALOG_ID is a static integer that uniquely identifies the Dialog. In the - onCreate() method, we get prepared by setting the layout and capturing the View elements. - Then we create an on-click listener for the Button, so that when it is clicked it will - show our DatePicker dialog. The showDialog() method will pop-up the date picker dialog - by calling the onCreateDialog() callback method - (which we'll define in the next section). We then create an - instance of {@link java.util.Calendar} and get the current year, month and day. Finally, we call - updateDisplay()—our own method (defined later) that will fill the TextView.

    -
  6. - -
  7. After the onCreate() method, add the onCreateDialog() callback method -(which is called by showDialog()) -
    -@Override
    -protected Dialog onCreateDialog(int id) {
    -    switch (id) {
    -    case DATE_DIALOG_ID:
    -        return new DatePickerDialog(this,
    -                    mDateSetListener,
    -                    mYear, mMonth, mDay);
    -    }
    -    return null;
    -}
    -
    -

    This method is passed the identifier we gave showDialog() and initializes - the DatePicker to the date we retrieved from our Calendar instance.

    -
  8. - -
  9. Following that, add the updateDisplay() method: -
    -    // updates the date we display in the TextView
    -    private void updateDisplay() {
    -        mDateDisplay.setText(
    -            new StringBuilder()
    -                    // Month is 0 based so add 1
    -                    .append(mMonth + 1).append("-")
    -                    .append(mDay).append("-")
    -                    .append(mYear).append(" "));
    -    }
    -
    -

    This uses the member date values to write the date to our TextView.

    -
  10. -
  11. Finally, add a listener that will be called when the user sets a new date: -
    -    // the callback received when the user "sets" the date in the dialog
    -    private DatePickerDialog.OnDateSetListener mDateSetListener =
    -            new DatePickerDialog.OnDateSetListener() {
    -
    -                public void onDateSet(DatePicker view, int year, 
    -                                      int monthOfYear, int dayOfMonth) {
    -                    mYear = year;
    -                    mMonth = monthOfYear;
    -                    mDay = dayOfMonth;
    -                    updateDisplay();
    -                }
    -            };
    -
    -

    This OnDateSetListener method listens for when the user is done setting the date - (clicks the "Set" button). At that time, this fires and we update our member fields with - the new date defined by the user and update our TextView by calling updateDisplay().

    -
  12. - -
  13. Now run it.
  14. -
-

When you press the "Change the date" button, you should see the following:

- - -

References

- - diff --git a/docs/html/guide/tutorials/views/hello-formstuff.jd b/docs/html/guide/tutorials/views/hello-formstuff.jd deleted file mode 100644 index f858ce3..0000000 --- a/docs/html/guide/tutorials/views/hello-formstuff.jd +++ /dev/null @@ -1,262 +0,0 @@ -page.title=Hello, Form Stuff -parent.title=Hello, Views -parent.link=index.html -@jd:body - -

This page introduces a variety of widgets, like image buttons, -text fields, checkboxes and radio buttons.

- - -
    -
  1. Start a new project/Activity called HelloFormStuff.
  2. -
  3. Your layout file should have a basic LinearLayout: -
    -<?xml version="1.0" encoding="utf-8"?>
    -<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    -    android:orientation="vertical"
    -    android:layout_width="fill_parent"
    -    android:layout_height="fill_parent" >
    -    	
    -</LinearLayout>
    -
    -

    For each widget you want to add, just put the respective View inside here.

    -
  4. -
-

Tip: As you add new Android code, press Ctrl(or Cmd) + Shift + O -to import all needed packages.

- - -

ImageButton

-

A button with a custom image on it. -We'll make it display a message when pressed.

-
    -
  1. - Drag the Android image on the right (or your own image) into the - res/drawables/ directory of your project. - We'll use this for the button.
  2. -
  3. Open the layout file and, inside the LinearLayout, add the {@link android.widget.ImageButton} element: -
    -<ImageButton
    -    android:id="@+id/android_button"
    -    android:layout_width="100dip"
    -    android:layout_height="wrap_content"
    -    android:src="@drawable/android" />	
    -
    -

    The source of the button - is from the res/drawables/ directory, where we've placed the android.png.

    -

    Tip: You can also reference some of the many built-in - images from the Android {@link android.R.drawable} resources, - like ic_media_play, for a "play" button image. To do so, change the source - attribute to android:src="@android:drawable/ic_media_play".

    -
  4. -
  5. To make the button to actually do something, add the following -code at the end of the onCreate() method: -
    -ImageButton button = (ImageButton) findViewById(R.id.android_button);
    -button.setOnClickListener(new OnClickListener() {
    -    public void onClick(View v) {
    -        // Perform action on clicks
    -        Toast.makeText(HelloFormStuff.this, "Beep Bop", Toast.LENGTH_SHORT).show();
    -    }
    -});
    -
    -

    This captures our ImageButton from the layout, then adds an on-click listener to it. -The {@link android.view.View.OnClickListener} must define the onClick() method, which -defines the action to be made when the button is clicked. Here, we show a -{@link android.widget.Toast} message when clicked.

    -
  6. -
  7. Run it.
  8. -
- - -

EditText

-

A text field for user input. We'll make it display the text entered so far when the "Enter" key is pressed.

- -
    -
  1. Open the layout file and, inside the LinearLayout, add the {@link android.widget.EditText} element: -
    -<EditText
    -    android:id="@+id/edittext"
    -    android:layout_width="fill_parent"
    -    android:layout_height="wrap_content"/>
    -
    -
  2. -
  3. To do something with the text that the user enters, add the following code -to the end of the onCreate() method: -
    -EditText edittext = (EditText) findViewById(R.id.edittext);
    -edittext.setOnKeyListener(new OnKeyListener() {
    -    public boolean onKey(View v, int keyCode, KeyEvent event) {
    -        if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
    -          // Perform action on key press
    -          Toast.makeText(HelloImageButton.this, edittext.getText(), Toast.LENGTH_SHORT).show();
    -          return true;
    -        }
    -        return false;
    -    }
    -});
    -
    -

    This captures our EditText element from the layout, then adds an on-key listener to it. -The {@link android.view.View.OnKeyListener} must define the onKey() method, which -defines the action to be made when a key is pressed. In this case, we want to listen for the -Enter key (when pressed down), then pop up a {@link android.widget.Toast} message with the -text from the EditText field. Be sure to return true after the event is handled, -so that the event doesn't bubble-up and get handled by the View (which would result in a -carriage return in the text field).

    -
  4. Run it.
  5. -
- - -

CheckBox

-

A checkbox for selecting items. We'll make it display the the current state when pressed.

- -
    -
  1. Open the layout file and, inside the LinearLayout, add the {@link android.widget.CheckBox} element: -
    -<CheckBox android:id="@+id/checkbox"
    -    android:layout_width="wrap_content"
    -    android:layout_height="wrap_content"
    -    android:text="check it out" />
    -
    -
  2. -
  3. To do something when the state is changed, add the following code -to the end of the onCreate() method: -
    -final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox);
    -checkbox.setOnClickListener(new OnClickListener() {
    -    public void onClick(View v) {
    -        // Perform action on clicks
    -        if (checkbox.isChecked()) {
    -            Toast.makeText(HelloImageButton.this, "Selected", Toast.LENGTH_SHORT).show();
    -        } else {
    -            Toast.makeText(HelloImageButton.this, "Not selected", Toast.LENGTH_SHORT).show();
    -        }
    -    }
    -});
    -
    -

    This captures our CheckBox element from the layout, then adds an on-click listener to it. -The {@link android.view.View.OnClickListener} must define the onClick() method, which -defines the action to be made when the checkbox is clicked. Here, we query the current state of the -checkbox, then pop up a {@link android.widget.Toast} message that displays the current state. -Notice that the CheckBox handles its own state change between checked and un-checked, so we just -ask which it currently is.

    -
  4. Run it.
  5. -
-

Tip: If you find that you need to change the state -in another way (such as when loading a saved {@link android.preference.CheckBoxPreference}), -use setChecked(true) or toggle().

- - -

RadioButton

-

Two mutually-exclusive radio buttons—enabling one disables the other. -When each is pressed, we'll pop up a message.

- -
    -
  1. Open the layout file and, inside the LinearLayout, add two {@link android.widget.RadioButton}s, -inside a {@link android.widget.RadioGroup}: -
    -<RadioGroup
    -  android:layout_width="fill_parent"
    -  android:layout_height="wrap_content"
    -  android:orientation="vertical">
    -  
    -  <RadioButton android:id="@+id/radio_red"
    -      android:layout_width="wrap_content"
    -      android:layout_height="wrap_content"
    -      android:text="Red" />
    -  
    -  <RadioButton android:id="@+id/radio_blue"
    -      android:layout_width="wrap_content"
    -      android:layout_height="wrap_content"
    -      android:text="Blue" />
    -  
    -</RadioGroup>
    -
    -
  2. -
  3. To do something when each is selected, we'll need an OnClickListener. Unlike the other -listeners we've created, instead of creating this one as an anonymous inner class, -we'll create it as a new object. This way, we can re-use the OnClickLIstener for -both RadioButtons. So, add the following code in the HelloFormStuff Activity -(outside the onCreate() method): -
    -OnClickListener radio_listener = new OnClickListener() {
    -    public void onClick(View v) {
    -        // Perform action on clicks
    -        RadioButton rb = (RadioButton) v;
    -        Toast.makeText(HelloImageButton.this, rb.getText(), Toast.LENGTH_SHORT).show();
    -    }
    -};
    -
    -

    Our onClick() method will be handed the View clicked, so the first thing to do -is cast it into a RadioButton. Then we pop up a -{@link android.widget.Toast} message that displays the selection.

    -
  4. Now, at the bottom of the onCreate() method, add the following: -
    -  final RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red);
    -  final RadioButton radio_blue = (RadioButton) findViewById(R.id.radio_blue);
    -  radio_red.setOnClickListener(radio_listener);
    -  radio_blue.setOnClickListener(radio_listener);
    -
    -

    This captures each of the RadioButtons from our layout and adds the newly-created -OnClickListener to each.

    -
  5. Run it.
  6. -
-

Tip: If you find that you need to change the state of a -RadioButton in another way (such as when loading a saved {@link android.preference.CheckBoxPreference}), -use setChecked(true) or toggle().

- - -

ToggleButton

-

A button used specifically for toggling something on and off.

- -
    -
  1. Open the layout file and, inside the LinearLayout, add the {@link android.widget.ToggleButton} element: -
    -<ToggleButton android:id="@+id/togglebutton"
    -    android:layout_width="wrap_content"
    -    android:layout_height="wrap_content" />
    -
    -
  2. -
  3. To do something when the state is changed, add the following code -to the end of the onCreate() method: -
    -final ToggleButton togglebutton = (ToggleButton) findViewById(R.id.togglebutton);
    -togglebutton.setOnClickListener(new OnClickListener() {
    -    public void onClick(View v) {
    -        // Perform action on clicks
    -        if (togglebutton.isChecked()) {
    -            Toast.makeText(HelloImageButton.this, "ON", Toast.LENGTH_SHORT).show();
    -        } else {
    -            Toast.makeText(HelloImageButton.this, "OFF", Toast.LENGTH_SHORT).show();
    -        }
    -    }
    -});
    -
    -

    This captures our ToggleButton element from the layout, then adds an on-click listener to it. -The {@link android.view.View.OnClickListener} must define the onClick() method, which -defines the action to be made when the button is clicked. Here, we query the current state of the -ToggleButton, then pop up a {@link android.widget.Toast} message that displays the current state. -Notice that the ToggleButton handles its own state change between checked and un-checked, so we just -ask which it is.

    -
  4. Run it.
  5. -
- -

Tip: By default, the text on the button is "ON" and "OFF", but -you can change each of these with setTextOn(CharSequence) and -setTextOff(CharSequence). And, if you find that you need to change the state -in another way (such as when loading a saved {@link android.preference.CheckBoxPreference}), -use setChecked(true) or toggle().

- - -

If you've added all the form items above, your application should look something like this:

- - -

References

- - diff --git a/docs/html/guide/tutorials/views/hello-gallery.jd b/docs/html/guide/tutorials/views/hello-gallery.jd deleted file mode 100644 index 084f912..0000000 --- a/docs/html/guide/tutorials/views/hello-gallery.jd +++ /dev/null @@ -1,135 +0,0 @@ -page.title=Hello, Gallery -parent.title=Hello, Views -parent.link=index.html -@jd:body - -

A {@link android.widget.Gallery} is a View commonly used to display items in a horizontally scrolling list -that locks the current selection at the center. When one is selected, we'll show a message.

- - -
    -
  1. Start a new project/Activity called HelloGallery.
  2. -
  3. Add some images to your res/drawable/ directory.
  4. -
  5. Open the layout file and make it like so: -
    -<?xml version="1.0" encoding="utf-8"?>
    -<Gallery xmlns:android="http://schemas.android.com/apk/res/android" 
    -    android:id="@+id/gallery"
    -    android:layout_width="fill_parent"
    -    android:layout_height="wrap_content"
    -/>
    -
    -
  6. - - -
  7. Open the HelloGallery.java file. Insert the following for the onCreate() method: -
    -@Override
    -public void onCreate(Bundle savedInstanceState) {
    -    super.onCreate(savedInstanceState);
    -    setContentView(R.layout.main);
    -
    -    Gallery g = (Gallery) findViewById(R.id.gallery);
    -    g.setAdapter(new ImageAdapter(this));
    -
    -    g.setOnItemClickListener(new OnItemClickListener() {
    -        public void onItemClick(AdapterView parent, View v, int position, long id) {
    -            Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show();
    -        }
    -    });
    -}
    -
    -

    We start as usual: set the layout and capture the View we want (our Gallery). -We then set an Adapter, called ImageAdapter for the Gallery—this is a new class that -we'll create next. Then we create an item click listener for the Gallery. This is like a normal -on-click listener (which you might be familiar with for buttons), but it listens to each item -that we've added to the Gallery. The onItemClick() callback method -receives the AdapterView where the click occurred, the specific View that received the click, the -position of the View clicked (zero-based), and the row id of the item clicked (if applicable). All -that we care about is the position, so that we can pop up a {@link android.widget.Toast} message that -tells us the index position of the item clicked. We do this with Toast.makeText().show(). -

    -
  8. - -
  9. After the onCreate() method, add the ImageAdapter class: -
    -public class ImageAdapter extends BaseAdapter {
    -    int mGalleryItemBackground;
    -    private Context mContext;
    -
    -    private Integer[] mImageIds = {
    -            R.drawable.sample_1,
    -            R.drawable.sample_2,
    -            R.drawable.sample_3,
    -            R.drawable.sample_4,
    -            R.drawable.sample_5,
    -            R.drawable.sample_6,
    -            R.drawable.sample_7
    -    };
    -
    -    public ImageAdapter(Context c) {
    -        mContext = c;
    -        TypedArray a = obtainStyledAttributes(android.R.styleable.Theme);
    -        mGalleryItemBackground = a.getResourceId(
    -                android.R.styleable.Theme_galleryItemBackground, 0);
    -        a.recycle();
    -    }
    -
    -    public int getCount() {
    -        return mImageIds.length;
    -    }
    -
    -    public Object getItem(int position) {
    -        return position;
    -    }
    -
    -    public long getItemId(int position) {
    -        return position;
    -    }
    -
    -    public View getView(int position, View convertView, ViewGroup parent) {
    -        ImageView i = new ImageView(mContext);
    -
    -        i.setImageResource(mImageIds[position]);
    -        i.setLayoutParams(new Gallery.LayoutParams(150, 100));
    -        i.setScaleType(ImageView.ScaleType.FIT_XY);
    -        i.setBackgroundResource(mGalleryItemBackground);
    -
    -        return i;
    -    }
    -}
    -
    -

    First, there are a few member variables, including an array of IDs that reference -the images we placed in our drawable resources directory.

    -

    Next is the constructor, where we define the member Context. The rest of the constructor -sets up a reference for our Gallery them, which adds the nice framing for each Gallery item. -Once we have our mGalleryItemBackground, it's important to recycle the -StyledAttribute for later re-use.

    -

    The next three methods are required for basic member queries. -But then we have the getView() method, which is called -for each item read by our ImageAdapter, when the Gallery is being built. Here, we -use our member Context to create a new {@link android.widget.ImageView}. We then define -the image resource with the current position of the Gallery items (corresponding to our -array of drawables), set the dimensions for the ImageView, -set the image scaling to fit the ImageView dimensions, then finally set the -background theme for the ImageView.

    - -

    See {@link android.widget.ImageView.ScaleType} -for other image scaling options, in case you want to avoid stretching images that don't -exactly match the ImageView dimensions.

    - -
  10. Now run it.
  11. -
-

You should see something like this:

- - - -

References

- - - diff --git a/docs/html/guide/tutorials/views/hello-gridview.jd b/docs/html/guide/tutorials/views/hello-gridview.jd deleted file mode 100644 index 623a03d..0000000 --- a/docs/html/guide/tutorials/views/hello-gridview.jd +++ /dev/null @@ -1,128 +0,0 @@ -page.title=Hello, GridView -parent.title=Hello, Views -parent.link=index.html -@jd:body - -

A {@link android.widget.GridView} displays items in a two-dimensional, scrolling grid. The items -are acquired from a {@link android.widget.ListAdapter}.

- - -
    -
  1. Start a new project/Activity called HelloGridView.
  2. -
  3. Find some photos you'd like to use, or copy some from the SDK samples res/drawable/ - folder of your project.
  4. -
  5. Open the layout and make it like so: -
    -<?xml version="1.0" encoding="utf-8"?>
    -<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    -    android:id="@+id/gridview"
    -    android:layout_width="fill_parent" 
    -    android:layout_height="fill_parent"
    -    android:numColumns="auto_fit"
    -    android:verticalSpacing="10dp"
    -    android:horizontalSpacing="10dp"
    -    android:columnWidth="90dp"
    -    android:stretchMode="columnWidth"
    -    android:gravity="center"
    -/>
    -
  6. -
  7. Open the HelloGridView Java file. Insert the following for the onCreate() method: -
    -public void onCreate(Bundle savedInstanceState) {
    -    super.onCreate(savedInstanceState);
    -    setContentView(R.layout.main);
    -
    -    GridView gridview = (GridView) findViewById(R.id.gridview);
    -    gridview.setAdapter(new ImageAdapter(this));
    -}
    -
    -

    Here, we get a handle on our GridView, from the layout, and give it an Adapter. - We're actually going to create our own Adapter called ImageAdapter.

    -
  8. -
  9. Create a new class (nested or otherwise), called ImageAdapter, which extends {@link android.widget.BaseAdapter}: -
    -public class ImageAdapter extends BaseAdapter {
    -    private Context mContext;
    -
    -    public ImageAdapter(Context c) {
    -        mContext = c;
    -    }
    -
    -    public int getCount() {
    -        return mThumbIds.length;
    -    }
    -
    -    public Object getItem(int position) {
    -        return null;
    -    }
    -
    -    public long getItemId(int position) {
    -        return 0;
    -    }
    -
    -    // create a new ImageView for each item referenced by the Adapter
    -    public View getView(int position, View convertView, ViewGroup parent) {
    -        ImageView imageView;
    -        if (convertView == null) {  // if it's not recycled, initialize some attributes
    -            imageView = new ImageView(mContext);
    -            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
    -            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    -            imageView.setPadding(8, 8, 8, 8);
    -        } else {
    -            imageView = (ImageView) convertView;
    -        }
    -
    -        imageView.setImageResource(mThumbIds[position]);
    -        return imageView;
    -    }
    -
    -    // references to our images
    -    private Integer[] mThumbIds = {
    -            R.drawable.sample_2, R.drawable.sample_3,
    -            R.drawable.sample_4, R.drawable.sample_5,
    -            R.drawable.sample_6, R.drawable.sample_7,
    -            R.drawable.sample_0, R.drawable.sample_1,
    -            R.drawable.sample_2, R.drawable.sample_3,
    -            R.drawable.sample_4, R.drawable.sample_5,
    -            R.drawable.sample_6, R.drawable.sample_7,
    -            R.drawable.sample_0, R.drawable.sample_1,
    -            R.drawable.sample_2, R.drawable.sample_3,
    -            R.drawable.sample_4, R.drawable.sample_5,
    -            R.drawable.sample_6, R.drawable.sample_7
    -    };
    -}
    -
    -

    First we take care of some required methods inherited from BaseAdapter. - The constructor and getCount() are self-explanitory. Normally, getItem() - should return the actual object at the specified position in our Adapter, but for this Hello World, - we're not going to bother. Likewise, getItemId() should return the row id of - the item, but right now we don't care.

    -

    However, getView() is the method we care about. This one creates a new View for each image that we - put in our ImageAdapter. So we're going to create an ImageView each time. When this is called, we're - going to receive a View, which is likely a recycled View object (at least after the first call), so we - check for this—if it's null, we initialize the ImageView and setup all the properties we want. - The LayoutParams() initialization sets the height and width of the View—this ensures - that no matter the drawable size, each image is resized and cropped to fit in the ImageView (if necessary). - With setScaleType(), we say that images should be cropped toward the center (if necessary). - And finally, we set the padding within the ImageView. (Note that, if the images have various aspect-ratios, - as they do in this demo, then less padding will cause for more cropping of the image, if it does not match - the dimensions given to the ImageView.) At the end of getView() we set the image resource and - return the ImageView.

    -

    All that's left is our array or drawable resources at the bottom.

    -
  10. -
  11. Run it.
  12. -
-

Your grid layout should look something like this:

- - -

Try experimenting with the behaviors of the GridView and ImageView by adjusting their properties. For example, - instead of setting the ImageView LayoutParams, you can try using - {@link android.widget.ImageView#setAdjustViewBounds(boolean)}.

- -

References

- - diff --git a/docs/html/guide/tutorials/views/hello-linearlayout.jd b/docs/html/guide/tutorials/views/hello-linearlayout.jd deleted file mode 100644 index 0e8947c..0000000 --- a/docs/html/guide/tutorials/views/hello-linearlayout.jd +++ /dev/null @@ -1,130 +0,0 @@ -page.title=Hello, LinearLayout -parent.title=Hello, Views -parent.link=index.html -@jd:body - -

A {@link android.widget.LinearLayout} is a GroupView that will lay child View elements -vertically or horizontally.

- - -
    -
  1. Start a new project/Activity called HelloLinearLayout.
  2. -
  3. Open the layout file. - Make it like so: -
    -<?xml version="1.0" encoding="utf-8"?>
    -<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    -    android:orientation="vertical"
    -    android:layout_width="fill_parent"
    -    android:layout_height="fill_parent">
    -
    -    <LinearLayout
    -	android:orientation="horizontal"
    -	android:layout_width="fill_parent"
    -	android:layout_height="fill_parent"
    -	android:layout_weight="1">
    -	
    -	<TextView
    -	    android:text="red"
    -	    android:gravity="center_horizontal"
    -	    android:background="#aa0000"
    -	    android:layout_width="wrap_content"
    -	    android:layout_height="fill_parent"
    -	    android:layout_weight="1"/>
    -	
    -	<TextView
    -	    android:text="green"
    -	    android:gravity="center_horizontal"
    -	    android:background="#00aa00"
    -	    android:layout_width="wrap_content"
    -	    android:layout_height="fill_parent"
    -	    android:layout_weight="1"/>
    -	
    -	<TextView
    -	    android:text="blue"
    -	    android:gravity="center_horizontal"
    -	    android:background="#0000aa"
    -	    android:layout_width="wrap_content"
    -	    android:layout_height="fill_parent"
    -	    android:layout_weight="1"/>
    -	
    -	<TextView
    -	    android:text="yellow"
    -	    android:gravity="center_horizontal"
    -	    android:background="#aaaa00"
    -	    android:layout_width="wrap_content"
    -	    android:layout_height="fill_parent"
    -	    android:layout_weight="1"/>
    -		
    -    </LinearLayout>
    -	
    -    <LinearLayout
    -	android:orientation="vertical"
    -	android:layout_width="fill_parent"
    -	android:layout_height="fill_parent"
    -	android:layout_weight="1">
    -	
    -	<TextView
    -	    android:text="row one"
    -	    android:textSize="15pt"
    -	    android:layout_width="fill_parent"
    -	    android:layout_height="wrap_content"
    -	    android:layout_weight="1"/>
    -	
    -	<TextView
    -	    android:text="row two"
    -	    android:textSize="15pt"
    -	    android:layout_width="fill_parent"
    -	    android:layout_height="wrap_content"
    -	    android:layout_weight="1"/>
    -	
    -	<TextView
    -	    android:text="row three"
    -	    android:textSize="15pt"
    -	    android:layout_width="fill_parent"
    -	    android:layout_height="wrap_content"
    -	    android:layout_weight="1"/>
    -	
    -	<TextView
    -	    android:text="row four"
    -	    android:textSize="15pt"
    -	    android:layout_width="fill_parent"
    -	    android:layout_height="wrap_content"
    -	    android:layout_weight="1"/>
    -        
    -    </LinearLayout>
    -        
    -</LinearLayout>
    -
    -

    Carefully inspect the XML. You'll notice how this layout works a lot like - an HTML layout. There is one parent LinearLayout that is defined to lay - its child elements vertically. The first child is another LinearLayout that uses a horizontal layout - and the second uses a vertical layout. Each LinearLayout contains several {@link android.widget.TextView} - elements.

    -
  4. -
  5. Now open the HelloLinearLayout Activity and be sure it loads this layout in the onCreate() method:

    -
    -public void onCreate(Bundle savedInstanceState) {
    -    super.onCreate(savedInstanceState);
    -    setContentView(R.layout.main);
    -}
    -
    -

    R.layout.main refers to the main.xml layout file.

    -
  6. -
  7. Run it.
  8. -
-

You should see the following:

- - -

Notice how the various XML attributes define the View's behavior. -Pay attention to the effect of the layout_weight. Try - experimenting with different values to see how the screen real estate is - distributed based on the weight of each element.

- -

References

- - - diff --git a/docs/html/guide/tutorials/views/hello-listview.jd b/docs/html/guide/tutorials/views/hello-listview.jd deleted file mode 100644 index d90005b..0000000 --- a/docs/html/guide/tutorials/views/hello-listview.jd +++ /dev/null @@ -1,90 +0,0 @@ -page.title=Hello, ListView -parent.title=Hello, Views -parent.link=index.html -@jd:body - -

A {@link android.widget.ListView} is a View that shows items in a vertically scrolling list. The items are - acquired from a {@link android.widget.ListAdapter}.

- - -
    -
  1. Start a new project/ListActivity called HelloListView.
  2. -
  3. Open the HelloListView Java file. Make the class extend ListActivity (instead of Activity). -
    public class HelloListView extends ListActivity {
    -
  4. -
  5. Insert the following for the onCreate() method: -
    -@Override
    -public void onCreate(Bundle savedInstanceState) {
    -  super.onCreate(savedInstanceState);
    -  
    -  setListAdapter(new ArrayAdapter<String>(this,
    -          android.R.layout.simple_list_item_1, COUNTRIES));
    -  getListView().setTextFilterEnabled(true);
    -}
    -
    -

    Notice that we don't need to load a layout (at least, not in this case, because we're using - the whole screen for our list). Instead, we just call setListAdapter() (which automatically - adds a ListView to the ListActivity), and provide it with an ArrayAdapter that binds a - simple_list_item_1 layout item to each entry in the COUNTRIES - array (added next). The next line of code adds a text filter to the ListView, so that when the user - begins typing, the list will filter the entire view to display only the items that match the entry.

    -
  6. -
  7. Following the onCreate() method, add the String array: -
    -  static final String[] COUNTRIES = new String[] {
    -    "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
    -    "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
    -    "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
    -    "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",
    -    "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia",
    -    "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory",
    -    "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
    -    "Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde",
    -    "Cayman Islands", "Central African Republic", "Chad", "Chile", "China",
    -    "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo",
    -    "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic",
    -    "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic",
    -    "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea",
    -    "Estonia", "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji", "Finland",
    -    "Former Yugoslav Republic of Macedonia", "France", "French Guiana", "French Polynesia",
    -    "French Southern Territories", "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar",
    -    "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau",
    -    "Guyana", "Haiti", "Heard Island and McDonald Islands", "Honduras", "Hong Kong", "Hungary",
    -    "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica",
    -    "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos",
    -    "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg",
    -    "Macau", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands",
    -    "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova",
    -    "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia",
    -    "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand",
    -    "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Marianas",
    -    "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru",
    -    "Philippines", "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar",
    -    "Reunion", "Romania", "Russia", "Rwanda", "Sqo Tome and Principe", "Saint Helena",
    -    "Saint Kitts and Nevis", "Saint Lucia", "Saint Pierre and Miquelon",
    -    "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Saudi Arabia", "Senegal",
    -    "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
    -    "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "South Korea",
    -    "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden",
    -    "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Bahamas",
    -    "The Gambia", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey",
    -    "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda",
    -    "Ukraine", "United Arab Emirates", "United Kingdom",
    -    "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan",
    -    "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Wallis and Futuna", "Western Sahara",
    -    "Yemen", "Yugoslavia", "Zambia", "Zimbabwe"
    -  };
    -
    -
  8. -
  9. Run it.
  10. -
-

You can scroll the list, or type to filter it. You should see something like this:

- - -

References

- - diff --git a/docs/html/guide/tutorials/views/hello-mapview.jd b/docs/html/guide/tutorials/views/hello-mapview.jd deleted file mode 100644 index b0f59de..0000000 --- a/docs/html/guide/tutorials/views/hello-mapview.jd +++ /dev/null @@ -1,243 +0,0 @@ -page.title=Hello, MapView -parent.title=Hello, Views -parent.link=index.html -@jd:body - -

A MapView allows you to create your own map-viewing Activity. -First, we'll create a simple Activity that can view and navigate a map. Then we will add some overlay items.

- -
    -
  1. Start a new project/Activity called HelloMapView. - -
  2. Because we're using the Google Maps library, - which is not a part of the standard Android library, we need to - declare it in the Android Manifest. Open the AndroidManifest.xml - file and add the following as a child of the <application> element: - -
    <uses-library android:name="com.google.android.maps" />
    -
  3. - -
  4. Open the layout file. Define the layout with a MapView inside a RelativeLayout: - -
    -<?xml version="1.0" encoding="utf-8"?>
    -<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    -    android:id="@+id/mainlayout"
    -    android:orientation="vertical"
    -    android:layout_width="fill_parent"
    -    android:layout_height="fill_parent" >
    -
    -    <com.google.android.maps.MapView
    -        android:id="@+id/mapview"
    -        android:layout_width="fill_parent"
    -        android:layout_height="fill_parent"
    -        android:clickable="true"
    -        android:apiKey="INSERT YOUR KEY HERE"
    -    />
    -
    -<RelativeLayout>
    -
    -

    Setting clickable is important. Otherwise, the map does not allow any user interaction.

    - -

    The android:apiKey must contain an authentic Android Maps API key. - The API key is generated using the MD5 fingerprint of your application certificate. For the purposes of - this exercise, you should use the fingerprint of your debug certificate (which cannot be used to release - your application for Android devices, but will work while developing). See how to - generate a fingerprint from your - debug certificate, then register the - certificate to retieve an API key. - Insert the API key as the value of the apiKey attribute. If you do not insert a valid - Maps API key, the application will still run, but no map tiles will load.

  5. - -
  6. Now open the HelloMapView.java file. For this Activity, we're going to extend the special sub-class of - Activity called MapActivity, so change the class declaration to extend - MapActicity, instead of Activity:

    - -
    public class HelloMapView extends MapActivity {
    - -
  7. The isRouteDisplayed() method is required, so add it inside the class: -
    -@Override
    -protected boolean isRouteDisplayed() {
    -    return false;
    -}
    -
    -

    You can actually run this now, but all it does is allow you to pan around the map.

    -

    Android provides a handy {@link android.widget.ZoomControls} widget for zooming in and out of a View. -MapView can automatically hook one for us by requesting it with the getZoomControls() -method. Let's do this.

    - -
  8. Go back to the layout file. We need a new ViewGroup element, in which we'll - place the ZoomControls. Just below the MapView element (but inside the RelativeLayout), add this element: -
    -<LinearLayout
    -    android:id="@+id/zoomview"
    -    android:layout_width="wrap_content"
    -    android:layout_height="wrap_content"
    -    android:layout_alignBottom="@id/mapview"
    -    android:layout_centerHorizontal="true"
    -/>
    - -

    It doesn't really matter what kind of ViewGroup we use, because we just want a - container that we can position within our root RelativeLayout.

    - -

    The last two attributes are available only to an element that's a child of a - RelativeLayout. layout_alignBottom aligns the bottom of this element to the bottom of - the element identified with a resource tag (which must be a sibling to this element). - layout_centerHorizontal centers this on the horizontal plane.

  9. - -
  10. Now go back to the HelloMapView class. We'll now retrieve the ZoomControls object from - the MapView and add it to our new layout element. First, at the top of the HelloMapView, - instantiate handles for the MapView and LinearLayout, plus a ZoomControl object: -
    -LinearLayout linearLayout;
    -MapView mapView;
    -ZoomControls mZoom;
  11. - -
  12. Then initialize each of these in onCreate(). We'll capture the LinearLayout and - MapView through their layout resources. Then get the ZoomControls from the MapView:: -
    -linearLayout = (LinearLayout) findViewById(R.id.zoomview);
    -mapView = (MapView) findViewById(R.id.mapview);
    -mZoom = (ZoomControls) mapView.getZoomControls();
    - -

    By using the ZoomControls object provided by MapView, we don't have to do any of the work - required to actually perform the zoom operations. The ZoomControls widget that MapView - returns for us is already hooked into the MapView and works as soon as we add it to the - layout. The controls will appear whenever the user touches the map, then dissapear after - a few moments of inactivity.

  13. - -
  14. Now just plug our ZoomControls into the LinearLayout we added: - -
    linearLayout.addView(mZoom);
  15. - -
  16. Run it.
  17. -
- -
- -

So, we now have full interaction controls. All well and good, but what we really want our map -for is custom markers and layovers. Let's add some Overlay -objects to our map. To do this, we're going to -implement the ItemizedOverlay -class, which can manage a whole set of Overlay items for us.

- -
    -
  1. Create a new Java class named HelloItemizedOverlay that implements ItemizedOverlay. - -

    When using Eclipse, right-click the package name in the Eclipse Package Explorer, and select New > Class. Fill-in - the Name field as HelloItemizedOverlay. For the Superclass, enter - com.google.android.maps.ItemizedOverlay. Click the checkbox for Constructors from - superclass. Click Finish.

  2. - -
  3. First thing, we need an OverlayItem ArrayList, in which we'll put each of the OverlayItem - objects we want on our map. Add this at the top of the HelloItemizedOverlay class: - -
    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
  4. - -
  5. All the constructor does is define the default marker to be used on each of the OverlayItems. - In order for the Drawable to actually get drawn, it must have its bounds defined. And we want the - center-point at the bottom of the image to be the point at which it's attached to the map - coordinates. We handle all this with the boundCenterBottom() method. Wrap this around our - defaultMarker, so the super constructor call looks like this: - -
    super(boundCenterBottom(defaultMarker));
  6. - -
  7. In order to add new OverlayItems to our ArrayList, we need a new public method. We'll handle - this with the following method: - -
    -public void addOverlay(OverlayItem overlay) {
    -    mOverlays.add(overlay);
    -    populate();
    -}
    - -

    Each time we add a new OverlayItem, we must call populate(), which will read each of out - OverlayItems and prepare them to be drawn.

  8. - -
  9. In order for the populate() method to read each OverlayItem, it will make a request to - createItem(int). We must define this method to properly read from our ArrayList. Replace the - existing contents of the createItem method with a get() call to our ArrayList: - -
    -@Override
    -protected OverlayItem createItem(int i) {
    -  return mOverlays.get(i);
    -}
    -
  10. - -
  11. We're also required to override the size() method. Replace the existing contents of the - method with a size request to our ArrayList: - -
    return mOverlays.size();
  12. -
- - -

That's it for the HelloItemizedOverlay class. We're now ready to use it.

- -
-

Go back to the HelloMapView -class. We'll start by creating one OverlayItem, adding to an instance of our HelloItemizedOverlay, -and then adding this to the MapView.

- - -

First, we need the image that we'll use for our map overlay. Here, we'll use the Android on the -right as our marker. Drag this image (or your own) to the res/drawable/ directory of your project workspace.

- -

Now we're ready to work in the HelloMapView:

- -
    -
  1. First we need some more types. Add the following at the top of the HelloMapView class: - -
    -List<Overlay> mapOverlays;
    -Drawable drawable;
    -HelloItemizedOverlay itemizedOverlay;
  2. - -
  3. Now pick up where we left off in the onCreate() method. Instantiate the - new fields: - -
    -mapOverlays = mapView.getOverlays();
    -drawable = this.getResources().getDrawable(R.drawable.androidmarker);
    -itemizedoverlay = new HelloItemizedOverlay(drawable);
    - -

    All overlay elements on a map are held by the MapView, so when we want to add some, we must - first retrieve the List with getOverlays() methods. We instantiate the Drawable, which will - be used as our map marker, by using our Context resources to get the Drawable we placed in - the res/drawable/ directory (androidmarker.png). Our HelloItemizedOverlay takes the Drawable in order to set the - default marker.

  4. - -
  5. Now let's make our first OverlayItem by creating a GeoPoint - that defines our map coordinates, then pass it to a new OverlayItem: - -
    -GeoPoint point = new GeoPoint(19240000,-99120000);
    -OverlayItem overlayitem = new OverlayItem(point, "", "");
    - -

    GeoPoint coordinates are based in microdegrees (degrees * 1e6). The OverlayItem takes this - GeoPoint and two strings. Here, we won't concern ourselves with the strings, which can display - text when we click our marker, because we haven't yet written the click handler for the OverlayItem.

  6. - -
  7. All that's left is for us to add this OverlayItem to our collection in the HelloItemizedOverlay, - and add this to the List of Overlay objects retrieved from the MapView: - -
    -itemizedoverlay.addOverlay(overlayitem);
    -mapOverlays.add(itemizedoverlay);
  8. - -
  9. Run it!
  10. -
- -

We've sent our droid to Mexico City. Hola, Mundo!

-

You should see the following:

- - -

Because we created our ItemizedOverlay class with an ArrayList, we can continue adding new -OverlayItems. Try adding another one. Before the addOverlay() method is called, add these lines:

-
-GeoPoint point2 = new GeoPoint(35410000, 139460000);
-OverlayItem overlayitem2 = new OverlayItem(point2, "", "");
-
-

Run it again... We've sent a new droid to Tokyo. Sekai, konichiwa!

- diff --git a/docs/html/guide/tutorials/views/hello-relativelayout.jd b/docs/html/guide/tutorials/views/hello-relativelayout.jd deleted file mode 100644 index 1b91537..0000000 --- a/docs/html/guide/tutorials/views/hello-relativelayout.jd +++ /dev/null @@ -1,75 +0,0 @@ -page.title=Hello, RelativeLayout -parent.title=Hello, Views -parent.link=index.html -@jd:body - -

A {@link android.widget.RelativeLayout} is a ViewGroup that allows you to layout child elements -in positions relative to the parent or siblings elements.

- -
    -
  1. Start a new project/Activity called HelloRelativeLayout.
  2. -
  3. Open the layout file. Make it like so: -
    -<?xml version="1.0" encoding="utf-8"?>
    -<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    -    android:layout_width="fill_parent"
    -    android:layout_height="fill_parent">
    -
    -    <TextView
    -        android:id="@+id/label"
    -        android:layout_width="fill_parent"
    -        android:layout_height="wrap_content"
    -        android:text="Type here:"/>
    -
    -    <EditText
    -        android:id="@+id/entry"
    -        android:layout_width="fill_parent"
    -        android:layout_height="wrap_content"
    -        android:background="@android:drawable/editbox_background"
    -        android:layout_below="@id/label"/>
    -
    -    <Button
    -        android:id="@+id/ok"
    -        android:layout_width="wrap_content"
    -        android:layout_height="wrap_content"
    -        android:layout_below="@id/entry"
    -        android:layout_alignParentRight="true"
    -        android:layout_marginLeft="10dip"
    -        android:text="OK" />
    -
    -    <Button
    -        android:layout_width="wrap_content"
    -        android:layout_height="wrap_content"
    -        android:layout_toLeftOf="@id/ok"
    -        android:layout_alignTop="@id/ok"
    -        android:text="Cancel" />
    -
    -</RelativeLayout>
    -
    -

    Pay attention to each of the additional layout_* attributes (besides the -usual width and height, which are required for all elements). When using relative layout, -we use attributes like layout_below and layout_toLeftOf to describe -how we'd like to position each View. Naturally, these are different relative positions, and the -value of the attribute is the id of the element we want the position relative to.

    -
  4. -
  5. Make sure your Activity loads this layout in the onCreate() method:

    -
    -public void onCreate(Bundle savedInstanceState) {
    -    super.onCreate(savedInstanceState);
    -    setContentView(R.layout.main);
    -}
    -
    -

    R.layout.main refers to the main.xml layout file.

    -
  6. -
  7. Run it.
  8. -
-

You should see the following:

- - -

Resources

- diff --git a/docs/html/guide/tutorials/views/hello-spinner.jd b/docs/html/guide/tutorials/views/hello-spinner.jd deleted file mode 100644 index 3a04214..0000000 --- a/docs/html/guide/tutorials/views/hello-spinner.jd +++ /dev/null @@ -1,106 +0,0 @@ -page.title=Hello, Spinner -parent.title=Hello, Views -parent.link=index.html -@jd:body - -

A {@link android.widget.Spinner} is a widget that allows the user to select an item from a group. -It is similar to a dropdown list and will allow scrolling when the -list exceeds the available vertical space on the screen.

- - -
    -
  1. Start a new project/Activity called HelloSpinner.
  2. -
  3. Open the layout file. - Make it like so: -
    -<?xml version="1.0" encoding="utf-8"?>
    -<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    -    android:orientation="vertical"
    -    android:padding="10dip"
    -    android:layout_width="fill_parent"
    -    android:layout_height="wrap_content">
    -
    -    <TextView
    -        android:layout_width="fill_parent"
    -        android:layout_height="wrap_content"
    -        android:layout_marginTop="10dip"
    -        android:text="Please select a planet:"
    -    />
    -
    -    <Spinner 
    -        android:id="@+id/spinner"
    -        android:layout_width="fill_parent"
    -        android:layout_height="wrap_content"
    -        android:drawSelectorOnTop="true"
    -        android:prompt="@string/planet_prompt"
    -    />
    -
    -</LinearLayout>
    -
    -

    Notice that the Spinner's android:prompt is a string resource. In - this case, Android does not allow it to be a string, it must be a reference to a resource. - So...

    -
  4. - -
  5. Open the strings.xml file in res/values/ and add the following <string> -element inside the <resources> element: -
    -<string name="planet_prompt">Choose a planet</string>
    -
    -
  6. - -
  7. Create a new XML file in res/values/ called arrays.xml. Insert the following: -
    -<resources>
    -
    -    <string-array name="planets">
    -        <item>Mercury</item>
    -        <item>Venus</item>
    -        <item>Earth</item>
    -        <item>Mars</item>
    -        <item>Jupiter</item>
    -        <item>Saturn</item>
    -        <item>Uranus</item>
    -        <item>Neptune</item>
    -    </string-array>
    -    
    -</resources>
    -
    -

    This is the list of items (planets) that the user can select from in the Spinner widget.

    -
  8. - -
  9. Now open the HelloSpinner.java file. Insert the following code into the HelloSpinner class: -
    -@Override
    -public void onCreate(Bundle savedInstanceState) {
    -    super.onCreate(savedInstanceState);
    -    setContentView(R.layout.main);
    -
    -    Spinner s = (Spinner) findViewById(R.id.spinner);
    -    ArrayAdapter adapter = ArrayAdapter.createFromResource(
    -            this, R.array.planets, android.R.layout.simple_spinner_item);
    -    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    -    s.setAdapter(adapter);
    -}
    -
    -

    That's it. We start by creating a Spinner from our layout. We then create an {@link android.widget.ArrayAdapter} - that binds each element of our string array to a layout view—we pass createFromResource our Context, - the array of selectable items and the type of layout we'd like each one bound to. We then call - setDropDownViewResource() to define the type of layout in which to present the - entire collection. Finally, we set this Adapter to associate with our Spinner, - so the string items have a place to go.

    -
  10. - -
  11. Now run it.
  12. -
-

It should look like this:

- - - -

Resources

- - diff --git a/docs/html/guide/tutorials/views/hello-tablelayout.jd b/docs/html/guide/tutorials/views/hello-tablelayout.jd deleted file mode 100644 index 83d6f5d..0000000 --- a/docs/html/guide/tutorials/views/hello-tablelayout.jd +++ /dev/null @@ -1,118 +0,0 @@ -page.title=Hello, TableLayout -parent.title=Hello, Views -parent.link=index.html -@jd:body - -

A {@link android.widget.TableLayout} is a ViewGroup that -will lay child View elements into rows and columns.

- - -
    -
  1. Start a new project/Activity called HelloTableLayout.
  2. -
  3. Open the layout file. - Make it like so: -
    -<?xml version="1.0" encoding="utf-8"?>
    -<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    -    android:layout_width="fill_parent"
    -    android:layout_height="fill_parent"
    -    android:stretchColumns="1">
    -
    -    <TableRow>
    -        <TextView
    -            android:layout_column="1"
    -            android:text="Open..."
    -            android:padding="3dip" />
    -        <TextView
    -            android:text="Ctrl-O"
    -            android:gravity="right"
    -            android:padding="3dip" />
    -    </TableRow>
    -
    -    <TableRow>
    -        <TextView
    -            android:layout_column="1"
    -            android:text="Save..."
    -            android:padding="3dip" />
    -        <TextView
    -            android:text="Ctrl-S"
    -            android:gravity="right"
    -            android:padding="3dip" />
    -    </TableRow>
    -
    -    <TableRow>
    -        <TextView
    -            android:layout_column="1"
    -            android:text="Save As..."
    -            android:padding="3dip" />
    -        <TextView
    -            android:text="Ctrl-Shift-S"
    -            android:gravity="right"
    -            android:padding="3dip" />
    -    </TableRow>
    -
    -    <View
    -        android:layout_height="2dip"
    -        android:background="#FF909090" />
    -
    -    <TableRow>
    -        <TextView
    -            android:text="X"
    -            android:padding="3dip" />
    -        <TextView
    -            android:text="Import..."
    -            android:padding="3dip" />
    -    </TableRow>
    -
    -    <TableRow>
    -        <TextView
    -            android:text="X"
    -            android:padding="3dip" />
    -        <TextView
    -            android:text="Export..."
    -            android:padding="3dip" />
    -        <TextView
    -            android:text="Ctrl-E"
    -            android:gravity="right"
    -            android:padding="3dip" />
    -    </TableRow>
    -
    -    <View
    -        android:layout_height="2dip"
    -        android:background="#FF909090" />
    -
    -    <TableRow>
    -        <TextView
    -            android:layout_column="1"
    -            android:text="Quit"
    -            android:padding="3dip" />
    -    </TableRow>
    -</TableLayout>
    -
    -

    Notice how this resembles the structure of an HTML table. TableLayout is like the -table element; TableRow is like a tr element; but for our cells like -the html td element, we can use any kind of View. Here, we use TextView for the cells.

    - -
  4. -
  5. Make sure your Activity loads this layout in the onCreate() method: -
    -public void onCreate(Bundle savedInstanceState) {
    -    super.onCreate(savedInstanceState);
    -    setContentView(R.layout.main);
    -}
    -
    -

    R.layout.main refers to the main.xml layout file.

    -
  6. -
  7. Run it.
  8. -
-

You should see the following:

- - -

References

- - - diff --git a/docs/html/guide/tutorials/views/hello-tabwidget.jd b/docs/html/guide/tutorials/views/hello-tabwidget.jd deleted file mode 100644 index 8424616..0000000 --- a/docs/html/guide/tutorials/views/hello-tabwidget.jd +++ /dev/null @@ -1,124 +0,0 @@ -page.title=Hello, TabWidget -parent.title=Hello, Views -parent.link=index.html -@jd:body - -

A {@link android.widget.TabWidget} offers the ability to easily draw an interface that uses -tabs to navigate between different views.

- -
    -
  1. Start a new project/Activity called HelloTabWidget.
  2. -
  3. Open the layout file and make it like so:
  4. -
    -<?xml version="1.0" encoding="utf-8"?>
    -<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    -    android:id="@android:id/tabhost"
    -    android:layout_width="fill_parent"
    -    android:layout_height="fill_parent">
    -    <LinearLayout
    -        android:orientation="vertical"
    -        android:layout_width="fill_parent"
    -        android:layout_height="fill_parent">
    -        <TabWidget
    -            android:id="@android:id/tabs"
    -            android:layout_width="fill_parent"
    -            android:layout_height="wrap_content" />
    -        <FrameLayout
    -            android:id="@android:id/tabcontent"
    -            android:layout_width="fill_parent"
    -            android:layout_height="fill_parent">
    -            <TextView 
    -                android:id="@+id/textview1"
    -                android:layout_width="fill_parent"
    -                android:layout_height="fill_parent" 
    -                android:text="this is a tab" />
    -            <TextView 
    -                android:id="@+id/textview2"
    -                android:layout_width="fill_parent"
    -                android:layout_height="fill_parent" 
    -                android:text="this is another tab" />
    -            <TextView 
    -                android:id="@+id/textview3"
    -                android:layout_width="fill_parent"
    -                android:layout_height="fill_parent" 
    -                android:text="this is a third tab" />
    -    	</FrameLayout>
    -    </LinearLayout>
    -</TabHost>
    -
    -

    Here, we've created a {@link android.widget.TabHost} that contains the entire layout of the Activity. - A TabHost requires two descendant elements: a {@link android.widget.TabWidget} and a {@link android.widget.FrameLayout}. - In order to properly layout these elements, we've put them inside a vertical {@link android.widget.LinearLayout}. - The FrameLayout is where we keep the content that will change with each tab. Each child in the FrameLayout will - be associated with a different tab. - In this case, each tab simply shows a different {@link android.widget.TextView} with some text.

    -

    Notice that the TabWidget and the FrameLayout elements have specific android namespace IDs. These are necessary - so that the TabHost can automatically retireve references to them, populate the TabWidget with the tabs that we'll define - in our code, and swap the views in the FrameLayout. We've also defined our own IDs for each TextView, which we'll use to - associate each tab with the view that it should reveal.

    -

    Of course, you can - make these child views as large as complex as you'd like — instead of the TextView elements, - you could start with other layout views and build a unique layout hierarchy for each tab.

    - -
  5. Now we'll add our code. Open HelloTabWidget.java and make it a TabActivity. -

    By default, Eclipse creates a class that extends Activity. Change it to - extend TabActivity:

    -
    -public class HelloTabWidget extends TabActivity {
    -
    -
  6. -
  7. Now fill in the the onCreate method like this: -
    -public void onCreate(Bundle savedInstanceState) {
    -    super.onCreate(savedInstanceState);
    -    setContentView(R.layout.main);
    -
    -    mTabHost = getTabHost();
    -    
    -    mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(R.id.textview1));
    -    mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.textview2));
    -    mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3").setContent(R.id.textview3));
    -    
    -    mTabHost.setCurrentTab(0);
    -}
    -
    -

    As usual, we start by setting our layout.

    -

    We then call the TabActivity method getTabHost(), - which returns us a reference to the TabHost we created in our layout. Upon our TabHost, we call addTab() - for each of the tabs that we want to add to the TabWidget. Each time we call this, we pass a - {@link android.widget.TabHost.TabSpec} that we build on the fly, and with it, chain together two necessary methods: - setIndicator() to set the text for the tab button, and setContent() to define - which View we want to associate with the tab and reveal when pressed. Our indicator is just a text string and - our content is an ID reference to the TextView elements we inserted in the FrameLayout.

    -

    At the end, we call setCurrentTab() to define which tab should be opened by default. The tabs - are saved like a zero-based array, so to open the first tab, we pass zero (0).

    -
  8. -
  9. To clean-up the presentation a bit more, let's remove the window title that appears at the top of the layout. - Android includes a theme that removes that title for us. To add it, open the Android Manifest file and add - the NoTitleBar theme to the <application> tag. It should end up like this: -
    -<application android:icon="@drawable/icon" android:theme="@android:style/Theme.NoTitleBar">
    -
    -
  10. -
  11. That's it. Run your application.
  12. - -
- - -

Your application should look like this:

- - -

You can include icons in your tabs by passing a -{@link android.graphics.drawable.Drawable} when you call setIndicator(). Here's an example -that uses a Drawable created from an image in the project resources:

-
setIndicator("TAB 1", getResources().getDrawable(R.drawable.tab_icon))
-
- -

References

- - diff --git a/docs/html/guide/tutorials/views/hello-timepicker.jd b/docs/html/guide/tutorials/views/hello-timepicker.jd deleted file mode 100644 index 1a6c8f9..0000000 --- a/docs/html/guide/tutorials/views/hello-timepicker.jd +++ /dev/null @@ -1,159 +0,0 @@ -page.title=Hello, TimePicker -parent.title=Hello, Views -parent.link=index.html -@jd:body - -

A {@link android.widget.TimePicker} is a widget that allows the -user to select the time by hour, minute and AM or PM.

- - -
    -
  1. Start a new project/Activity called HelloTimePicker.
  2. -
  3. Open the layout file and make it like so: -
    -<?xml version="1.0" encoding="utf-8"?>
    -<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    -    android:layout_width="wrap_content"
    -    android:layout_height="wrap_content"
    -    android:orientation="vertical">
    -
    -    <TextView android:id="@+id/timeDisplay"
    -        android:layout_width="wrap_content"
    -        android:layout_height="wrap_content"
    -        android:text=""/>
    -
    -    <Button android:id="@+id/pickTime"
    -        android:layout_width="wrap_content"
    -        android:layout_height="wrap_content"
    -        android:text="Change the time"/>
    -
    -</LinearLayout>
    -
    -

    For the layout, we're using a vertical LinearLayout, with a {@link android.widget.TextView} that - will display the time and a {@link android.widget.Button} that will initiate the - {@link android.widget.TimePicker} dialog. - With this layout, the TextView will sit above the Button. - The text value in the TextView is set empty, as it will be filled by our Activity - with the current time.

    -
  4. - -
  5. Open HelloTimePicker.java. Insert the following to the HelloTimePicker class: -
    -private TextView mTimeDisplay;
    -private Button mPickTime;
    -
    -private int mHour;
    -private int mMinute;
    -
    -static final int TIME_DIALOG_ID = 0;
    -
    -@Override
    -protected void onCreate(Bundle savedInstanceState) {
    -    super.onCreate(savedInstanceState);
    -    setContentView(R.layout.main);
    -    
    -    // capture our View elements
    -    mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
    -    mPickTime = (Button) findViewById(R.id.pickTime);
    -
    -    // add a click listener to the button
    -    mPickTime.setOnClickListener(new View.OnClickListener() {
    -        public void onClick(View v) {
    -            showDialog(TIME_DIALOG_ID);
    -        }
    -    });
    -
    -    // get the current time
    -    final Calendar c = Calendar.getInstance();
    -    mHour = c.get(Calendar.HOUR_OF_DAY);
    -    mMinute = c.get(Calendar.MINUTE);
    -
    -    // display the current date
    -    updateDisplay();
    -}
    -
    -

    Tip: Press Ctrl(or Cmd) + Shift + O to import all needed packages.

    -

    We start by instantiating variables for our View elements and time fields. - The TIME_DIALOG_ID is a static integer that uniquely identifies the dialog. In the - onCreate() method, we get prepared by setting the layout and capturing the View elements. - We then set an on-click listener for the Button, so that when it is clicked, it will - show our TimePicker dialog. The showDialog() method will perform a callback - to our Activity. (We'll define this callback in the next section.) We then create an - instance of {@link java.util.Calendar} and get the current hour and minute. Finally, we call - updateDisplay()—our own method that will fill the TextView with the time.

    -
  6. - -
  7. After the onCreate() method, add the onCreateDialog() callback method: -
    -@Override
    -protected Dialog onCreateDialog(int id) {
    -    switch (id) {
    -    case TIME_DIALOG_ID:
    -        return new TimePickerDialog(this,
    -                mTimeSetListener, mHour, mMinute, false);
    -    }
    -    return null;
    -}
    -
    -

    This is passed the identifier we gave showDialog() and initializes - the TimePicker to the time we retrieved from our Calendar instance. It will be called by - showDialog().

    -
  8. - -
  9. Now add our updateDisplay() method: -
    -// updates the time we display in the TextView
    -private void updateDisplay() {
    -    mTimeDisplay.setText(
    -        new StringBuilder()
    -                .append(pad(mHour)).append(":")
    -                .append(pad(mMinute)));
    -}
    -
    -

    This simply takes our member fields for the time and inserts them in - the mTimeDisplay TextView. Note that we call a new method, pad(), - on the hour and minute. (We'll create this method in the last step.)

    -
  10. - -
  11. Next, add a listener to be called when the time is reset: -
    -// the callback received when the user "sets" the time in the dialog
    -private TimePickerDialog.OnTimeSetListener mTimeSetListener =
    -    new TimePickerDialog.OnTimeSetListener() {
    -        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    -            mHour = hourOfDay;
    -            mMinute = minute;
    -            updateDisplay();
    -        }
    -    };
    -
    -

    Now when the user is done setting the time (clicks the "Set" button), we update our member fields with - the new time and update our TextView.

    -
  12. -
  13. Finally, add the pad() method that we called from the updateDisplay(): -
    -private static String pad(int c) {
    -    if (c >= 10)
    -        return String.valueOf(c);
    -    else
    -        return "0" + String.valueOf(c);
    -}
    -
    -

    This method returns the appropriate String representation of the hour or minute. - It will prefix a zero to the number if it's a single digit. -

    -
  14. - -
  15. Now run it.
  16. -
-

When you press the "Change the time" button, you should see the following:

- - -

References

-
    -
  1. {@link android.widget.TimePicker}
  2. -
  3. {@link android.widget.Button}
  4. -
  5. {@link android.widget.TextView}
  6. -
  7. {@link java.util.Calendar}
  8. -
- diff --git a/docs/html/guide/tutorials/views/hello-webview.jd b/docs/html/guide/tutorials/views/hello-webview.jd deleted file mode 100644 index c4388ea..0000000 --- a/docs/html/guide/tutorials/views/hello-webview.jd +++ /dev/null @@ -1,118 +0,0 @@ -page.title=Hello, WebView -parent.title=Hello, Views -parent.link=index.html -@jd:body - -

A {@link android.webkit.WebView} allows you to create your own web browser Activity. In this tutorial, -we'll create a simple Activity that can view web pages.

- -
    -
  1. Create a new project/Activity called HelloWebView.
  2. -
  3. Open the layout file. Insert a WebView so it looks like so: -
    -<?xml version="1.0" encoding="utf-8"?>
    -<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    -    android:layout_width="wrap_content"
    -    android:layout_height="wrap_content"
    -    android:orientation="vertical">
    -
    -    <WebView 
    -        android:id="@+id/webview"
    -        android:layout_width="fill_parent"
    -        android:layout_height="fill_parent"
    -    />
    -
    -</LinearLayout>
    -
  4. - -
  5. Now open the HelloWebView.java file. - At the top of the class, instantiate a WebView object: -
    WebView webview;
    -

    Then add the following at the end of the onCreate() method:

    -
    -webview = (WebView) findViewById(R.id.webview);
    -webview.getSettings().setJavaScriptEnabled(true);
    -webview.loadUrl("http://www.google.com");
    -
    - -

    This captures the WebView we created in our layout, then requests a - {@link android.webkit.WebSettings} object and enables JavaScript. - Then we load a URL.

  6. - -
  7. Because we're accessing the internet, we need to add the appropriate - permissions to the Android manifest file. So open the AndroidManifest.xml file - and, add the following as a child of the <manifest> element: - -
    <uses-permission android:name="android.permission.INTERNET" />
  8. - -
  9. Now run it.
  10. -
-

You now have the world's simplest web page viewer. - It's not quite a browser yet. It only loads the page we've requested.

- -
- -

We can load a page, but as soon as we click a link, the default Android web browser -handles the Intent, instead of our own WebView handling the action. So now we'll -override the {@link android.webkit.WebViewClient} to enable us to handle our own URL loading.

- -
    -
  1. In the HelloAndroid Activity, add this nested private class: -
    -private class HelloWebViewClient extends WebViewClient {
    -    @Override
    -    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    -        view.loadUrl(url);
    -        return true;
    -    }
    -}
  2. - -
  3. Now, in the onCreate() method, set an instance of the HelloWebViewClient - as our WebViewClient: -
    webview.setWebViewClient(new WebViewClientDemo());
    - -

    This line should immediately follow the initialization of our WebView object.

    -

    What we've done is create a WebViewClient that will load any URL selected in our -WebView in the same WebView. You can see this in the shouldOverrideUrlLoading() -method, above—it is passed the current WebView and the URL, so all we do -is load the URL in the given view. Returning true says that we've handled the URL -ourselves and the event should not bubble-up.

    -

    If you try it again, new pages will now load in the HelloWebView Activity. However, you'll notice that -we can't navigate back. We need to handle the back button -on the device, so that it will return to the previous page, rather than exit the application.

    -
  4. - -
  5. To handle the back button key press, add the following method inside the HelloWebView -Activity: -
     
    -@Override
    -public boolean onKeyDown(int keyCode, KeyEvent event) {
    -    if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
    -        webview.goBack();
    -        return true;
    -    }
    -    return super.onKeyDown(keyCode, event);
    -}
    -

    The condition uses a {@link android.view.KeyEvent} to check - whether the key pressed is the BACK button and whether the - WebView is actually capable of navigating back (if it has a history). If both are - not true, then we send the event up the chain (and the Activity will close). - But if both are true, then we call goBack(), - which will navigate back one step in the history. We then return true to indicate - that we've handled the event.

    -
  6. -
-

When you open the application, it should look like this:

- - -

Resource

- - - - - - diff --git a/docs/html/guide/tutorials/views/images/android.png b/docs/html/guide/tutorials/views/images/android.png deleted file mode 100755 index 39a1ac7..0000000 Binary files a/docs/html/guide/tutorials/views/images/android.png and /dev/null differ diff --git a/docs/html/guide/tutorials/views/images/androidmarker.png b/docs/html/guide/tutorials/views/images/androidmarker.png deleted file mode 100755 index 8c43d46..0000000 Binary files a/docs/html/guide/tutorials/views/images/androidmarker.png and /dev/null differ diff --git a/docs/html/guide/tutorials/views/images/hello-autocomplete.png b/docs/html/guide/tutorials/views/images/hello-autocomplete.png deleted file mode 100755 index e1fd80d..0000000 Binary files a/docs/html/guide/tutorials/views/images/hello-autocomplete.png and /dev/null differ diff --git a/docs/html/guide/tutorials/views/images/hello-datepicker.png b/docs/html/guide/tutorials/views/images/hello-datepicker.png deleted file mode 100755 index 2075066..0000000 Binary files a/docs/html/guide/tutorials/views/images/hello-datepicker.png and /dev/null differ diff --git a/docs/html/guide/tutorials/views/images/hello-formstuff.png b/docs/html/guide/tutorials/views/images/hello-formstuff.png deleted file mode 100755 index 3b4bf54..0000000 Binary files a/docs/html/guide/tutorials/views/images/hello-formstuff.png and /dev/null differ diff --git a/docs/html/guide/tutorials/views/images/hello-gallery.png b/docs/html/guide/tutorials/views/images/hello-gallery.png deleted file mode 100755 index 22d1eaf..0000000 Binary files a/docs/html/guide/tutorials/views/images/hello-gallery.png and /dev/null differ diff --git a/docs/html/guide/tutorials/views/images/hello-gridview.png b/docs/html/guide/tutorials/views/images/hello-gridview.png deleted file mode 100755 index 2def0df..0000000 Binary files a/docs/html/guide/tutorials/views/images/hello-gridview.png and /dev/null differ diff --git a/docs/html/guide/tutorials/views/images/hello-linearlayout.png b/docs/html/guide/tutorials/views/images/hello-linearlayout.png deleted file mode 100755 index dfef819..0000000 Binary files a/docs/html/guide/tutorials/views/images/hello-linearlayout.png and /dev/null differ diff --git a/docs/html/guide/tutorials/views/images/hello-listview.png b/docs/html/guide/tutorials/views/images/hello-listview.png deleted file mode 100755 index a1cf7aa..0000000 Binary files a/docs/html/guide/tutorials/views/images/hello-listview.png and /dev/null differ diff --git a/docs/html/guide/tutorials/views/images/hello-mapview.png b/docs/html/guide/tutorials/views/images/hello-mapview.png deleted file mode 100755 index 0956760..0000000 Binary files a/docs/html/guide/tutorials/views/images/hello-mapview.png and /dev/null differ diff --git a/docs/html/guide/tutorials/views/images/hello-relativelayout.png b/docs/html/guide/tutorials/views/images/hello-relativelayout.png deleted file mode 100755 index ec4d9d4..0000000 Binary files a/docs/html/guide/tutorials/views/images/hello-relativelayout.png and /dev/null differ diff --git a/docs/html/guide/tutorials/views/images/hello-spinner.png b/docs/html/guide/tutorials/views/images/hello-spinner.png deleted file mode 100755 index 42e2a91..0000000 Binary files a/docs/html/guide/tutorials/views/images/hello-spinner.png and /dev/null differ diff --git a/docs/html/guide/tutorials/views/images/hello-tablelayout.png b/docs/html/guide/tutorials/views/images/hello-tablelayout.png deleted file mode 100755 index 3d80e7f..0000000 Binary files a/docs/html/guide/tutorials/views/images/hello-tablelayout.png and /dev/null differ diff --git a/docs/html/guide/tutorials/views/images/hello-tabwidget.png b/docs/html/guide/tutorials/views/images/hello-tabwidget.png deleted file mode 100644 index 6a52356..0000000 Binary files a/docs/html/guide/tutorials/views/images/hello-tabwidget.png and /dev/null differ diff --git a/docs/html/guide/tutorials/views/images/hello-timepicker.png b/docs/html/guide/tutorials/views/images/hello-timepicker.png deleted file mode 100755 index bd5a1ee..0000000 Binary files a/docs/html/guide/tutorials/views/images/hello-timepicker.png and /dev/null differ diff --git a/docs/html/guide/tutorials/views/images/hello-webview.png b/docs/html/guide/tutorials/views/images/hello-webview.png deleted file mode 100755 index 283ce7d..0000000 Binary files a/docs/html/guide/tutorials/views/images/hello-webview.png and /dev/null differ diff --git a/docs/html/guide/tutorials/views/index.jd b/docs/html/guide/tutorials/views/index.jd deleted file mode 100644 index 2248c68..0000000 --- a/docs/html/guide/tutorials/views/index.jd +++ /dev/null @@ -1,118 +0,0 @@ -page.title=Hello, Views -@jd:body - - - -

This collection of "Hello World"-style tutorials is designed -to get you quickly started with common Android Views and widgets. The aim is to let you copy and paste -these kinds of boring bits so you can focus on developing the code that makes your Android application rock. -Of course, we'll discuss some of the given code so that it all makes sense.

- -

Note that a certain amount of knowledge is assumed for these tutorials. If you haven't -completed the Hello, World tutorial, -please do so—it will teach you many things you should know about basic -Android development and Eclipse features. More specifically, you should know:

- -

Please, also notice that, in order to make these tutorials simple, some may -not convey the better Android coding practices. In particular, many of them -use hard-coded strings in the layout files—the better practice is to reference strings from -your strings.xml file.

-

With this knowledge, you're ready to begin, so take your pick.

- -
- - - - - - - - - -
-Spinner
- -
- - -
-ListView
- -
-
-GridView
- -
- -
-Gallery
- -
- -
-TabWidget
- -
- -
-MapView
- -
- -
-WebView
- -
- - - -

-There are plenty more Views and widgets available. See the {@link android.view.View} class -for more on View layouts, and the {@link android.widget widget package} -for more useful widgets. And for more raw code samples, visit the -Api Demos. -These can also be found offline, in /<sdk>/samples/ApiDemos.

-
- -- cgit v1.1