Monday, March 31, 2014

How to create menu in Android using xml file

This code will show how to create menu in android, how to perform action when we click on menu item and how to set icon in menu item.

Just create a new project and give "rl" id to layout or paste below code in layout->main xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".TestMenu"
    android:id="@+id/rl">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

 
Now open menu folder and create test_menu xml file and paste below code


    <item android:id="@+id/red"
        android:title="RED"
        android:icon="@drawable/lion">
       
    </item>
      <item android:id="@+id/blue"
        android:title="BLUE">
       
    </item>
      <item android:id="@+id/green"
        android:title="GREEN">
       
    </item>
      <item android:id="@+id/yellow"
        android:title="YELLOW">
       
    </item>
      <item android:id="@+id/black"
        android:title="BLACK">
      </item>
           <item android:id="@+id/gray"
        android:title="GREY">
        </item>
      
           <item android:id="@+id/pink"
        android:title="PINK">
      </item>
         <item android:id="@+id/white"
        android:title="WHITE">
      </item>
         <item android:id="@+id/close"
        android:title="CLOSE">
      </item>
</menu>

Now open Main java file and paste below code

package test.menutest; //your package name

importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.graphics.Color;
importandroid.view.Menu;
importandroid.view.MenuItem;
importandroid.widget.RelativeLayout;

public class TestMenu extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

 //This function will call when we click on menu button
    @Override
    public booleanonCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.test_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
//This function will perform action when we click on menu item

    public booleanonOptionsItemSelected(MenuItem item)
    {
      RelativeLayout rl=(RelativeLayout)findViewById(R.id.rl);
      switch(item.getItemId())
      {
      case R.id.red:rl.setBackgroundColor(Color.RED); break;
      case R.id.blue:rl.setBackgroundColor(Color.BLUE); break;
      case R.id.gray:rl.setBackgroundColor(Color.GRAY); break;
      case R.id.yellow:rl.setBackgroundColor(Color.YELLOW); break;
      case R.id.pink:rl.setBackgroundColor(Color.MAGENTA); break;
      case R.id.white:rl.setBackgroundColor(Color.WHITE); break;
      case R.id.green:rl.setBackgroundColor(Color.GREEN); break;
      case R.id.close:finish(); break;
      default:rl.setBackgroundColor(Color.BLACK);
     
      }
     
      return true;
    }  
}

Run code and Enjoy...

Related Posts by Categories

0 comments:

Post a Comment