Wednesday, December 26, 2012

Android CustomeMenu example

we can make custom menu in Aid after following just few steps.
1) create a project with name CustomMenu and make a main activity as CustomMenu.java. copy the given below code to it.


package jitesh.example.custommenu;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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 CustomMenu extends ListActivity {
MenuDialog customMenuDialog;
private static final String[] items = { "a", "b", "b",
"d", "e", "f", "g", "h", "i",
"j", "k", "l", "m", "n", "o",
"p", "q", "r" };
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, 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));
}
}

2) make  custom_menu layout under layout folder of res, copy the given xml layout as follows


<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/first"
            android:background="@drawable/background"
            android:contentDescription="@string/image_des" />
  <ImageButton
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:src="@drawable/second"
     android:background="@drawable/background"
     android:contentDescription="@string/image_des"/>
  <ImageButton
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:src="@drawable/third"
     android:background="@drawable/background"
     android:contentDescription="@string/image_des" />
  <ImageButton
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:src="@drawable/fourth"
     android:background="@drawable/background"
     android:contentDescription="@string/image_des" />
</LinearLayout>

3)now copy these images to yours drawable folder









4) make colors.xml file under res\values and copy these lines


<resources>

    <color name="offcolor">#222222</color>
    <color name="oncolor">#C44141</color>

</resources>

5) make a file under re->drawable with name background.xml and copy these lines to it


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_focused="true" android:drawable="@color/oncolor"/>
   <item android:state_pressed="true" android:drawable="@color/oncolor" />
   <item android:drawable="@color/offcolor" />
</selector>

6) make files ffirst.xml, second.xml, third.xml, fourth.xml under  res->drawable

and copy these lines respective to each files

first.xml


<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
           android:drawable="@drawable/gifth" /> <!-- pressed -->
     <item android:state_focused="true"
           android:drawable="@drawable/gifth" /> <!-- focused -->
     <item android:drawable="@drawable/gift" /> <!-- default -->
 </selector>




second.xml


<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
           android:drawable="@drawable/logouth" /> <!-- pressed -->
     <item android:state_focused="true"
           android:drawable="@drawable/logouth" /> <!-- focused -->
     <item android:drawable="@drawable/logout" /> <!-- default -->
 </selector>

third.xml


<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
           android:drawable="@drawable/settingsh" /> <!-- pressed -->
     <item android:state_focused="true"
           android:drawable="@drawable/settingsh" /> <!-- focused -->
     <item android:drawable="@drawable/settings" /> <!-- default -->
 </selector>

fourth.xml


<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
           android:drawable="@drawable/visitush" /> <!-- pressed -->
     <item android:state_focused="true"
           android:drawable="@drawable/visitush" /> <!-- focused -->
     <item android:drawable="@drawable/visitus" /> <!-- default -->
 </selector>





No comments:

Post a Comment