Technical Library
Dropdown for Android™
A Dropdown List UI element enables users to choose between multiple items in a list. These UI elements have three states: default, pressed, and disabled. The current item is checked and the user is allowed to scroll through the list.
Download UI elements for Android.
Creation
You need to create a new com.att.widgets.lib.dropdown.DropDown object and add it to your view or layout.
Full layout code
<?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is a sample of 3 Dropdowns" android:paddingBottom="10dp" android:textStyle="bold"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingBottom="10dp"> <TextView android:layout_width="100dip" android:layout_height="fill_parent" android:gravity="center_vertical" android:text="Andriod Os:"/> <com.att.widgets.lib.dropdown.DropDown android:id="@+id/dropdown" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:id="@+id/dropdown_2_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingBottom="10dp"> <TextView android:layout_width="100dip" android:layout_height="fill_parent" android:gravity="center_vertical" android:text="Phones:"/> </LinearLayout> <TextView android:id="@+id/dropdown_text_1" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/dropdown_text_2" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="10dip"> <TextView android:layout_width="100dip" android:layout_height="fill_parent" android:gravity="center_vertical" android:text="Disabled Dropdown:"/> <com.att.widgets.lib.dropdown.DropDown android:id="@+id/dropdown_disabled" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout>
Activity
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.RelativeLayout; import android.widget.TextView; import com.att.widgets.lib.dropdown.DropDown; public class DropDownActivity extends Activity implements OnClickListener { private String[]array_1 = {"Gingerbread", "Froyo", "Cupcake", "Donut", "Eclair"}; private String[]array_2 = {"Milestone", "Galaxy", "Nexus One", "MotoBlur"}; private DropDown dropDown_1, dropDown_2; private TextView textView_1, textView_2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ViewGroup v = (ViewGroup)findViewById(R.id.dropdown_2_view); textView_1 = (TextView)findViewById(R.id.dropdown_text_1); textView_2 = (TextView)findViewById(R.id.dropdown_text_2); dropDown_1 = ((DropDown)findViewById(R.id.dropdown)); dropDown_1.setFocusable(true); dropDown_2 = new DropDown(this); RelativeLayout.LayoutParams ll = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); dropDown_2.setLayoutParams(ll); v.addView(dropDown_2); dropDown_1.setData(array_1); dropDown_2.setData(array_2); dropDown_1.setFieldsNumber(4); dropDown_2.setFieldsNumber(3); ((DropDown)findViewById(R.id.dropdown_disabled)).setEnabled(false); ((DropDown)findViewById(R.id.dropdown_disabled)).setDisplayText("Disabled Dropdown"); //listeners dropDown_1.setOnClickListener(this); dropDown_2.setOnClickListener(this); } public void onClick(View v) { if(v == dropDown_1){ textView_1.setText("Selected Android Os: " + dropDown_1.getSelectedText()); }else if(v == dropDown_2){ textView_2.setText("Selected Phone: " +dropDown_2.getSelectedText()); } } }
Explanation
The first Dropdown is defined in Xml with the id "dropdown", and focused by code
First Drop Down Layout
<com.att.widgets.lib.dropdown.DropDown android:id="@+id/dropdown" android:layout_width="fill_parent" android:layout_height="wrap_content" />
First Drop Down Activity Code
dropDown_1 = ((DropDown)findViewById(R.id.dropdown)); dropDown_1.setFocusable(true); dropDown_1.setData(array_1); dropDown_1.setOnClickListener(this); // Number of fields visible on the drop down list dropDown_1.setFieldsNumber(4);
Second Drop Down Layout
And the second DropDown is generated from the following code. Only LinearLayout is defined in the xml
<LinearLayout android:id="@+id/dropdown_2_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> </LinearLayout>
Second Drop Down Activity Code
ViewGroup v = (ViewGroup)findViewById(R.id.dropdown_2_view); dropDown_2 = new DropDown(this); RelativeLayout.LayoutParams ll = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); dropDown_2.setLayoutParams(ll); v.addView(dropDown_2); dropDown_2.setFocusable(true); dropDown_2.setData(array_2); dropDown_2.setOnClickListener(this); //number of fields visible on the drop down list dropDown_2.setFieldsNumber(3);
You can choose how you want to do it.
Click listeners
You should add a click listener to handle the selection event
dropDown.setOnClickListener(this);
and implement the OnClickListener in the current class. For example:
public class DropDownActivity extends Activity implements OnClickListener ... public void onClick(View v) { ... }
Android is a trademark of Google Inc.