First write the XML file under the Res/mene folder My_menu
<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;
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Menuinflater inflater = new Menuinflater (this);
Inflater.inflate (R.menu.my_menu, menu);
return Super.oncreateoptionsmenu (menu);
}
@Override
public boolean onoptionsitemselected (MenuItem item) {
if ( Item.ischeckable ()) {
item.setchecked (true);
}
switch (Item.getitemid ()) {
case r.id.font_10:
Edit.settextsize (10*2);
break;
case R.id.font_12:
edit.settextsize (12*2);
break;
case r.id.font_14:
edit.settextsize (14*2);
break;
case r.id.font_16:
edit.settextsize (16*2);
break;
case r.id.font_18:
edit.settextsize (18*2);
break;
}
return true;
}
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Edit = (TextView) Findviewbyid (R.ID.TEXTVIEW1);
Registerforcontextmenu (edit);
}
}
Android with XML File Settings menu