How to dynamically change option menu item text color in Android

In this android programming source code example, we are going to dynamically change option menu item text color in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to dynamically change option menu item text color in Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ededed">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Option Menu"
        android:gravity="center"
        android:textSize="20sp"/>

</RelativeLayout>

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.bluapp.androidview.R;

public class MenuActivity7 extends AppCompatActivity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();inflater.inflate(R.menu.option_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch(item.getItemId()){
            //
        }

        return true;
    }
}

menu\option_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/action_home"
        android:title="@string/home"
        android:orderInCategory="100"
        app:showAsAction="never" />

    <item
        android:id="@+id/action_blog"
        android:title="@string/blog"
        android:orderInCategory="100"
        app:showAsAction="never" />

    <item
        android:id="@+id/action_App"
        android:title="@string/App"
        android:orderInCategory="100"
        app:showAsAction="never" />
</menu>

values\styles.xml

<style name="CustomMenu7" parent="@android:style/TextAppearance.Widget.IconMenu.Item">
        <item name="android:textColor">@color/colorMenu</item>
    </style>

    <style name="MenuActivity7Theme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:itemTextAppearance">@style/CustomMenu7</item>
    </style>

AndroidManifest.xml

<activity
            android:name=".MainActivity"
            android:theme="@style/MenuActivity7Theme" />

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment