How do you rate this blog

Sunday, November 6, 2011

Menus in Android – Part1: Option Menu


Menus are basically the commands which the users are allowed perform in an activity through keys, button or gestures.
There are 2 types of Menus in android they are
1) Options Menu –This menu is generally visible to the user after he presses he menu button and is basically used for navigation inside the application.
2) Context Menu – This is basically invoked based on touch. The selected operation can be related to the current or in other activity. You can include radio buttons; check boxes etc in this option.
In this post, I have created an app which displays Options Menu which will have 3 items in it called Menu1, Menu2 and Menu3. On selecting anyone of the options the user can go to other activities.

                I have created a new project called OptionsMenu which has a package called optionsmenu in which there are 4 classes OptionsMenuActivity which has code for  creating all the menu items; Menu1,Menu2 and Menu3 which defines activity . I have created 3 other new layouts called menu1,menu2 and menu3.
Add a textview to Main.xml and add the title to this textview. The main.xml file will look as follows.



Now go to OptionsMenuActivity file and add the below 2 functions
First add all the menu items which you want in the application using the function add(menugroupdID, ItemId, OrderID, text) as shown below –
public boolean onCreateOptionsMenu(Menu m){
                super.onCreateOptionsMenu(m);
                m.add(0, 0, 0, "Menu 1");
                m.add(0, 1, 0, "Menu 2");
                m.add(0, 2, 0, "Menu 3");
                return true;
}

Once you have added the menu items, you can associate actions for each menu item. This can be done using the onOptionsItemSelected as shown below  –
public boolean onOptionsItemSelected(MenuItem mi){
                super.onOptionsItemSelected(mi);
                TextView tv=(TextView) findViewById(R.id.textView2);
                switch(mi.getItemId()){
                case 0: Intent Menu1=new Intent(this,Menu1.class);
                                                this.startActivity(Menu1);
                                                break;
                case 1: Intent Menu2=new Intent(this,Menu2.class);
                                                this.startActivity(Menu2);
                                                break;
                case 2: Intent Menu3=new Intent(this,Menu3.class);
                                                this.startActivity(Menu3);
                                                break;
                default:tv.setText("You didnt select any");
                                                break;
                }
                return true;
}

Finally your OptionsmenuActivity file will look as shown below – 



Now that we have defined our main screen activity, we have to design and configure other activities Menu1, Menu2 and Menu3  to go back to original screen once the user clicks on the button. To do this we just need to use setOnClickListener  button as shown below
Button menu1_b=(Button) findViewById(R.id.button100);
                                                menu1_b.setOnClickListener(new View.OnClickListener() {
                                                @Override
                                                                public void onClick(View v) {
                                                                                Intent mainScreen=new Intent();
                                                                                setResult(RESULT_OK,mainScreen);
                                                                                finish();
                                                                }
                                                });
                                }

Given below is how menu1 laoyout looks like (Menu2 and Menu3 will be similar to Menu1)



Given below is how Menu1.java a looks like (Menu2 and Menu3 will be similar to Menu1) 



Now we need to add all the activities in the AndroidManifest.xml, so add the below code to the file
<activity android:name="Menu1"></activity>                   
<activity android:name="Menu2"></activity>
<activity android:name="Menu3"></activity>

Now Right click on the project and say run as Android Application. Once the android application loads on your emulator click on the menu button and all the menu items should appear as shown below.



Click on an any of the menu items and it should take you to the respective screen. 




In the next part will tell how to create Context Menu.

No comments:

Post a Comment