android – How to not change Bottom Navigation View Item Color on click-ThrowExceptions

Exception or error:

I have three items in my bottomNavigationView

enter image description here

When I click on the profile item the code checks whether the person is logged in or not. If the person is not logged in then I need to start a new Activity else I need to load a fragment in my frameLayout.

Now the problem is that when I click on the profile item and the person is not logged in then the activity starts but when I click back then the profile item is highlighted but the home fragment is loaded in the frame layout.

enter image description here

I have tried the following ways to solve this

1) I used setSelectedItemId to set the item color when profile item is clicked but it didn’t work

Is there any other way to do this ?

How to solve:

I finally figured out how to do this and i am sharing it here

You need to return false if you don’t want to change the item’s color

bottomNavigationView.setOnNavigationItemSelectedListener(
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.action_home:
                            HomeFragment fav_frag = new HomeFragment();
                            currentFragment = fav_frag;
                            loadfragment(fav_frag);
                            break;
                        case R.id.action_designers:
                          break;
                        case R.id.action_profile:
                            if(PreferenceManager.getDefaultSharedPreferences(BaseActivity.this).getString("customer_id","").equalsIgnoreCase("")){
                                Intent intent=new Intent(BaseActivity.this,LoginActivity.class);
                                intent.putExtra("Target","Profile");
                                startActivity(intent);
                                overridePendingTransition(R.anim.slide_up,R.anim.stable);
// Here is the key , you need to return false when you don't want to change the items color
                                return false;
                            }else {
                                ProfileFragment accountFragment = new ProfileFragment();
                                currentFragment=accountFragment;
                                loadfragment(accountFragment);

                            }


                            break;
                    }
                    return true;
                }
            });

###

Try this.

Add selector to your code.

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

Then add your xml code.

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:itemIconTint="@drawable/selector_navigation"
    app:itemTextColor="@drawable/selector_navigation"
    app:menu="@menu/menu_navigation"/>

Note

// icon 
app:itemIconTint="@drawable/selector_navigation"
// text
app:itemTextColor="@drawable/selector_navigation"

###

you can try

<android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/toolbar_background"
            app:itemIconTint="@color/bottom_nac_color"
            app:itemTextColor="@color/bottom_nac_color"
            app:menu="@menu/bottom_navigation_main" />

here @color/bottom_nac_color is the color you want to display on screen

HOPE It works

###

you can change by java code like this.

UPDATE:

 bottomNav.addItemNav(new ItemNav(this, R.drawable.ic_home, getResources().getString(R.string.home)).addColorAtive(R.color.yellow_selected_color).addColorInative(R.color.text_color));

###

return false thats it because for launching activity we need not to higlight menu item

Leave a Reply

Your email address will not be published. Required fields are marked *