Recipe Creating a Drop Down Menu

A drop-down menu is called a spinner. It is a widget defined in a normal screen layout such as the one shown in Listing 4.19.

Listing 4.19 res/layout/spinner.xml

<?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"> <Spinner android:id="@+id/spinner"

android:prompt="@string/ocean_prompt" android:layout_width="wrap_content" android:layout_height="wrap_content" />

</LinearLayout>

The title of the drop-down menu can be specified with the android:prompt attribute. It needs to be defined in a strings.xml file, for example:

<string name="ocean_prompt">Choose your favorite ocean</string>

The spinner also needs a separate layout defined for the drop-down menu appearance, such as Listing 4.20 for the spinner_entry.xml.

Listing 4.20 res/layout/spinner_entry.xml

<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android"

android:gravity="center" android:textColor="#000" android:textSize="40sp" android:layout_width="fill_parent" android:layout_height="wrap_content"> </TextView>

Note the spinner entry layout is not limited to text, but can include images or any object supported in layouts.

The activity to call the spinner needs to declare an Adapter to fill the drop-down menu with the view from the spinner entry layout file. An example of such an activity is shown in Listing 4.21.

Listing 4.21 src/com/cookbook/layout_widgets/SpinnerExample.java package com.cookbook.layout_widgets;

import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.Spinner;

public class SpinnerExample extends Activity { private static final String[] oceans = { "Pacific", "Atlantic", "Indian", "Arctic", "Southern" };

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.spinner);

Spinner favoriteOcean = (Spinner) findViewByld(R.id.spinner);

ArrayAdapter<String> mAdapter = new

ArrayAdapter<String>(this, R.layout.spinner_entry, oceans); mAdapter.setDropDownViewResource(R.layout.spinner_entry); favoriteOcean.setAdapter(mAdapter);

In the previous example, the spinner entries are defined by the string array oceans[], which is passed to the ArrayAdapter constructor. This implementation assumes the spinner entries do not change during run-time. To specify a more general case where spinner entries can be added or manipulated, mAdapter needs to be built using its add()

method. The bold part of code in the onCreate() method would then become the following:

Spinner favoriteOcean = (Spinner) findViewById(R.id.spinner); ArrayAdapter<String> mAdapter = new

ArrayAdapter<String>(this, R.layout.spinner_entry); mAdapter.setDropDownViewResource(R.layout.spinner_entry); for(int idx=0; idx<oceans.length; idx++)

mAdapter.add(oceans[idx]); favoriteOcean.setAdapter(mAdapter);

This ArrayAdapter allows the add(), remove(), and clear() methods to change the selection list during run-time, and getView() to improve performance speed by reusing layout views for each spinner entry.

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Responses

  • claudia
    How to create dropdown manually in android?
    7 years ago

Post a comment