How to create Option Menu Example – On Hardware Click

Posted on .
option_menu

Hello all , hope you doing great ! Today I am going to show you how to code on click of option Menu. This Option Menu is used for showing extra items or delete something and for searching.How lets dig inside –

Just you have to do is code in onOptionsItemSelected(Menu item) override method.
Download code

Lets start with designing part.open your activity_main.xml file and paste the below code , This xml file is your common file you can do your normal stuff here like –

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textSize="20sp" />

</RelativeLayout>

Now open your menu folder in your IDE and open main.xml file and paste the below code there-

main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item  android:id="@+id/item1"
        android:title="cupcake"/>

	<item  android:id="@+id/item2"
        android:title="lollipop"/>
   
	<item  android:id="@+id/item3"
        android:title="kitkat"/>

    
</menu>

Now open your MainActivity.java file and paste the below code there and you have to work on onOptionItemSelected() method in which i have taken a Toast on switch case statements . You can code with your desires –

MainActivity.java

package androidarena.optionmenu;
// copyright androidarena.co.in
import androidarena.optionmenu.R;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item1:
              Toast.makeText(getApplicationContext(),"cupcake",Toast.LENGTH_LONG).show();
            return true;   

            case R.id.item2:
                Toast.makeText(getApplicationContext(),"lollipop",Toast.LENGTH_LONG).show();
              return true;   
            
            case R.id.item3:
                Toast.makeText(getApplicationContext(),"kitkat",Toast.LENGTH_LONG).show();
              return true;   

              default:
                return super.onOptionsItemSelected(item);
        }
    }
}

Now its time to Run your project in Android by pressing f11 .

Output –

option_menu

option_menu example

Stay tuned for easy examples .For more queries comment on Disqus section :p

Leave a Reply

Your email address will not be published. Required fields are marked *