Android Introduction: GUI Menu
Android Introduction: GUI Menu
GUI Menu
1 Many thanks to Jun Bum Lim for his help with this tutorial.
Goal
<context menu>
2
Menu Composition
Sub1 Plus Sub2 Hi Hola Hello Long press in EditText
Home
<sub-menu>
Pre
Create the two TextViews and an EditText Create menu folder in res/ Create menu.xml in res/menu/ (New > Other > Android XML File) Create context_menu.xml in res/menu/
res/menu/menu.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menuItemPlus" android:title="@string/plus" android:icon="@drawable/plus" > Sub-menu items <menu> <item android:id="@+id/menuItemSub1" android:title="@string/sub1"></item> <item android:id="@+id/menuItemSub2" android:title="@string/sub2"></item> </menu> </item> <item android:icon="@drawable/home" android:id="@+id/menuItemHome" android:title="@string/home"></item> <item android:icon="@drawable/pre" android:id="@+id/menuItemPre" android:title="@string/pre"></item> <item android:icon="@drawable/next" android:id="@+id/menuItemNext" android:title="@string/next"></item> </menu>
res/menu/context.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menuItemHi" android:title="@string/hi_msg"></item> <item android:id="@+id/menuItemHola" android:title="@string/hola_msg"></item> <item android:id="@+id/menuItemHello" android:title="@string/hello_msg"></item> </menu>
res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Click Menu Button ! </string> <string name="app_name">Android Menu Example</string> <string <string <string <string name="plus">Plus</string> name="pre">Pre</string> name="next">Next</string> name="home">Home</string>
<string name="hi_msg">Hi !</string> <string name="hola_msg">Hola !</string> <string name="hello_msg">Hello !</string> </resources>
icons
icons
Inflating a menu resource (menu.xml) by adding onCreateOptionsMenu(Menu menu) in the main Activity. Menu items in menu.xml will appear when the user touches the MENU button
public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; }
Response to menu click events by overriding onOptionsItemSelected(Menu menu) in the main Activity.
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menuItemPlus: Toast.makeText(this, "Plus Button Clicked !", Toast.LENGTH_SHORT).show(); Log.i(TAG,"menuItemPlus"); return true; : : case R.id.menuItemNext: Toast.makeText(this, "Next Button Clicked !", Toast.LENGTH_SHORT).show(); Log.i(TAG,"menuItemNext"); return true; } return false; }
10
By calling registerForContextMenu() and passing it a View (an EditText in this example) you assign it a context menu. When this View (EditText) receives a long-press, it displays a context menu.
private EditText mOutEditText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mOutEditText = (EditText) findViewById(R.id.editText); registerForContextMenu(mOutEditText); } : :
@2011 Mihail L. Sichitiu 11
@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.context_menu, menu); }
12
By overriding your activity's menu selection callback method for context menu , onContextItemSelected().
@Override public boolean onContextItemSelected(MenuItem item) { Log.i(TAG,"ContextItem selected"); AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); switch (item.getItemId()) { case R.id.menuItemHi: mOutEditText.setText( this.getResources().getText( R.string.hi_msg) ); return true; case R.id.menuItemHola: : : default: return super.onContextItemSelected(item); } }
13