Nadeem
Nadeem

Reputation: 227

Creating popup list and selecting from the list

I want to make a popup list like above in that list there will be different distances and when i click on a distance in that distance's description will be shown from the following list. please help atleast tell how to make popup list. For Android Thanks

enter image description here

Upvotes: 0

Views: 944

Answers (2)

TJ Thind
TJ Thind

Reputation: 784

I think you're referring to a Spinner. Its very easy to use. If you're only using a specific set of values for the drop down list you can use string array resource as the source of data for the Spinner. Define in XML under the values directory an array like this. Put as many items in as you need for the Spinners drop down list.

<string-array name="distances">
    <item>1 Mile</item>
    <item>2 Miles</item>
</string-array>

In a layout file define a Spinner widget

<Spinner
    android:id="@+id/spinnerDistances"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

And then in whatever activity you use that layout in:

Spinner spinner = (Spinner) findViewById(R.id.spinnerDistances);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.distances, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);

Don't forget to define an event listener for the spinner so the program can do something when a user selects something on the Spinner.

EDIT: To do something when an item is selected from the spinner you need to override OnItemSelectedListener.onItemSelected() and set the Spinner to use that listener with Spinner.setOnItemSelectedListener(). You can do this by making your class implement OnItemSelectedListener and implementing the required methods or something like:

mySpinner.setOnItemSelectedListener(new OnItemSelectedListener()
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)         
    {
    // TODO Auto-generated method stub
}

    @Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
);

Upvotes: 2

Abhinav Singh Maurya
Abhinav Singh Maurya

Reputation: 3313

Well you can create activity in android with

<activity
            android:name=".youractivityname"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Dialog" />

And add list view in this activity with your desired item in the list. The theme dialog creates your activity as a dialog and show as its a kind of popup Please let me know if this helps you

Upvotes: 0

Related Questions