How to Create Overflow Menus in Android
Hi friends, in this tutorial I am going to explain How to create Overflow Menus in Android .Overflow Menu is a menu that is accessible to the user from the device display and allows the developer to include other application options beyond those included in the user interface of the application. The location of the Overflow Menu is dependent upon the version of Android that is running on the device.With the Android 4.0 release and later the Overflow Menu button is located in the top right hand corner in the action toolbar. Overflow Menus is a mechanism for offering additional choices to the user beyond the view components that are present in the user interface layout.
Let’s see how to work with Overflow Menus.
Prerequisites:
- JDK 7.0 or Above
- Android Studio 2.0
Steps to follow:
- Create a New Android Studio project name MenuExample.
- Select minimum API level 8 so it can run 100% of the Android Device Available in the play store .Select Empty Activity and your Activity name is MenuExampleActivity click next.
- Give your layout xml name is activity_menu_example and menu resource xml is menu_menu_example and click finish .
- Now from app -> res -> menu open the menu_menu_example file
- Inside menu_menu_example write the following code.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.prosen.menuexample.MenuExampleActivity">
<group android:checkableBehavior="single">
<item
android:id="@+id/menu_edit"
android:orderInCategory="1"
app:showAsAction="never"
android:title="@string/red_string"/>
<item
android:id="@+id/menu_smartGroup"
android:orderInCategory="2"
app:showAsAction="never"
android:title="@string/green_string"/>
<item
android:id="@+id/menu_LockApps"
android:orderInCategory="3"
app:showAsAction="never"
android:title="@string/yellow_string"/>
<item
android:id="@+id/menu_Uninstall"
android:orderInCategory="4"
app:showAsAction="never"
android:title="@string/blue_string"/>
</group>
</menu>
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.prosen.menuexample.MenuExampleActivity">
<group android:checkableBehavior="single">
<item
android:id="@+id/menu_edit"
android:orderInCategory="1"
app:showAsAction="never"
android:title="@string/red_string"/>
<item
android:id="@+id/menu_smartGroup"
android:orderInCategory="2"
app:showAsAction="never"
android:title="@string/green_string"/>
<item
android:id="@+id/menu_LockApps"
android:orderInCategory="3"
app:showAsAction="never"
android:title="@string/yellow_string"/>
<item
android:id="@+id/menu_Uninstall"
android:orderInCategory="4"
app:showAsAction="never"
android:title="@string/blue_string"/>
</group>
</menu>
6. Locate and double click on the app -> res -> values -> strings.xml file. Within the file,
add new string resources for the color names as referenced by the menu items:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MenuExample</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="red_string">Edit</string>
<string name="green_string">Smart Group</string>
<string name="yellow_string">Lock Apps</string>
<string name="blue_string">Uninstall</string>
</resources>
<resources>
<string name="app_name">MenuExample</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="red_string">Edit</string>
<string name="green_string">Smart Group</string>
<string name="yellow_string">Lock Apps</string>
<string name="blue_string">Uninstall</string>
</resources>
7. Now inside MenuExampleActivity write the following code .
public class MenuExampleActivityextends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_example);
}
@Override
public booleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_menu_example, menu);
return true;
}
@Override
public booleanonOptionsItemSelected(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.
RelativeLayoutmainLayout =
(RelativeLayout) findViewById(R.id.menu_view);
switch (item.getItemId()) {
case R.id.menu_red:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
mainLayout.setBackgroundColor(android.graphics.Color.RED);
return true;
case R.id.menu_green:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
mainLayout.setBackgroundColor(android.graphics.Color.GREEN);
return true;
case R.id.menu_yellow:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
mainLayout.setBackgroundColor(android.graphics.Color.YELLOW);
return true;
case R.id.menu_blue:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
mainLayout.setBackgroundColor(android.graphics.Color.BLUE);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_example);
}
@Override
public booleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_menu_example, menu);
return true;
}
@Override
public booleanonOptionsItemSelected(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.
RelativeLayoutmainLayout =
(RelativeLayout) findViewById(R.id.menu_view);
switch (item.getItemId()) {
case R.id.menu_red:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
mainLayout.setBackgroundColor(android.graphics.Color.RED);
return true;
case R.id.menu_green:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
mainLayout.setBackgroundColor(android.graphics.Color.GREEN);
return true;
case R.id.menu_yellow:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
mainLayout.setBackgroundColor(android.graphics.Color.YELLOW);
return true;
case R.id.menu_blue:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
mainLayout.setBackgroundColor(android.graphics.Color.BLUE);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

Comments ( 2 )

Roger Belk:
This code needs work, Please check your work before posting. If anyone need the corrected code hit me up.
This code needs work, Please check your work before posting. If anyone need the corrected code hit me up.

Roger Belk:
This code needs work, please check your work before you post it.
This code needs work, please check your work before you post it.
Subscribe Latest Information
Categories
Most Popular Posts
How to Withdraw Money from ATM Machine 7steps 1178798 Views
How to Create Chat Application in Android Studio 151642 Views
How to Create a Shopping Cart Application in Android 114502 Views
You May Like Also
How to create your first Android Game 15747 Views
How to Make a PopUp Window in Android 55025 Views
How to make a Simple Quiz App in Android 72249 Views