I need tabs for the application I am developing. So I ended up with tablayout and it contains few tabs. Code is as follows:
private void setupNavTab() {
int[][] states = new int[][]{
new int[]{android.R.attr.state_selected},
new int[]{-android.R.attr.state_selected},
new int[]{-android.R.attr.state_enabled}
};
int[] colors = new int[]{
ContextCompat.getColor(getActivity(), R.color.cricut_selected_green),
ContextCompat.getColor(getActivity(), R.color.nav_bar_unselected_content),
ContextCompat.getColor(getActivity(), R.color.edit_button_text_color_inactive)
};
ColorStateList cslist = new ColorStateList(states, colors);
if (tabs != null) {
tabs.removeAllTabs();
tabs.setTabTextColors(cslist);
firstTab = tabs.newTab().setTag(TAB_FIRST);
View customFirstTabView = LayoutInflater.from(getActivity()).inflate(R.layout.tab_item_layout, null, false);
firstTabView = (TextView) customFirstTabView.findViewById(R.id.textContainer);
firstTabView.setText(R.string.first);
firstTabView.setTextColor(cslist);
firstTab.setCustomView(customFirstTabView);
secondTab = tabs.newTab().setTag(TAB_SECOND);
View customSecondView = LayoutInflater.from(getActivity()).inflate(R.layout.tab_item_layout, null, false);
secondTabView = (TextView) customSecondView.findViewById(R.id.textContainer);
secondTabView.setText(R.string.second);
secondTabView.setTextColor(cslist);
secondTab.setCustomView(customSecondView);
thirdTab = tabs.newTab().setTag(TAB_THIRD);
View customThirdTabView
= LayoutInflater.from(getActivity()).inflate(R.layout.tab_item_layout, null, false);
thirdTabView = (TextView) customThirdTabView.findViewById(R.id.textContainer);
thirdTabView.setText(R.string.third);
thirdTabView.setTextColor(cslist);
thirdTab.setCustomView(customThirdTabView);
tabs.addTab(firstTab, true);
tabs.addTab(secondTab, false);
tabs.addTab(thirdTab, false);
}
}
I am not using viewpager. Here the third tab should be inaccessible until the user completes necessary steps in firstTab and secondTab. Is there a way to disable the thirdTab before those steps are completed by the user?
the method has slightly different on the time of its invocation, you just have to setup your tabitem to disable its click after all viewpager fragment already added.
You can change the font family for selected/unselected tab only programmatically. You can listen which Tab is selected and unselected with TabLayout. OnTabSelectedListener and on onTabSelected(TabLayout. Tab tab) callback you can change the Typeface for the selected Tab and on onTabUnselected(TabLayout.
use this for disable all tabs item :
LinearLayout tabStrip = ((LinearLayout)mTabLayout.getChildAt(0));
for(int i = 0; i < tabStrip.getChildCount(); i++) {
tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
}
and use this to disable one of them : (for example disable item 2)
LinearLayout tabStrip = ((LinearLayout)mTabLayout.getChildAt(0));
tabStrip.getChildAt(2).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
be careful item 2 has to exist
thanks from harry to refer to this answer in comments
You can access the view property of a tab
tabs.getTabAt(index).view.setClickable(true); //enable clicking
tabs.getTabAt(index).view.setClickable(false); //disable clicking
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With