Prerequisite
If you not understand this lecture Option Menu in android studio.You should also read my previous lectures Creating a time picker in android and Toggle button in android studio . After reading these lecture you will also able to create Option Menu of your app.
Overview
Menus are used in applications to handle some common functionality around the application.The options menu is the primary collection of menu items for an activity.You can also set the color for the option menu and its main items.
Introduction
In this lecture we will describe how can create Option menu in android and also describe how can handle click events in option menu.And also describe contextual Menu. why we can use it.After Reading this lecture you will understand Option menu in android And i hope you will create Option menu of your application.
Description of option menu
Android menu is used to show multiple options for the application. when the button is pressed. Menu are show on that activity.Menu consists items and sub item.You can add, insert, and remove option menu.In this lecture we will describe how can create a option menu in android .Option menu is available in API 3.0 and above .But lower 3.0 API option menu button is available instead of option menu.So we create a project in android studio and make sure that minimum SDK is 3.0 this is why because we add a menu bar in Activity. Menu are available in res folder you can expand the res folder and click on menu folder and expand the menu folder. You can see menu. XML file is available in menu folder.This statement is used to show menu.
menuInflater.inflate(R.menu.my_menu,menu);
For more detail about android menu and option click on Option menu in android. First of all you will create a new project in android studio. Every project contain two file first is java class file and second is layout XML file .For all menu types, Android provides a standard XML format to define menu items. Instead of building a menu in your activity’s code, you should define a menu and all its items in an XML.For menus layout you will create a separate layout . how can create menu layout detail is provides step by step.
Step 1=Add menu<-Right click the menu folder-><- select new option->and click on-<Menu resource folder->
Step2=Step1<-Writename->ok
After that you will check it new menu.XML file will add in menu folder. All the menu items will add in new menu.xml file.you can add items and assign id to each item and also assign title of each item. how can add title of each item. you will open a string.XML file and add string resource in string .XML file.
Code that are write in string resource.xml file
<resources> <string name="app_name">OptionMenu</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="title_search">Search</string> <string name="title_share">Share</string> </resources>
Code that are write in menu.xml file
For all menu types, Android provides a standard XML format to define menu items. Instead of building a menu in your activity code you should define a menu and all its items in an XML.
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/id_search" android:title="@string/title_search" android:showAsAction="ifRoom" ></item> <item android:id="@+id/id_share" android:title="@string/title_share" ></item> </menu>
Code that are write MainActivity. java class file.
mport android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater menuInflater=getMenuInflater(); menuInflater.inflate(R.menu.my_menu,menu); return true; } }
Run the application android Date Picker
Handling click Events of option menu
For handling on click method of the menu item override the onOptionsItemSelected(MenuItem item)
method in your activity. This method invoke whenever user click/select any menu item. For handling different actions for each element get the id of the selected menu item using getItemId()
method and perform action for each menu item.Each activity can has its own menu. You can put different menu items for each activity and handle it differently. Some activity cannot have any menu.
Android 3.0 provides facility to define on-click on a menu .XML file.Using on click method you can change the background color of your activity.For selecting a option in option menu you can move from one activity to other. Method that are used must have public and only single parameter is used.when the system calls this method, it passes the menu item that are selected.if your application contain multipal activity in which many activity have same option menu then you will use these method.
onOptionsItemSelecte
onCreateOptionsMenu
public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.new_page: newpage(); return true; case R.id.help: showdelete(); return true; default: return super.onOptionsItemSelected(item); } }
Creating Contextual Menu
Contextual menus used to select one option menu.Context Menu displays hierarchical data as a multi level menu in a popup.It provides option of items and user can select one option at a time.
Handling click Events of floating contextual menu
A menu display as a floating list of menu items such as dialog box. when user click on button or long press the floating contextual menu display option.So we will describe how can handle click events in floating contextual menu.The example that we describe consists three option delete share and help when user click or select option dialog box appear.First of all you will create a new project in android studio. Every project contain two file first is java class file and second is layout XML file .For all menu types, Android provides a standard XML format to define menu items. Instead of building a menu in your activity’s code, you should define a menu and all its items in an XML.For menu layout you will create a separate layout. how can create a menu layout you will see step1 and step2.
Code that are write in string resource.xml file
<resources> <string name="app_name">contextualEvents</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="title_delete">Delete</string> <string name="title_share">Share</string> <string name="title_help">Help</string> <string-array name="android_version"> <item>Froyo</item> <item>Kitkat</item> <item>Lollipop</item> <item>Gingerbread</item> <item>jelly Beans</item> </string-array> </resources>
Code that are write in menu.xml file
For all menu types, Android provides a standard XML format to define menu items. Instead of building a menu in your activity code you should define a menu and all its items in an XML.
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/id_delete" android:title="@string/title_delete" ></item> <item android:id="@+id/id_share" android:title="@string/title_share" ></item> <item android:id="@+id/id_help" android:title="@string/title_help" ></item> </menu>
Code that are write in Layout.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <ListView android:id="@+id/list_view" android:layout_width="wrap_content" android:layout_height="wrap_content"> </ListView> </RelativeLayout>
Now you may open the Graphical layout editor to preview the User Interface you created of List view in android.
Code that are write MainActivity. java class file.
public class MainActivity extends ActionBarActivity { ListView listView; String[] android_version; ArrayAdapter<String> adapter; ArrayList<String> arrayList = new ArrayList<String>(); List selections=new ArrayList(); int count =0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.list_view); registerForContextMenu(listView); android_version = getResources().getStringArray(R.array.android_version); for (String item : android_version) { arrayList.add(item); } adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.row_layout, R.id.row_item, arrayList); listView.setAdapter(adapter); listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE); listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() { @Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { if (checked) { selections.add(arrayList.get(position)); count++; } else{ selections.remove(arrayList.get(position)); count--; mode.setTitle(count+"selected"); } } @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.my_contest,menu); return false; } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { if (item.getItemId()==R.id.id_delete){ for (Object Item : selections){ String ITem=item.toString(); arrayList.remove(ITem); adapter.notifyDataSetChanged(); mode.finish(); } } return false; } @Override public void onDestroyActionMode(ActionMode mode) { } }); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater menuInflater=getMenuInflater(); menuInflater.inflate(R.menu.my_contest,menu); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); switch (item.getItemId()) { case R.id.id_delete: arrayList.remove(info.position); adapter.notifyDataSetChanged(); return true; default: return super.onContextItemSelected(item); } @Override public boolean onCreateOptionsMenu(Menu menu) { Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(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. int id = item.getItemId(); noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Run the application
when you long press in a list view then a dialog box is appear show in a below pic
Thank you for reading this lecture Hope you got the idea.
The post Option Menu in android studio appeared first on The Master World.