Download presentation
Presentation is loading. Please wait.
Published byBernadette Bell Modified over 9 years ago
1
@2011 Mihail L. Sichitiu1 Android Introduction GUI Menu Many thanks to Jun Bum Lim for his help with this tutorial.
2
@2011 Mihail L. Sichitiu2 Goal Create an application that supports options/sub/context menus Automatically fill “Hi!” in the EditText Plus menu will also open a sub-menu Display messages when a menu clicked
3
@2011 Mihail L. Sichitiu3 Menu Composition Plus Home Pre Next Sub1 Sub2 Hi Hola Hello Long press in EditText
4
@2011 Mihail L. Sichitiu4 Create HelloMenu Project 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/
5
@2011 Mihail L. Sichitiu5 res/menu/menu.xml <menu xmlns:android="http://schemas.android.com/apk/res/android"> Sub-menu items Option menu items Define Option menu and sub-menu
6
@2011 Mihail L. Sichitiu6 res/menu/context.xml <menu xmlns:android="http://schemas.android.com/apk/res/android"> Define context menu for EditText
7
@2011 Mihail L. Sichitiu7 res/values/strings.xml Click Menu Button ! Android Menu Example Plus Pre Next Home Sub1 Sub2 Hi ! Hola ! Hello ! Define constant strings used in the application
8
@2011 Mihail L. Sichitiu8 icons Place icons used in menu.xml in res/drawable/ icons Download icons at: http://www4.ncsu.edu/~mlsichit/UCAB/resources.html
9
@2011 Mihail L. Sichitiu9 Inflating a option menu resource 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; } public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; }
10
@2011 Mihail L. Sichitiu10 Response to user action 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; } @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; }
11
@2011 Mihail L. Sichitiu11 Register View for a context menu 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. public class AndroidMenuExampleActivity extends Activity { private EditText mOutEditText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mOutEditText = (EditText) findViewById(R.id.editText); registerForContextMenu(mOutEditText); } : public class AndroidMenuExampleActivity extends Activity { private EditText mOutEditText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mOutEditText = (EditText) findViewById(R.id.editText); registerForContextMenu(mOutEditText); } :
12
@2011 Mihail L. Sichitiu12 Define context menu’s appearance By overriding the activity's context menu create callback method, onCreateContextMenu(). @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); } @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); }
13
@2011 Mihail L. Sichitiu13 Define context menu’s behavior 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); } @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); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.