Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable one of the tabs in tablayout?

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?

like image 415
SarojMjn Avatar asked Feb 29 '16 06:02

SarojMjn


People also ask

How do I disable TabLayout click?

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.

How do you change the typeface of a selected tab of a TabLayout?

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.


2 Answers

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

like image 184
m.sajjad.s Avatar answered Oct 08 '22 05:10

m.sajjad.s


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
like image 25
Jasper Diongco Avatar answered Oct 08 '22 06:10

Jasper Diongco