Saturday, 7 May 2016

Menu bar in Android Studio

 Firstly Create new Project and Click app-----res-------menu------menu_main.xml  and add thes lines in menu_main.xml
<menu xmlns:android="http://schemas.
android.com/apk/res/android 
 xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools"tools:context=".MainActivity">

    <item 

          android:id="@+id/menu" 
          android:title="Red"/>
    <item 
          android:id="@+id/menu1" 
          android:title="Yellow"/>
<item  
          android:id="@+id/menu1"  
          android:title="Green"/> 
</menu>
 
Go To Content_main.xml
and Double click on Layout and Id Name Write mainLayout 
  
 then Go Main activity.java and add these lines in last Step
  @Override 
 public boolean onOptionsItemSelected(MenuItem item) {
 int selectedid = item.getItemId();
 RelativeLayout mainlayout = (RelativeLayout)
findViewById(R.id.mainLayout);

 switch (selectedid){
   case R.id.Menu1:
   mainlayout.setBackgroundColor(Color.RED);
    break;
   case R.id.Menu2:
   mainlayout.setBackgroundColor(Color.YELLOW);
    break;
   case R.id.Menu3:
    mainlayout.setBackgroundColor(Color.GREEN);
        }



   return super.onOptionsItemSelected(item);
    }
}
RESULT:-  
Click on ... then main layout show red and green you click red your main layout red see in image 3

 

 

FreeWebSubmission.com

No comments:

Post a Comment