Denoteone
Denoteone

Reputation: 4055

pop up select list for android

I am trying to get away from using 5 buttons when I would like to have one when pressed pops up a window that allows the user to select where they want to go.

Example: button "Country"

 <Button
android:id="@+id/countrySelect"
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="@string/backhome"
android:layout_x="8px"
android:layout_y="21px"
>
</Button>

and when pressed it would pop up a list of countrys to select from: Something like-

countrySelect.setOnClickListener(new OnClickListener() {         
    @Override
    public void onClick(View view){

           //POP UP SELECT MENU WHEN SELECTED START A NEW INTENT
           Intent myIntent = new Intent(view.getContext(), ***SELECT MENU CONTROLS***.class);
           startActivityForResult(myIntent, 0);

        }

 });

Sorry this probably is easy fix but I am not having much luck when I researched it.

Upvotes: 1

Views: 23275

Answers (4)

Asaf Pinhassi
Asaf Pinhassi

Reputation: 15573

If you want a popup menu like a spinner but with a button, you can use PopupMenu

see this example

Upvotes: 0

user790431
user790431

Reputation:

You can add spinner in your layout.xml :

<Spinner 
android:id="@+id/areaspinner"
android:layout_width="150dip"
android:layout_height="40dip"
android:drawSelectorOnTop="true"
android:padding="5dip"
android:paddingLeft="10dip"/>  

Now in Activity.java :

areaspinner = (Spinner) findViewById(R.id.areaspinner);
    ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, array);  //array you are populating  
    adapter2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    areaspinner.setAdapter(adapter2);
        areaspinner.setSelection(Integer.parseInt(strarea));  

Now you can get gtghe selected value from the spinner by :

int ipos=areaspinner.getSelectedItemPosition();
String str=array[iPos];  

Good luck.

Upvotes: 7

Tushar
Tushar

Reputation: 1272

Why don't you use Spinner or Single choice alert dialog for the same. http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog

Upvotes: 2

denis.solonenko
denis.solonenko

Reputation: 11775

You could use AlertDialog for that. It can show a list of items and react when user taps on any of them http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog

Upvotes: 17

Related Questions