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.
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();
}
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);
}
}
Related
I'm setting a new android app, and I want to exploit the bottom navigation bar in my phone (previous,home) so that i don't have to create a customized one, I want to know if it is possible to set an onClickEvent on these buttons
You have to handle both button separately.
For hardware back button (Previous) you have to override onBackPress method in your Activity class. So, you will get the back press event you can change the behavior of back button if you don't want back event just remove super.onBackPress(). If you want to do this from fragment you just need to add your logic like you can get current fragment instance from FragmentManager like below and call fragment method what ever you want to do on that fragment.
\\ To get current fragment
\\ NOTE: I add back stack name as class name.
public static Fragment getCurrentFragment(FragmentManager fragmentManager) {
int count = fragmentManager.getBackStackEntryCount();
if (count > 0) {
FragmentManager.BackStackEntry backStackEntry = fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount() - 1);
String tag = backStackEntry.getName();
return fragmentManager.findFragmentByTag(tag);
}
return null;
}
#Override
public void onBackPressed() {
// enter code here
super.onBackPressed(); // Remove this line if you don't want to go back.
}
For home button handling you can use this answer or this post.
Please comment me if you have more questions.
I have implemented BottomNavigationView in my app. Everything going easy and perfect but there is one problem i.e. on back press I want to get the currently active tab selected but now selected tab does not change when back press. Fragment changes but tab selection does not change. How can I detect the current tab and change the selected tab on back press. I tried a lot of things to do but not able to get selected tab ID.Please help.
Code:
int i = getSelectedItem(bottomNavigationView);
Log.e("TAG", "onCreate:tab "+i );
private int getSelectedItem(BottomNavigationView bottomNavigationView){
Menu menu = bottomNavigationView.getMenu();
for (int i=0;i<bottomNavigationView.getMenu().size();i++){
MenuItem menuItem = menu.getItem(i);
if (menuItem.isChecked()){
return menuItem.getItemId();
}
}
return 0;
}
If you want to find the checked item id, try this:
int checkedItemId = bottomNavigationView.getSelectedItemId();
To change current selected item, use this:
bottomNavigationView.getMenu().findItem(R.id.target_item_nemu).setChecked(true);
so i have a standard tabLayout that is in a viewpager.
But the very last tab needs to open a new activity. its only purpose is to open a new activity. and when the user returns from the activity by pressing the back button the last tab the user was at should be selected.
my tabs are custom views and there are 4 of them but the last one should be a button to trigger an event. so it will look just like a tab but really will be a button with a onclick listener. how can i acheive this ? i am wondering if a framelayout could be used and activated for the very last tab icon . thats the only way i can see to do it is to get the custom view and set its parent to be clickable that way it absorbs the click event. then i can set a onclick listener on the parent to open the activity. but then how to handle swipping to that tab ? swipping should open that activity as well.
Assuming you're using TabLayout.setupWithViewPager(), you can accomplish what you want by simply adding an extra tab and changing the OnTabSelectedListener.
// normal setup
tabs.setupWithViewPager(pager);
// our extra tab
tabs.addTab(tabs.newTab().setText("extra"));
// remove the `OnTabSelectedListener` created by `setupWithViewPager()`
tabs.clearOnTabSelectedListeners();
// add our own `OnTabSelectedListener`
tabs.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(pager) {
#Override
public void onTabSelected(TabLayout.Tab tab) {
if (tab.getPosition() == pager.getAdapter().getCount()) {
// special case for the last tab in the list
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
else {
// otherwise, handle as normal
super.onTabSelected(tab);
}
}
});
When transitioning between pages, each fragment page shares the same menu options. The menu option contains button which launches an activity. That activity is highly dependent on the information the current page item shows.
I noticed that when I change the view such that, the next page is almost shown on the screen (not fully transitioned) and I select the option for that item, the options shows the data from the previous item. I think this is because, he transition between pages was not fully complete. This is kind of confusing for my users. There could be people who swipe faster and press the option button. I noticed then when swiping between fragments and then suddenly press the menu option. The options shows the data from the previously active page.
If I could only hide the menu options, and only show it when the page is fully transitioned, I believe I can solve this problem. Or else, maybe I am doing something wrong which could have averted this in the first place?
I am using FragmentStatePagerAdapter.
Thank you!
Use ViewPager.SimpleOnPageChangeListener and when the onPageScrollStateChanged event is triggered check to see the state of the scroll if is idle then the page is in view and active (allow user to press the menu button), if is in another state then lock/hide the menu buttons.
I think I was able to solve my problem. I created an interface on my fragment which the hosting activity has to implement:
public interface OnOptionsMenuEnabledListener{ public boolean onOptionsMenuEnabledListener(); }
The hosting activity will just return a flag indicating if the menu is enabled.
#Override
public boolean onOptionsMenuEnabledListener()
{
return mOptionsEnabled;
}
And set the flag via ViewPager.onPageScrollStateChanged (int state):
#Override
public void onPageScrollStateChanged(int state)
{
switch(state)
{
case ViewPager.SCROLL_STATE_IDLE:
mOptionsEnabled = true;
break;
case ViewPager.SCROLL_STATE_DRAGGING:
mOptionsEnabled = false;
break;
case ViewPager.SCROLL_STATE_SETTLING:
mOptionsEnabled = true;
break;
}
}
Every time the option is being selected during screen transition, I call the interface method to communicate to me what the status of the flag:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
boolean enable = true;
if(mOnOptionsMenuEnabledListener != null)
{
enable = mOnOptionsMenuEnabledListener.onOptionsMenuEnabledListener();
}
if(enable)
{
...
// select your menu items
}
return true;
}
So in this approach, the menu is still there (which is a big plus). But the option menu doesn't react until it settles down on a particular page.
With this I no longer encounter the problem.
I hope this help someone in the future!
Cheers!
When the button is clicked check ViewPagers current item -
https://developer.android.com/reference/android/support/v4/view/ViewPager.html#getCurrentItem()
YourObject object = yourList.get(yourViewPager.getCurrentItem());
I'm using scrollable tabs with FragmentPagerAdapter and I need to switch between the tabs programmatically
After clicking the create group button, I need to switch to the next tab i.e. manage group. How to do that?
For just moving 1 step ahead you can do:
int current = mPager.getCurrentItem();
mPager.setCurrentItem(current + 1, true);
If you want to scroll to any specific index you can just pass that too:
mPager.setCurrentItem(index, true);
You can also set smoothScroll flag as true/false which will give you smooth transition effect.
EDIT:
In your activity make a method :
public void scrollPager(int index) {
if (mPager != null) {
mPager.setCurrentItem(index, true);
}
}
From your fragment when you hit the button you can either create a call back or simply call activity's method:
((ActivityName) getActivity()).scrollPager(1);