• Tidak ada hasil yang ditemukan

Using the SpinnerView to Display an item at a Timetry it out

Dalam dokumen Beginning Android Application Development (Halaman 185-191)

1 .

Using Eclipse, create an Android project and name it as BasicViews6.

codefile BasicViews6.zip available for download at Wrox.com

2 .

Modify the main.xml file located in the res/layout folder as shown here:

<?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”

​​​​>

<Spinner

​​​​android:id=”@+id/spinner1”

​​​​android:layout_width=”wrap_content”

​​​​android:layout_height=”wrap_content”

​​​​android:drawSelectorOnTop=”true” />

</LinearLayout>

3 .

Add the following lines in bold to the strings.xml file located in the res/values folder:

<?xml​version=”1.0”​encoding=”utf-8”?>

<resources>

​​​​<string​name=”hello”>Hello​World,​MainActivity!</string>

​​​​<string​name=”app_name”>BasicViews6</string>

​​​​<string-array name=”presidents_array”>

​​​​​​​​<item>Dwight D. Eisenhower</item>

​​​​​​​​<item>John F. Kennedy</item>

​​​​​​​​<item>Lyndon B. Johnson</item>

​​​​​​​​<item>Richard Nixon</item>

​​​​​​​​<item>Gerald Ford</item>

​​​​​​​​<item>Jimmy Carter</item>

​​​​​​​​<item>Ronald Reagan</item>

​​​​​​​​<item>George H. W. Bush</item>

​​​​​​​​<item>Bill Clinton</item>

​​​​​​​​<item>George W. Bush</item>

​​​​​​​​<item>Barack Obama</item>

​​​​</string-array>

</resources>

4 .

Add the following statements in bold to the MainActivity.java file:

package​net.learn2develop.BasicViews6;

import​android.app.Activity;

import​android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.ArrayAdapter;

import android.widget.Spinner;

import android.widget.Toast;

public​class​MainActivity​extends​Activity​{

​​​​String[] presidents;

​​​​/**​Called​when​the​activity​is​first​created.​*/

​​​​@Override

​​​​public​void​onCreate(Bundle​savedInstanceState)​{

​​​​​​​​super.onCreate(savedInstanceState);

​​​​​​​​setContentView(R.layout.main);

​​​​​​​​presidents =

​​​​​​​​​​​​getResources().getStringArray(R.array.presidents_array);

​​​​​​​​Spinner s1 = (Spinner) findViewById(R.id.spinner1);

​​​​​​​​ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,

​​​​​​​​​​​​android.R.layout.simple_spinner_item, presidents);

​​​​​​​​s1.setAdapter(adapter);

​​​​​​​​s1.setOnItemSelectedListener(new OnItemSelectedListener()

​​​​​​​​{

​​​​​​​​​​​​@Override

​​​​​​​​​​​​public void onItemSelected(AdapterView<?> arg0, View arg1,

​​​​​​​​​​​​int arg2, long arg3)

​​​​​​​​​​​​{

​​​​​​​​​​​​​​​​int index = arg0.getSelectedItemPosition();

​​​​​​​​​​​​​​​​Toast.makeText(getBaseContext(),

​​​​​​​​​​​​​​​​​​​​“You have selected item : “ + presidents[index],

​​​​​​​​​​​​​​​​​​​​Toast.LENGTH_SHORT).show();

​​​​​​​​​​​​}

​​​​​​​​​​​​@Override

​​​​​​​​​​​​public void onNothingSelected(AdapterView<?> arg0) {}

​​​​​​​​});

​​​​}

}

5 .

Press F11 to debug the application on the Android Emulator. Click on the SpinnerView and you will see a pop-up displaying the list of presidents’ names (see Figure 4-18). Clicking on an item will display a message showing you the item selected.

How It Works

The preceding example works very much like the ListView. One additional method you need to implement is the onNothingSelected() method. This method is fired when the user presses the Back button, which dismisses the list of items displayed. In this case, nothing is selected and you do not need to do anything.

Figure 4-18

Instead of displaying the items in the ArrayAdapter as a simple list, you can also display them using radio buttons. To do so, modify the second parameter in the constructor of the ArrayAdapter class:

​​​​​​​​ArrayAdapter<String>​adapter​=​new​ArrayAdapter<String>(this,

​​​​​​​​​​​​android.R.layout.simple_spinner_dropdown_item,​presidents);

This will cause the items to be displayed as a list of radio buttons (see Figure 4-19).

Figure 4-19

SummAry

This chapter provided a brief look at some of the commonly used views in an Android application.

While it is not possible to exhaustively examine each view in detail, the views you learned about here should provide a good foundation for designing your Android application’s user interface, regardless of its requirements.

exerciSeS

1 .

How do you programmatically determine whether a RadioButton is checked?

2 .

How do you access the string resource stored in the strings.xml file?

3 .

Write the code snippet to obtain the current date . Answers to the Exercises can be found in Appendix C.

WhAt you leArned in thiS chApter

topic key conceptS

textview <TextView

​​​​android:layout_width=”fill_parent”​

​​​​android:layout_height=”wrap_content”

​​​​android:text=”@string/hello”

​​​​/>

Button <Button​android:id=”@+id/btnSave”

​​​​android:layout_width=”fill_parent”​

​​​​android:layout_height=”wrap_content”

​​​​android:text=”Save”​/>

imageButton <ImageButton​android:id=”@+id/btnImg1”

​​​​android:layout_width=”fill_parent”​

​​​​android:layout_height=”wrap_content”

​​​​android:src=”@drawable/icon”​/>

edittext <EditText​android:id=”@+id/txtName”

​​​​android:layout_width=”fill_parent”

​​​​android:layout_height=”wrap_content”​/>

checkBox <CheckBox​android:id=”@+id/chkAutosave”

​​​​android:layout_width=”fill_parent”

​​​​android:layout_height=”wrap_content”

​​​​android:text=”Autosave”​/>

radiogroup and radioButton <RadioGroup​android:id=”@+id/rdbGp1”

​​​​android:layout_width=”fill_parent”

​​​​android:layout_height=”wrap_content”

​​​​android:orientation=”vertical”​>

​​​​<RadioButton​android:id=”@+id/rdb1”

​​​​​​​​android:layout_width=”fill_parent”

​​​​​​​​android:layout_height=”wrap_content”

​​​​​​​​android:text=”Option​1”​/>

​​​​<RadioButton​android:id=”@+id/rdb2”

​​​​​​​​android:layout_width=”fill_parent”

​​​​​​​​android:layout_height=”wrap_content”

​​​​​​​​android:text=”Option​2”​/>

​​​​</RadioGroup>

toggleButton <ToggleButton​android:id=”@+id/toggle1”

​​​​android:layout_width=”wrap_content”

​​​​android:layout_height=”wrap_content”​/>​

progressBar <ProgressBar​android:id=”@+id/progressbar”

​​​​android:layout_width=”wrap_content”​

​​​​android:layout_height=”wrap_content”​/>

AutocompletetextBox <AutoCompleteTextView​android:id=”@+id/txtCountries”

​​​​android:layout_width=”fill_parent”

​​​​android:layout_height=”wrap_content”​/>

continues

topic key conceptS

timepicker <TimePicker​android:id=”@+id/timePicker”

​​​​android:layout_width=”wrap_content”

​​​​android:layout_height=”wrap_content”​/>

datepicker <DatePicker​android:id=”@+id/datePicker”

​​​​android:layout_width=”wrap_content”

​​​​android:layout_height=”wrap_content”​/>

Spinner <Spinner​android:id=”@+id/spinner1”

​​​​android:layout_width=”wrap_content”

​​​​android:layout_height=”wrap_content”

​​​​android:drawSelectorOnTop=”true”​/>

(continued)

Displaying Pictures and

Dalam dokumen Beginning Android Application Development (Halaman 185-191)