/res/menu/option_menu.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/itemRed" android:title="@string/red" /> <item android:id="@+id/itemBlue" android:title="@string/blue" /> <item android:id="@+id/itemGreen" android:title="@string/green" /> <item android:id="@+id/itemPurple" android:title="@string/purple" /> </menu>
MainActivity.java
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) { getMenuInflater().inflate(R.menu.option_menu,menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { RelativeLayout relativeLayout= (RelativeLayout) findViewById(R.id.activity_main); switch (item.getItemId()) { case R.id.itemBlue: relativeLayout.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light)); break; case R.id.itemGreen: relativeLayout.setBackgroundColor(getResources().getColor(android.R.color.holo_green_light)); break; case R.id.itemPurple: relativeLayout.setBackgroundColor(getResources().getColor(android.R.color.holo_purple)); break; case R.id.itemRed: relativeLayout.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light)); break; } return super.onOptionsItemSelected(item); } }
References
https://github.com/mhdr/AndroidSamples/tree/master/021