2

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 ?

Anil
  • 1,605
  • 1
  • 14
  • 24
ANUJ GUPTA
  • 905
  • 13
  • 22
  • Have you tried putting setOnNavigationItemReselectedListener(BottomNavigationView.OnNavigationItemReselectedListener listener) Set a listener that will be notified when the currently selected bottom navigation item is reselected. – vikas kumar Aug 25 '17 at 08:02
  • this is how you can know which menu is clicked and then change its color using setItemIconTintList(ColorStateList tint) – vikas kumar Aug 25 '17 at 08:03
  • @vikaskumar I am not reselecting the item. I am on the Home item and then i click on the Profile Item. – ANUJ GUPTA Aug 25 '17 at 08:03
  • @vikaskumar i also need to highlight the item whose fragment is currently loaded. – ANUJ GUPTA Aug 25 '17 at 08:04
  • can you post your code for above, here i can't tell where you are going wrong. what i am getting is you are trying to check weather user has logged in or not then open a activity or a fragment. but if you place a profile tab in navigation it is suppossed to open some fragment anyway. you can check your condition either in adapter or some where before opening third tab. you can place listener posted above to know which menu item clicked and then make logic there to open activity or profile. And open home fragment if user is not logged in. – vikas kumar Aug 25 '17 at 08:14
  • @ANUJGUPTA you can check my answer. – KeLiuyue Aug 25 '17 at 08:32

6 Answers6

7

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;
                }
            });
ANUJ GUPTA
  • 905
  • 13
  • 22
1

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"
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
  • This is the best approach. More details here : https://stackoverflow.com/questions/40325422/selected-tabs-color-in-bottom-navigation-view – Jack' Aug 22 '19 at 15:22
0

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

Shailesh Bandil
  • 497
  • 7
  • 20
0

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));
Amir Christian
  • 597
  • 6
  • 20
Ali Doran
  • 378
  • 1
  • 12
0

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

Ru Chern Chong
  • 3,692
  • 13
  • 33
  • 43
Prakash Reddy
  • 944
  • 10
  • 20
0

probably you use switch in onNavigationItemSelected for your bottom navigation and you return true for in end of every case you have.

for every item that you don't want to change their colors, return false and for change color when user click on item return true.

bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
        @SuppressLint("NonConstantResourceId")
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.one:
                    // your code
                    return true; // on click will change color
                case R.id.two:
                    // your code 
                    return false; // on click will not change color
            }
            return false;
        }
    });