0

I want to change the color of the text of an item that groups a menu.

The xml file:

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

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_mi_cuenta"
            android:icon="@mipmap/ic_mi_cuenta"
            android:title="@string/mi_cuenta" />
    </group>

    <item android:title="@string/herramientas">
        <menu>
            <item
                android:id="@+id/nav_configuracion"
                android:icon="@mipmap/ic_config"
                android:title="@string/configuraci_n" />
        </menu>
    </item>

</menu>

I wnat to change text color of:

<item android:title="@string/herramientas">

enter image description here

As you can see, Herramientas comes out in black and as the background is also black it barely looks. I can not find any property to change it. The color of the item 'Mis datos' and 'Configuración' change it programmatically as follows from an external json file of a server:

colorElegido = getParseColor(json.getString("colorMenuLateral"));
navigationView.setBackgroundColor(colorElegido);

colorElegido = getParseColor(json.getString("colorFuenteMenuLateral"));
ColorStateList colorList = getColorList(colorElegido);

navigationView.setItemTextColor(colorList);

2 Answers2

3

The following code will workout,

menu.xml

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

        <group android:checkableBehavior="single">
            <item
                android:id="@+id/nav_mi_cuenta"
                android:icon="@mipmap/ic_mi_cuenta"
                android:title="@string/mi_cuenta" />
        </group>

        <item 
 android:id="@+id/herramientas"
android:title="@string/herramientas">
            <menu>
                <item
                    android:id="@+id/nav_configuracion"
                    android:icon="@mipmap/ic_config"
                    android:title="@string/configuraci_n" />
            </menu>
        </item>

    </menu>

Add this in your styles,

<style name="TextAppearance44">
    <item name="android:textColor">#FF0000</item>
    <item name="android:textSize">20sp</item>
</style>
<style name="NavigationDrawerStyle">
    <item name="android:textSize">16sp</item><!-- text size in menu-->
    <item name="android:textColor">#880ACE0A</item>
    <item name="itemTextColor">#880ACE0A</item>
</style>

And in your activity,

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        Menu menu = navigationView.getMenu();

        MenuItem tools= menu.findItem(R.id.herramientas);
        SpannableString s = new SpannableString(tools.getTitle());
        s.setSpan(new TextAppearanceSpan(this, R.style.TextAppearance44), 0, s.length(), 0);
        tools.setTitle(s);
        navigationView.setNavigationItemSelectedListener(this);

Change according to you,

And other text color change you can use like in your xml file,

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    style="@style/NavigationDrawerStyle"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />
Sunisha Guptan
  • 1,555
  • 17
  • 44
1

to make a custom popup make an ImageView in toolbar

private ImageView setting;

public void menuOptions() {
    PopupMenu popup = new PopupMenu(MainActivity.this, setting);
    // Inflating the Popup using xml file
    popup.getMenuInflater()
            .inflate(R.menu.menu_main, popup.getMenu());

    //registering popup with OnMenuItemClickListener
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {

            switch (item.getItemId()) {
                case R.id.button1:

                    return (true);
                case R.id.button2:
                    // block contacts

                    return (true);
            }

            return true;
        }
    });

    popup.show(); //showing popup menu
}

on Button click call above function

 menuOptions();

in the layout you inflate menu_main.xml, make your desired view.