1

BottomNavigationView Original icon color I have found it here that it is possible to retain the same icon color. But is it possible to keep it only when the icon is selected and keep a different greyish color for a non-selected tab? This is my code for changing the icon color.

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:color="@color/colorSecondary"
    android:state_checked="true"/>
<item
    android:color="#bebebe"
    android:state_checked="false"/>

Here in state_checked I tried giving @null but it just makes the icons pinkish. I want to retain the icon color when it is checked. Is it possible?

Note: I have also tried using bNavigationView.setIconTintList(null); which changes all the icon color to original.

public class MainActivity extends AppCompatActivity {
 Toolbar toolbar;
 BottomNavigationView bNavigationView;
 BottomNavigationView.OnNavigationItemSelectedListener 
 mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        switch (item.getItemId()) {
            case R.id.navigation_account:
// AS gives me an error here saying setItemIconTintList cannot be applied to (int,null)
                bNavigationView.setItemIconTintList(0,null);
                fragmentTransaction.replace(R.id.content,new AccountFragment()).commit();
                break;
            case R.id.navigation_technical:
                fragmentTransaction.replace(R.id.content,new TechnicalEventsFragment()).commit();
                break;
            case R.id.navigation_corporate:
                fragmentTransaction.replace(R.id.content,new CorporateEventsFragment()).commit();
                break;
            case R.id.navigation_cultural:
                fragmentTransaction.replace(R.id.content,new CulturalEventsFragment()).commit();
                break;
            case R.id.navigation_dashboard:
                fragmentTransaction.replace(R.id.content,new DashboardFragment()).commit();
                break;
        }

        return true;
    }

};
sanjeev
  • 1,664
  • 19
  • 35

2 Answers2

0

try this

 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true" android:color="@color/colorSecondary"/>
        <item android:color="#bebebe"  />
    </selector>
Kapil Parmar
  • 881
  • 8
  • 19
0

I know this is old question but for whom they want to retain the icon color as use this method and set it to null setItemIconTintList(ColorStateList tint) – Set the tint which is applied to menu icons.

this is the simplest way try it out and let me know

    bNavigationView = (BottomNavigationView) 


 findViewById(R.id.bottom_navigation);
 bNavigationView.setItemIconTintList(null);

This is the most elegant super fast way to do that.

notice :set setItemIconTintList to null will remove the whole tint color for icons but if u need this effect for specific icon u can do that inside onNavigationItemSelected

  bNavigationView.setOnNavigationItemSelectedListener(new 
    BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {


                switch (item.getItemId()) {

                    case R.id.iconOne:
               bNavigationView.setIconTintList(0,null);

       replaceFragment(FirstFragment.newInstance(),"FragmentOne");                            
                    break;
                    case R.id.mySpecificIcon:
                //setIconTintList taking to params the first
                //is the position of the icon wich is array starts from     
                //0    

                bNavigationView.setIconTintList(1,null);

              replaceFragment(FirstFragment.newInstance(),"Fragment2");


                        // mainViewPager.setCurrentItem(3);
                        break;


                }
                return true;
            }
        })
moahmed ayed
  • 636
  • 7
  • 10
  • Hello Mohamad.. I have already tried that.. it changes all the colors of the icons to default. I want the selected icon to be displayed with the original color and the remaining to be displayed with grey#bebebe – sanjeev Dec 22 '17 at 17:53
  • see the updated answer Sanjeev I tested it for u and its working if it didn't with you plz let me know – moahmed ayed Dec 24 '17 at 07:07
  • Hello Mohammad. Android Studio gives me an error when I use bNavigationView.setIconTintList(0,null); – sanjeev Dec 25 '17 at 08:52
  • setIconTintList cannot be applied to (int,null) is the error. Please refer to the question to check my code. – sanjeev Dec 25 '17 at 08:56
  • print the error here when using bNavigationView.setIconTintList(0,null); – moahmed ayed Dec 25 '17 at 23:12
  • Error:(28, 36) error: method setItemIconTintList in class BottomNavigationView cannot be applied to given types; required: ColorStateList found: int, reason: actual and formal argument lists differ in length – sanjeev Dec 26 '17 at 10:49
  • Don't use setItemIconTintList. use bNavigationView.setIconTintList(1,null); instead if u read the my updated answer u will find that I use bNavigationView.setIconTintList(1,null); not setItemIconTintList – moahmed ayed Dec 26 '17 at 10:58