Lyusten Elder
Lyusten Elder

Reputation: 3303

Android: Drop Down Menu - who have simple example?

Who have simple example for Drop Down Menu by Button Click?

Need make list for installed programs and select for starting.

Menu listMenu = null;
listMenu.add("quasatron"); listMenu.add("magnetron"); listMenu.add("atarrilix");
onCreateOptionsMenu(listMenu);


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.popup_menu, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.food:
            makeToast("food","","","");
            return true;
        case R.id.other:
            makeToast("other","","","");
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

And XML file of popup_menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/food" android:title="Food" />
<item android:id="@+id/other" android:title="Other" />
</menu>

Upvotes: 0

Views: 7991

Answers (1)

Jaykumar Patel
Jaykumar Patel

Reputation: 27614

This example is create myself to when you select any item in dropdown (spinner) list at a moment image display rightside on base on select item. so this example help to you.

MainActivity.java

public class MainActivity extends Activity implements OnClickListener, OnItemSelectedListener{

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

        String[] technology = {"PHP", "Ruby", "Java", "SQL"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, technology);
        final Spinner spinnertech = (Spinner) findViewById(R.id.spinnertech);
        spinnertech.setAdapter(adapter);

        spinnertech.setOnItemSelectedListener(this);

    // Spinner Start.... 
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        int position = arg0.getSelectedItemPosition();
        ImageView ivtech = (ImageView) findViewById(R.id.imgtech);

        if(position == 0) {
            ivtech.setImageResource(R.drawable.php);
        } else if(position == 1) {
            ivtech.setImageResource(R.drawable.ruby);
        } else if(position == 2) {
            ivtech.setImageResource(R.drawable.java);
        } else if(position == 3) {
            ivtech.setImageResource(R.drawable.sql);
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {

    }
    // Spinner End.... 

}

main.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/TableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imgtech"
            android:layout_width="80dp"
            android:layout_height="60dp"
            android:layout_marginRight="10dp" />

        <Spinner
            android:id="@+id/spinnertech"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_weight="1" />

    </LinearLayout>

</TableLayout>

Upvotes: 3

Related Questions