mishti
mishti

Reputation: 193

How to disable a tab in bottom navigation view?

I have 4 tabs in my app. In which one is accessible without log-in but others can't be. So, I need to implement a functionality in which if a user clicks on that tab then rest 4 will be disabled and when I click on those tabs I only want toast but if I click on those tabs I get toast but it also got selected but fragment not changed. I want to disable that tab I have assigned a value to a variable to check whether it is without logging or after logging.

Code:

  case R.id.home:
                if(value.equals("1")){
                    Toast.makeText(CarerSeekerActivity.this,R.string.login_signup,Toast.LENGTH_SHORT).show();
                    navigation.getMenu().getItem(0).setEnabled(false);

                }
                else {
                    fragment = new CreatePersonalizedPackageCareSeekerFragment();
                    changeFragments(fragment);
                }
                return true;

but it is showing toast and but selected that tab. I do not want to make it selected. Please help.

Upvotes: 0

Views: 4289

Answers (2)

Piereligio Di Sante
Piereligio Di Sante

Reputation: 21

I know I'm replying 3 years later, but this is the function I've done for changing all the tabs to clickable or not clickable. You can easily adapt it for what you asked.

private void setNavigationBarClickableItems(BottomNavigationBar bottomNavigationBar, boolean clickable) {
    BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNavigationBar.getChildAt(0);

    for (int i = 0; i < menuView.getChildCount(); i++)  {
        menuView.getChildAt(i).setClickable(clickable);
    }
}

Upvotes: 0

Hemant N. Karmur
Hemant N. Karmur

Reputation: 880

After setting the menu please write below code in your on create method:

if(isloggedin){
// do click action which is required if the user already logged in
change your fragment from here
}else{
    bottomnavigation.getMenu().getItem(your_position).setEnabled(false); // disable menu if user not logged in
Toast.makeText(CarerSeekerActivity.this,R.string.login_signup,Toast.LENGTH_SHORT).show();
}

Upvotes: 3

Related Questions