Disable one of the tabs in tablayout?
Asked Answered
B

3

11

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?

Brookins answered 29/2, 2016 at 6:6 Comment(4)
Check this if helps : https://mcmap.net/q/328786/-disable-tablayoutMadelene
thanks for the comment. That worked. Please put it as answer so that I can accept it.Brookins
hey Saroj, you disabled a single tab of a particular position ? I tried same code but it din't get. Can you please tell me ?Mientao
Did you look at the link mentioned above? I believe you can use the answer right below the accepted one by pat8719Brookins
S
7

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
Sycamine answered 31/7, 2020 at 3:38 Comment(0)
N
5

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

Natatorium answered 30/12, 2019 at 6:29 Comment(0)
L
2

I looked for this up and down. Here the tabLayout is declared public in MainActivity, and in this fragment I set them all disabled while the audioRecording is going on.

TabLayout tabLayout = MainActivity.tabLayout;
for (int i = 0; i < 3; i++){
    tabLayout.getTabAt(i).view.setEnabled(b);
}
Ledet answered 5/8, 2020 at 18:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.