Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the icon color selected on bottom navigation bar in android studio

When I select an item in bottom navigation bar in android studio, background item selected is equal to primarycolor in values->colors.xml . and now I want to change this color which is not to same the primarycolor. how do i can to change it?

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {
        Fragment fragment;
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    fragment = new HomeFragment();
                    loadFragment(fragment);
                    return true;
                case R.id.navigation_addpost:
                    fragment = new AddFragment();
                    loadFragment(fragment);
                    return true;
                case R.id.navigation_notifications:
//                    mTextMessage.setText(R.string.title_notifications);
                    return true;
                case R.id.navigation_profile:
                    fragment = new ProfileFragment();
                    loadFragment(fragment);
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        loadFragment(new HomeFragment());
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        navigation.setItemTextColor(ColorStateList.valueOf(Color.RED));
    }
like image 491
Hasan Cheraghi Avatar asked Jul 29 '18 11:07

Hasan Cheraghi


People also ask

How do I change the color of my bottom navigation bar?

If you are using the BottomNavigationView, the solution could be easy. You just need to create a selector as a ColorStateList, then assign the selector to the "itemIconTint" attribute of the BottomNavigationView.

How to create bottom navigation bar in Android Studio?

Steps for Creating Bottom Navigation Bar. Step 1: Create a new Android Studio project. To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language. Step 2: Adding the dependency to the build.gradle(:app) file

Is it possible to change the icons and color of navigation bar?

Hi, is it possible to change the icons and color of Android bottom navigation bar on phone or tablet ? If yes, how can I do it ? Settings > Display > Navigation Bar. You can also change the style of navigation bar by choosing other theme. Advanced customization of navigation bar is possible with some apps, that you need to download.

How to change the selected tab icon color in bottomnavigationview?

To change the selected tab icon color in BottomNavigationView you should use the Selector. Create bottom_navigation_selector.xml Apply app:itemIconTint="@drawable/bottom_navigation_selector" to your BottomNavigationView in xml file.

How to change navigation bar color on Android using React Native?

React Native Navigation Bar Color Change is a React Native library for change color of navigation/Bottom bar on Android. That's is all! Open up android/app/src/main/java/ [...]/MainApplication.java Add import com.thebylito.navigationbarcolor.NavigationBarColorPackage; to the imports at the top of the file


3 Answers

To change the selected tab icon color in BottomNavigationView you should use the Selector.

Create bottom_navigation_selector.xml

<?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/yourSelectedColor" />
  <item android:color="@color/defaultColor"  />
</selector>

Apply app:itemIconTint="@drawable/bottom_navigation_selector" to your BottomNavigationView in xml file.

like image 164
Chandan Sharma Avatar answered Oct 16 '22 11:10

Chandan Sharma


Despite reading all the answers I was confused about whole process, so I am going to explain step by step how I resolved this issue so beginners can understand it properly

Let's assume you created bottom navigation activity with name MainActivity so now

create bottom_navigation_selector.xml in your drawable folder using this code

<?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/yourSelectedColor" />
  <item android:color="@color/defaultColor"  />
</selector>

then go to the activity_main.xml layout and add this line in BottomNavigationView

app:itemIconTint="@drawable/bottom_navigation_selector"

If you also want to change text color accordingly then you need to add this line aswell

app:itemTextColor="@drawable/bottom_navigation_selector"
    
like image 35
Zohab Ali Avatar answered Oct 16 '22 09:10

Zohab Ali


To show the real color of items use this

java

bottom_navigation.setItemIconTintList(null);

kotlin

bottom_navigation.itemIconTintList = null

and if you want to change the calor just replace the null with

Color.parseColor("#ffffff")
like image 1
devio Avatar answered Oct 16 '22 10:10

devio