PopupMenu represents a pop-up menu. He will specify a popup popupmenu on the component, by default PopupMenu will appear below or above the component, PopupMenu can add multiple menu items and can be added to the menu item submenu,
First write the XML file under Res/menu My_menu.xml
<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item
android:icon= "@drawable/ic_launcher"
android:title= "Fonts" >
<menu>
<!--define a single menu item--
<group android:checkablebehavior= "single" >
<item
Android:id= "@+id/font_10"
Android:title= "Font No. 10th"/>
<item
Android:id= "@+id/font_12"
Android:title= "Font No. 12th"/>
<item
Android:id= "@+id/font_14"
Android:title= "Font No. 14th"/>
<item
Android:id= "@+id/font_16"
Android:title= "Font No. 16th"/>
<item
Android:id= "@+id/font_18"
Android:title= "Font No. 18th"/>
</group>
</menu>
</item>
<!--define a common menu item--
<item
android:icon= "@drawable/FY_D_KF"
android:title= "normal Menu"/>
<item
android:icon= "@drawable/fy_d_kf2"
android:title= "COLOR" >
<menu>
<!--define a single menu item--
<group>
<item
Android:id= "@+id/red"
android:title= "Red"/>
<item
Android:id= "@+id/blue"
android:title= "Blue"/>
<item
Android:id= "@+id/green"
android:title= "Green"/>
</group>
</menu>
</item>
</menu>
The main code in the activity is as follows
public class Mainactivity extends Activity {
Private TextView edit;
Private PopupMenu menu;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Edit = (TextView) Findviewbyid (R.ID.TEXTVIEW1);
Edit.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View arg0) {
Clickpopumne (arg0);
}
});
}
public void Clickpopumne (View v) {
menu = new PopupMenu (this, v);
Getmenuinflater (). Inflate (R.menu.my_menu, Menu.getmenu ());
Menu.setonmenuitemclicklistener (New Onmenuitemclicklistener () {
@Override
public boolean Onmenuitemclick (MenuItem arg0) {
Switch (Arg0.getitemid ()) {
Case R.ID.FONT_10:
Menu.dismiss ();
Break
Default
Toast.maketext (Mainactivity.this, "" +arg0.gettitle (), Toast.length_short). Show ();
}
return true;
}
});
Menu.show ();
}
}
Use of the Android PopupMenu pop-up menu