Wednesday, November 14, 2012

How To Show The Options Menu In Android

If you have a wide target audience, it is important to take note that there is a big possibility that your audience will have a wide range of screen sizes. Android’s design accommodates these different screen sizes and that’s why it implemented the Options Menu. This menu comes up when the user presses on the menu button on a mobile phone. If your mobile application has quite a number of functionalities where you need to create buttons then designing the UI (User Interface) of your application can be quite a challenge. And there are a number of options such as laying these buttons in a horizontal manner, enabling your layout scrollable, etc. Another way is off course to create just a menu button on the Activity. Although it can quite redundant because there is a physical menu button on most mobile phones, having a menu button on the Activity itself will just make it user friendly – especially those who aren’t aware of a menu button.

So to add a menu button perform the following steps:

1. Add a button view to your layout as declared in main.xml. (Partial code shown)

<Button    android:id="@+id/btnMenu"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:text="@string/menu_text"  />

2. Declare the text value of your button in your resources file (string.xml).

<?xml version="1.0" encoding="utf-8"?><resources>  <string name="menu_text">Menu</string></resources>

3. And in your Activity.java file add the following codes:

// -- code to run when button is clicked --Button btnMenu = (Button) findViewById(R.id.btnMenu); btnMenu.setOnClickListener(new View.OnClickListener() { 		@Override		public void onClick(View view) {			openOptionsMenu();		}});

4. Run the program. When the Menu button is clicked, the options menu will appear.


Source : htmlpress[dot]net

0 comments:

Post a Comment