Search Tutorials

Saturday 21 September 2013

Android Menu example using Java

Simple and easy android tutorials to learn how to create menu in android using Java 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.

Android Menu example using Java

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rl"
    android:background="#143">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Menu Example using Java:-by coder hub"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
</RelativeLayout>

Java code is given below with description. Open main Java file and paste below code:


package coders.hub.com; //your package name

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

public class MainActivity extends Activity {

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

    //This function will call when we click on menu button
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
      //add(groupID,itemID,ItemPosition,ItemTitle)
      //setIcon(): To set icon in menu
      menu.add(0,0,3, "RED"); //.setIcon(R.drawable.icon);
      menu.add(0,1,0, "BLUE");
      menu.add(0,2,1, "GREEN");
      menu.add(0,3,2, "YELLOW");
      menu.add(0,4,8, "GRAY");
      menu.add(0,5,4, "PINK");
      menu.add(0,6,5, "WHITE");
      menu.add(0,7,7, "BLACK");
      //Maximum 6 items can display in default menu 
      //here setIcon will set icon on menu list but it will not display
      //because icon will not display in list view
      menu.add(0,8,6, "CLOSE"); //.setIcon(R.drawable.icon);
      return super.onCreateOptionsMenu(menu);
    }

    //This function will perform action when we click on menu item  
    public boolean onOptionsItemSelected(MenuItem item)
    {
      RelativeLayout rl=(RelativeLayout)findViewById(R.id.rl);
      switch(item.getItemId())
      {
   //Simply changing color of layout
      case 0: rl.setBackgroundColor(Color.RED); break;
      case 1: rl.setBackgroundColor(Color.BLUE); break;
      case 4: rl.setBackgroundColor(Color.GRAY); break;
      case 3: rl.setBackgroundColor(Color.YELLOW); break;
      case 5: rl.setBackgroundColor(Color.MAGENTA); break;
      case 6: rl.setBackgroundColor(Color.WHITE); break;
      case 2: rl.setBackgroundColor(Color.GREEN); break;
      case 8: finish(); break;
      default: rl.setBackgroundColor(Color.BLACK);  
      }
      return true;
    } 
}

Simple and easy code...if you have still any doubt than comment below. share with friends and care all developers.

Related Tutorials:-

Create Menu using XML

Access Call, Dialer Screen, Camera and Web pages

Open Second Activity using Intent

Make any column invisible in Table layout

Create a Dialog box

Send SMS without any Report

6 comments:

  1. how to make chat server please help me at brijeshsftiant@gmail.com

    ReplyDelete
    Replies
    1. Chat application code is give in this blog ...for java project, check java label and for android project, check android Label. :)

      Delete
  2. Hello. can you help me please with some info? I'm developing an android chat bot app with program ab. It must be an chat without internet connection, the program ab is the java server that will be integrated in asset android folder. My problem is with the android chat part. I don't have so much experience with android and I need some tips...I need to know what views to use exactly and what layout... it is like your chat exemple?

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. It is showing "It doesnot have main method!"

    ReplyDelete
  5. Thank You So Much Very Helpfull

    ReplyDelete

Back to Top