12 OPTION MENU
Algorithm:
Step 1) Create a new Android Studio Project
Step 2)Goto Project Structure and open activity_main.xml file
Step 3) Create menu Android resource Directory and inside menu folder create menu resource file.
Step 4) open menu resource file and create the list of items you want to display an options menu as options.
Step 5) Implement the following methods
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
|
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//code
return super.onOptionsItemSelected(item);
}
}
|
Step 6) Run the application from the menu bar select Run and Click on Run App option.
Program:
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
<android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
File: menu_main.xml
<item android:id="@+id/item1" android:title="Item 1"/>
<item android:id="@+id/item2" android:title="Item 2"/>
<item android:id="@+id/item3" android:title="Item 3" app:showAsAction="withText"/>
</menu>
MainActivity.java
package com.example.basha.optionmenu;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@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.menu_main, menu);
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId();
switch (id){
case R.id.item1: Toast.makeText(getApplicationContext(),"Item 1
Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.item2: Toast.makeText(getApplicationContext(),"Item 2
return true;
case R.id.item2: Toast.makeText(getApplicationContext(),"Item 2
Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.item3: Toast.makeText(getApplicationContext(),"Item 3
return true;
case R.id.item3: Toast.makeText(getApplicationContext(),"Item 3
Selected",Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
OUTPUT:
Comments
Post a Comment