0

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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
mishti
  • 183
  • 7
  • 20

2 Answers2

3

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();
}
Hemant N. Karmur
  • 840
  • 1
  • 7
  • 21
0

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);
    }
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 22 '22 at 04:28
  • This sets the clickable for all the children under `menuView`, but the OP wants a specific one to be set clickable/non-clickable. – Abhishek Dutt Sep 22 '22 at 04:31
  • @AbhishekDutt correct, like I said it can be easily adapted though. Should be enough to access the corresponding index in the children of menuView, for getting the one he wanted to disable instead of iterating; or to insert a condition inside the iteration, for doing a check based on the id of the button or something similar. – Piereligio Di Sante Oct 10 '22 at 15:02