I have a basic TabLayout that has 2 lists under 2 tabs (1 list in tab A and 1 list in tab B) setup with a ViewPager something like below.
// CustomPagerAdapter extends PagerAdapter
CustomPagerAdapter adapter = new CustomPagerAdapter(getFragmentManager());
adapter.addFragments();
TabLayout tabLayout = findViewById(R.id.tab_layout);
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(PagerAdapter);
tabLayout.setupWithViewPager(viewPager);
The given situation is that a user is seeing the tab A on screen. At this point, when a user taps on the tab B, I want the app to make a toast message saying it's not possible to view tab B, instead of actually navigating to the tab B.
In other words, I don't want the app to navigate to tab B, I just want it to show a Toast message while staying on tab A when the user taps on tab B.
I tried something like
((LinearLayout) tabLayout.getChildAt(0)).setEnabled(false);
but this didn't work because the tab is disabled by the code above so it doesn't even show the Toast message because it doesn't sense the click event.
I feel like I need to keep it enabled (by setEnabled(true) or something) and somehow override the method so the app show a Toast message instead of navigating to another tab.
How can I achieve this? I couldn't find any other stackoverflow post related to this issue..
Please help!
Use this code to enable/disable any tab in tablayout:
LinearLayout tabStrip = ((LinearLayout)tabLayout.getChildAt(0));
tabStrip.getChildAt(1).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(your condition) {
Toast.makeText(getActivity(), "Tab Enable", Toast.LENGTH_SHORT).show();
return false;
} else {
Toast.makeText(getActivity(), "Tab Disable", Toast.LENGTH_SHORT).show();
return true;
}
}
});