Tuesday 22 January 2013

Android Custom Options Menu Button[Tutorial]

Certainly, there may be times your application is required to make a move beyond showing a typical menu once the user presses the actual MENU button





Creating Layout for Option Menu with Default Icon


res/layout/custom_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:gravity="bottom">
    <ImageButton
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@android:drawable/ic_menu_add"
        android:contentDescription="@string/image_des" />
   <ImageButton
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:src="@android:drawable/ic_menu_call"
       android:contentDescription="@string/image_des"/>
   <ImageButton
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:src="@android:drawable/ic_menu_camera"
       android:contentDescription="@string/image_des" />
   <ImageButton
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:src="@android:drawable/ic_menu_compass"
       android:contentDescription="@string/image_des" />
</LinearLayout>




Default Android Icon — Options Menu Without SetTitle() in Java Code



Default Android Icon — Options Menu Without SetTitle() in Java Code


Creating Layout for Option Menu with Custom Icon


res/layout/custom_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:gravity="bottom">
    <ImageButton
        android:id="@+id/sendBtn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/gmail"
            android:contentDescription="@string/image_des" />
   <ImageButton
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:src="@drawable/android"
       android:contentDescription="@string/image_des"/>
   <ImageButton
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:src="@drawable/filer"
       android:contentDescription="@string/image_des" />
   <ImageButton
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:src="@drawable/settings"
       android:contentDescription="@string/image_des" />
</LinearLayout>



Java Code To Configure Options Menu


src/CustomMenuActivity.java

package com.vimaltuts.android.custommenu;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
public class CustomMenuActivityDemo extends ListActivity {
 MenuDialog customMenuDialog;
 private static final String[] items = { "Android", "Bluetooth", "Chrome",
   "Docs", "Email", "Facebook", "Google", "Hungary", "Iphone",
   "Korea", "Machintosh", "Nokia", "Orkut", "Picasa", "Singapore",
   "Talk", "Windows", "Youtube" };
 private ArrayList item = null;
 @Override
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  initAdapter();
  registerForContextMenu(getListView());
 }
 private class MenuDialog extends AlertDialog {
  public MenuDialog(Context context) {
   super(context);
   setTitle("Android Option Menu Example");
   View cus_menu = getLayoutInflater().inflate(R.layout.custom_menu_layout, null);
   setView(cus_menu);
  }
  @Override
  public boolean onKeyUp(int keyCode, KeyEvent event) {
   if (keyCode == KeyEvent.KEYCODE_MENU) {
    dismiss();
    return true;
   }
   return super.onKeyUp(keyCode, event);
  }
 }
 @Override
 public boolean onKeyUp(int keyCode, KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_MENU) {
   if (customMenuDialog == null) {
    customMenuDialog = new MenuDialog(this);
   }
   customMenuDialog.show();
   return true;
  }
  return super.onKeyUp(keyCode, event);
 }
 private void initAdapter() {
  item = new ArrayList();
  for (String str : items) {
   item.add(str);
  }
  setListAdapter(new ArrayAdapter(this,
    android.R.layout.simple_list_item_1, item));
 }
}

Output Screen of the Android Custom Option Menu Example




Options Menu Without SetTitle()



                                                 Options Menu Without SetTitle()

So far we have play with the Android Custom Options Menu through this Android Tutorial for Beginners : Custom Options Menu Button Example Tutorial. On the next tutorial we gonna see about the Android ActionBar Menu

Share This!


No comments:

Post a Comment

We are Happy to help you so we are given reply as soon as possible.
Thank You.

Increase Traffic By Click

Contact Form

Name

Email *

Message *

Powered By Blogger · Designed By Priyesh Marvi

Receive All Free Updates Via Facebook.