简体   繁体   中英

BottomNavigationView original icon color for checked and not checked different

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;
    }

};

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>

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;
            }
        })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM