Handling Click Events on Options Menu

Handling Click Events on Options Menu

MainCLASS

package com.example.malik.test;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {

RelativeLayout relativeLayout;

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

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.my_menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout);

switch (item.getItemId()){
case R.id.red:
relativeLayout.setBackgroundColor(getResources().getColor(android.R.color.holo_red_dark));
break;

case R.id.blue:
relativeLayout.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_dark));
break;

case R.id.purple:
relativeLayout.setBackgroundColor(getResources().getColor(android.R.color.holo_purple));
break;

case R.id.green:
relativeLayout.setBackgroundColor(getResources().getColor(android.R.color.holo_green_dark));
break;
}

return true;
}
}

 

MainXML

<?xml version=”1.0″ encoding=”utf-8″?>
<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: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=”com.example.malik.test.MainActivity”
android:id=”@+id/relative_layout”
>
<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Hello”
android:id=”@+id/button”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”87dp”
android:onClick=”setDate”
/>
</RelativeLayout>

 

my_menu XML

<?xml version=”1.0″ encoding=”utf-8″?>
<menu xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
>

<item android:id=”@+id/red”
android:title=”@string/title_red” ></item>

<item android:id=”@+id/blue”
android:title=”@string/title_blue” ></item>

<item android:id=”@+id/purple”
android:title=”@string/title_purple” ></item>

<item android:id=”@+id/green”
android:title=”@string/title_green” ></item>
</menu>

 

String XML

<resources>
<string name=”app_name”>Test</string>
<string name=”action_settings”>Settings</string>

<string name=”title_red”>Red</string>
<string name=”title_blue”>Blue</string>
<string name=”title_purple”>Purple</string>
<string name=”title_green”>Green</string>

</resources>

 

Output :

pic

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.