Adding Your Own Examples to Api Demos

The ApiDemos application is a handy sandbox for your own testing, and adding a new menu entry and Activity to it is quite easy. But remember that whenever you upgrade your API, all of your changes will be lost. Don't add code to the ApiDemo that you might want to save after an upgrade. It really is just a sandbox for quick tests.

With that caveat in mind, this section shows you how to add a new menu and screen to the ApiDemos application. We'll do that by adding a new ToastTest Activity with a matching toast_test layout. We'll then hook them into the ApiDemos application by adding them to the AndroidManifest.xml file.

First, create a file named toast_test.xml in the res/layouts directory and add the following content to lay out the widgets:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="wrap_content " android:layout_height="wrap_content"

xmlns:android=""> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Guess my favorite color:" /> <RadioGroup android:id="@+id/RadioGroup01" android:layout_below="@id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:id="@+id/redButton" android:layout_width="wrap_content"

android:layout_height="wrap_content" android:text="Red" /> <RadioButton android:id="@+id/greenButton" android:layout_width="wrap_content"

android:layout_height="wrap_content" android:text="Green" /> <RadioButton android:id="@+id/blueButton" android:layout_width="wrap_content"

android:layout_height="wrap_content" android:text="Blue" /> </RadioGroup> </RelativeLayout>

This layout creates a RelativeLayout layout manager named RelativeLayout01, specifying up a TextView and a RadioGroup. The TextView presents the user with the text "Guess My Favorite Color" while the RadioGroup, named RadioGroup01, contains three RadioButton widgets: redButton, greenButton, and blueButton. They have the text "Red", "Green", and "Blue", respectively.

Next, create the view/ file. It simply responds to clicks from the layout: package;

//Need the following import to get access to the app resources, since this //class is in a sub-package. import;

import; import android.os.Bundle; import android.view.View; import android.widget.RadioButton; import android.widget.Toast;

public class ToastTest extends Activity{

Adding Your Own Examples to ApiDemos | 85

/** Called when the activity is first created. */ ^Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.toast_test);©

final RadioButton redButton = (RadioButton) findViewById(;© redButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){

Toast.makeText(ToastTest.this, "Ooooh, red", Toast.LENGTH_SHORT).show();

Here are some of the highlights of the code:

O Calls the method from the superclass, which is the Activity class itself.

© Sets the ContentView to use the toast_test layout, defined in the layout file you created earlier.

© Creates one of the RadioButton widgets, also going to the layout file.

O Sets up the OnClickListener of the redButton to show a piece of "Toast" that says "Ooooh, red" for a short period of time. Chapter 12 covers graphics programming on Android.

Like toast in a toaster, this text pops up when activated. This technique can be quite handy for debug code.

Finally, add a new activity element to the AndroidManifest.xml file:

<activity android:name=".view.ToastTest" android:label="Views/ToastTest" > <intent-filter>

<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.SAMPLE_CODE" /> </intent-filter> </activity>

This activity element should be added right after the TextSwitcher1 demo.


0 0

Post a comment