Options Menu in Android.

In this tutorial, we will see how to create Option Menu in Android. Very few steps we need to follow. We are creating a single menu with 4 menu items. Each menu item has a title to display the label. 




Step 1: Create a new project in Android Studio. ( Select File  New  Android Project and give     
                 activity name MenusActivity.java.)

Step 2:  Open menu.xml (located under res/menu folder) file and paste below code into it.

                <?xml version="1.0" encoding="utf-8"?>

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

                    <!-- Single menu item       Set id, icon and Title for each menu item    --> 

                        <item android:id="@+id/menu_save"

                          android:title="Save" /> 

                        <item android:id="@+id/menu_share"

                          android:title="Share" /> 

                        <item android:id="@+id/menu_delete"

                          android:title="Delete" />  

                        <item android:id="@+id/menu_preferences"

                          android:title="Preferences" />

               </menu> 



 Step 3:  Now open your Activity class file (MenusActivity.java) and paste the following code. In the following code, each menu item is identified by its ID in the switch case statement.

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuInflater;

import android.view.MenuItem;

import android.widget.Toast;

public class AndroidMenusActivity extends Activity 

{

    @Override

    public void onCreate(Bundle savedInstanceState)

   {       

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

    }

    // Initiating Menu XML file (menu.xml)

    @Override

    public boolean onCreateOptionsMenu(Menu menu)

    {

        MenuInflater menuInflater = getMenuInflater();

        menuInflater.inflate(R.layout.menu, menu);

        return true;

    }

       @Override

    public boolean onOptionsItemSelected(MenuItem item)

    {        

        switch (item.getItemId())

        { 

        case R.id.menu_save:

            Toast.makeText(AndroidMenusActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();

            return true; 

        case R.id.menu_share:

            Toast.makeText(AndroidMenusActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();

            return true; 

        case R.id.menu_delete:

            Toast.makeText(AndroidMenusActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();

            return true;

         case R.id.menu_preferences:

            Toast.makeText(AndroidMenusActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();

            return true; 

        default:

            return super.onOptionsItemSelected(item);

        }

    }    

}


        woohoo, just three steps, and we are done. 
        
        



        Thank You🙌😉.

Comments

  1. Simple and well explained... 👍

    ReplyDelete
  2. Simple Way To Learn And Great Explained 👌🙌

    ReplyDelete
  3. Very well explained and very much thank you for providing screen shots, it helped me a lot!!!

    ReplyDelete
  4. Nice n simple way to explained

    ReplyDelete
  5. Very interesting and informative

    ReplyDelete
  6. Step by step explanation has given... So nice 👍

    ReplyDelete
  7. Awesomely explained....... Each step was broken into simpler one

    ReplyDelete
  8. Well explained ����

    ReplyDelete
  9. Well explained and easy to understand

    ReplyDelete
  10. Explained in simple language. Great Going

    ReplyDelete
  11. Easiest way to learn..very well explained..✌️✌️

    ReplyDelete
  12. Good...👍 Easy to understand and in that way anyone can do it 👍👍👍

    ReplyDelete
  13. Explained in a simple way ��

    ReplyDelete
  14. Nice and simple. I am definitely going to try this.

    ReplyDelete
  15. Truly good for beginners.
    Thank you mam

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete

Post a Comment

Popular posts from this blog

Create an android app that demonstrates Activity Lifecycle and Instance State.

Program using Light Sensitive Sensors on Tinkercad