Android Option Menu Tutorial

Option Menus are the primary menus of android. They can be used for settings, search, delete item etc.

Step 1.

Create new application with any name from file menu option.

File → New Project → Application name → select API → Add Blank Activity → Activity Name → finish.

Step 2.

Open menu_main.xml file and Create menu item as need in application.

Go to app →res →menu →menu_main.xml file.

option menu tutorial

Step 3.

Import Package and write following coding in MainActivity.java class file.

	
    import android.widget.Toast;
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        switch(item.getItemId())
        {
            case R.id.file:
                Toast.makeText(MainActivity.this,"File Option Selected",Toast.LENGTH_LONG).show();
                break;
            case R.id.cut:
                Toast.makeText(MainActivity.this,"Cut Option Selected",Toast.LENGTH_LONG).show();
                break;
            case R.id.copy:
                Toast.makeText(MainActivity.this,"Copy Option Selected",Toast.LENGTH_LONG).show();
                break;
            case R.id.paste:
                Toast.makeText(MainActivity.this,"Paste Option Selected",Toast.LENGTH_LONG).show();
                break;
        }
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
     }
  }

Step 4. Final Output

option menu tutorial

Post Your Comment