0

I am adding item in navigation view programmatically but I have to add itemTextColor programmatically

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    final Menu menu = navigationView.getMenu();
    for (int i = 1; i <= 3; i++) {
        menu.add("test" + i);
    }

When clicking on a particular item it's color change and remains the same color. For e.g: When a user opens the drawer and click on a particular item it changes color and remains(same color) still another item click.

Check screenshot

Darshan
  • 38
  • 10
  • you want set all item same color or different color for different item? – N J Jun 26 '17 at 12:29
  • When clicking on a particular item it's color change and remains the same color. For e.g: When a user opens the drawer and click on a particular item it changes color and remains(same color) still another item click. – Darshan Jun 26 '17 at 12:51
  • 1
    can you add images or screenshots of the cases along with the code of onclick section (if u have given somewhere)? Before Click and After Click, Expected and Actual colors. – pro_cheats Jun 26 '17 at 13:04
  • I have added Screenshot – Darshan Jun 26 '17 at 13:29
  • 1
    use this.............navigationView.setItemTextColor(); – Aniruddh Parihar Jun 26 '17 at 13:49
  • How can I do with navigationView.setItemTextColor(); ? NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);navigationView.setNavigationItemSelectedListener(this)final Menu menu = navigationView.getMenu();int[][] states = new int[][] {new int[] { android.R.attr.state_enabled}, new int[] { android.R.attr.state_pressed},new int[] { android.R.attr.state_checkable}};int[] colors = new int[] {Color.BLACK,Color.RED,Color.GREEN,Color.BLUE}; ColorStateList myList = new ColorStateList(states, colors); navigationView.setItemTextColor(myList); – Darshan Jun 27 '17 at 04:51

2 Answers2

4

I have found solutions for dynamic data add and menu item selector(On drawer click).

Here is the code:

 // MainActivity.java
 DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

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

        for (int i = 1; i <= 3; i++) {
            menu.add("Test " + i);
        }

 public boolean onNavigationItemSelected(MenuItem item) {

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        item.setCheckable(true);
        item.setChecked(true);
        item.setEnabled(true);
        return true;
    }

// Drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#097054" android:state_enabled="false" />
    <item android:color="#097054" android:state_checked="true" />
    <item android:color="#FFDE00" />
</selector>

// activity.xml

<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:background="#ffffff"
        android:fitsSystemWindows="true"
        app:itemBackground="@android:color/transparent"
        app:itemTextColor="@drawable/menu_text_color">


    </android.support.design.widget.NavigationView>
Darshan
  • 38
  • 10
0

You can use Spannable String for change Color of NavigationView Item.. I can achieve by using this code.

here item is your MenuItem.

SpannableString mNewTitle = new SpannableString(item.getTitle());
        mNewTitle.setSpan(new CustomTypefaceSpan("", font, 16), 0,
                mNewTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        mNewTitle.setSpan(new RelativeSizeSpan(1.2f), 0, mNewTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        mNewTitle.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.light_grey)), 0, mNewTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        item.setTitle(mNewTitle);
Akash
  • 961
  • 6
  • 15
  • Can you please refer me a link or any demo? I have tried this way but not able to do it. – Darshan Jun 27 '17 at 05:17
  • I have tried with [https://stackoverflow.com/questions/30594025/how-to-customize-item-background-and-item-text-color-inside-navigationview] [https://stackoverflow.com/questions/30967851/change-navigation-view-item-color-dynamicly-android] no possible to do. – Darshan Jun 27 '17 at 06:40
  • you are adding items programmatically. try to add items using Menu it will helps you. – Akash Jun 27 '17 at 09:36