Last active
October 4, 2016 15:16
-
-
Save ahmadmust8/c35d86871671e53c7d4ae088be850c23 to your computer and use it in GitHub Desktop.
#onOptionsItemSelected Method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ahmad.mustafa.menuoption; | |
import android.graphics.Color; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.widget.RelativeLayout; | |
public class MainActivity extends AppCompatActivity | |
{ | |
@Override | |
protected void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) | |
{ | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) | |
{ | |
RelativeLayout my_layout = (RelativeLayout) findViewById(R.id.content_main); | |
switch (item.getItemId()) | |
{ | |
case R.id.red_item: | |
if (item.isChecked()) | |
item.setChecked(false); | |
else | |
item.setChecked(true); | |
my_layout.setBackgroundColor(Color.RED); | |
return true; | |
case R.id.black_item: | |
if (item.isChecked()) | |
item.setChecked(false); | |
else | |
item.setChecked(true); | |
my_layout.setBackgroundColor(Color.BLACK); | |
return true; | |
case R.id.blue_item: | |
if (item.isChecked()) | |
item.setChecked(false); | |
else | |
item.setChecked(true); | |
my_layout.setBackgroundColor(Color.BLUE); | |
return true; | |
default: | |
return super.onOptionsItemSelected(item); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment