Alan
Alan

Reputation: 3

How can I change the menu title in navigation drawer programmatically

Im trying to change the color of the menu title in a navigation drawer programmatically, I've managed to change the color of the items but now I want to change the color of the title. enter image description here

I've created all the items dynamically from a data base, in the image I show what it is that I want to change

for(Actividad actividad: actividades) {
                Log.d("PRUEBA", String.valueOf(actividad.get_id()));
                //this is where I create the menu title
                SubMenu menuGroup = menu.addSubMenu(actividad.get_id(), actividad.get_id(), Menu.NONE, actividad.getNombre());
                SpannableString s = new SpannableString(menuGroup.toString());
                s.setSpan(new ForegroundColorSpan(Color.WHITE), 0, s.length(), 0);
                for (Hijo hijo : actividad.getHijos()) {
                    //this is where I create the items
                    menuGroup.add(hijo.get_id(),hijo.get_id(),Menu.NONE,hijo.getNombre());
                }
            }

Any ideas on how can I do it?

Upvotes: 0

Views: 501

Answers (1)

tahsinRupam
tahsinRupam

Reputation: 6396

Replace this :

 s.setSpan(new ForegroundColorSpan(Color.WHITE), 0, s.length(), 0);

With:

s.setSpan(new TextAppearanceSpan(this, R.style.MyStyle), 0, s.length(), 0);

Define your style in style.xml

<style name="MyStyle">
        <item name="android:textColor">#FFFFFF</item>
 </style>

Hope this helps.

Upvotes: 0

Related Questions