Skip to content Skip to sidebar Skip to footer

Get All Tab ( Tabs) Labels In A List From Tabs Layout Android

I have set tab layout with view pager, As follows : sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager(), suraName, suraId, ayatId); final Vie

Solution 1:

So, how to get all tab labels from tabsLayout. In oncreate after setting up all adapters, viewpager and tabsLayout (you can wait for some time also) :

ArrayList<String> labelList = newArrayList<>();

        labelList = tabLabelList(tabsLayout, labelList);
        if(labelList!=null)
        Log.i(TAG, "onCreate: list labels"+labelList.toString());

And :

publicstatic ArrayList<String> tabLabelList(View view, ArrayList<String> tabLabelsList) {
        if (!(view instanceof ViewGroup)) {
            returnnull;
        }

        ViewGroupgroup= (ViewGroup)view;
        for (inti=0; i < group.getChildCount(); i++) {
            Viewchild= group.getChildAt(i);
            if (child instanceof TextView) {
                Strings= ((TextView)child).getText().toString();
                tabLabelsList.add (s);
            } else {
                tabLabelList(child, tabLabelsList);
            }
        }
        return  tabLabelsList;
    }

This way one can get all text from a view which has a textView as child.

Post a Comment for "Get All Tab ( Tabs) Labels In A List From Tabs Layout Android"