Skip to content Skip to sidebar Skip to footer

How Do I Get The View Of A Tab In A Tablayout?

I'd like to find the view of a Tab in a TabLayout so that I can pass it to another function. I'm not sure how to go about finding the view. myTabLayout.getTabAt(0).getCustomView()

Solution 1:

I ended up using the following to get tab views. I'm just not sure if it's best practice or if it's reliable across all Android versions:

mainTab = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(desiredPosition);

Looking at source we can see that tabLayout.getChildAt(0) returns the SlidingTabStrip which is an internal class extending LinearLayout that holds the tab views. Then, you can access the tab view with .getChildAt(desiredPosition). Note that when using getChildAt() boundaries are not being checked, so make sure you are calling correct indexes and also check for null returns.

Solution 2:

    TabLayout tabLayout = .... (findview or code creation )
    /** get selected tab index */int selectedTabPosition = tabLayout.getSelectedTabPosition();
    /** get tab for selected index or if u want any other tab set desired index */
    TabLayout.Tab tabAt = tabLayout.getTabAt(selectedTabPosition);
    /** get view - but first u need set custom view on tabl via Tab.setCustomView(View) */View tabView = tabAt.getCustomView():

hint:

  • if you populate TabLayout with ViewPager check first if view is laid out :). If not set onLayoutChangedListener for ViewPager then setup with pager!

Tab Source if you wish to use reflections :D

/**
* A tab in this layout. Instances can be created via {@link #newTab()}.
*/publicstatic final classTab {
    /**
     * An invalid position for a tab.
     *
     * @see #getPosition()
     */publicstatic final int INVALID_POSITION = -1;
    privateObject mTag;
    privateDrawable mIcon;
    privateCharSequence mText;
    privateCharSequence mContentDesc;
    private int mPosition = INVALID_POSITION;
    privateView mCustomView;
    private final TabLayout mParent;

    Tab(TabLayout parent) {
        mParent = parent;
    }

    /**
     * @return This Tab's tag object.
     */@NullablepublicObjectgetTag() {
        return mTag;
    }

    /**
     * Give this Tab an arbitrary object to hold for later use.
     *
     * @param tag Object to store
     * @return The current instance for call chaining
     */@NonNullpublicTabsetTag(@NullableObject tag) {
        mTag = tag;
        returnthis;
    }

    /**
     * Returns the custom view used for this tab.
     *
     * @see #setCustomView(View)
     * @see #setCustomView(int)
     */@NullablepublicViewgetCustomView() {
        return mCustomView;
    }

    /**
     * Set a custom view to be used for this tab.
     * <p>
     * If the provided view contains a {@link TextView} with an ID of
     * {@link android.R.id#text1} then that will be updated with the value given
     * to {@link #setText(CharSequence)}. Similarly, if this layout contains an
     * {@link ImageView} with ID {@link android.R.id#icon} then it will be updated with
     * the value given to {@link #setIcon(Drawable)}.
     * </p>
     *
     * @param view Custom view to be used as a tab.
     * @return The current instance for call chaining
     */@NonNullpublicTabsetCustomView(@Nullable View view) {
        mCustomView = view;
        if (mPosition >= 0) {
            mParent.updateTab(mPosition);
        }
        returnthis;
    }

    /**
     * Set a custom view to be used for this tab.
     * <p>
     * If the inflated layout contains a {@link TextView} with an ID of
     * {@link android.R.id#text1} then that will be updated with the value given
     * to {@link #setText(CharSequence)}. Similarly, if this layout contains an
     * {@link ImageView} with ID {@link android.R.id#icon} then it will be updated with
     * the value given to {@link #setIcon(Drawable)}.
     * </p>
     *
     * @param layoutResId A layout resource to inflate and use as a custom tab view
     * @return The current instance for call chaining
     */@NonNullpublicTabsetCustomView(@LayoutRes int layoutResId) {
        returnsetCustomView(
                LayoutInflater.from(mParent.getContext()).inflate(layoutResId, null));
    }

    /**
     * Return the icon associated with this tab.
     *
     * @return The tab's icon
     */@NullablepublicDrawablegetIcon() {
        return mIcon;
    }

    /**
     * Return the current position of this tab in the action bar.
     *
     * @return Current position, or {@link #INVALID_POSITION} if this tab is not currently in
     * the action bar.
     */public int getPosition() {
        return mPosition;
    }

    voidsetPosition(int position) {
        mPosition = position;
    }

    /**
     * Return the text of this tab.
     *
     * @return The tab's text
     */@NullablepublicCharSequencegetText() {
        return mText;
    }

    /**
     * Set the icon displayed on this tab.
     *
     * @param icon The drawable to use as an icon
     * @return The current instance for call chaining
     */@NonNullpublicTabsetIcon(@Nullable Drawable icon) {
        mIcon = icon;
        if (mPosition >= 0) {
            mParent.updateTab(mPosition);
        }
        returnthis;
    }

    /**
     * Set the icon displayed on this tab.
     *
     * @param resId A resource ID referring to the icon that should be displayed
     * @return The current instance for call chaining
     */@NonNullpublicTabsetIcon(@DrawableRes int resId) {
        returnsetIcon(TintManager.getDrawable(mParent.getContext(), resId));
    }

    /**
     * Set the text displayed on this tab. Text may be truncated if there is not room to display
     * the entire string.
     *
     * @param text The text to display
     * @return The current instance for call chaining
     */@NonNullpublicTabsetText(@Nullable CharSequence text) {
        mText = text;
        if (mPosition >= 0) {
            mParent.updateTab(mPosition);
        }
        returnthis;
    }

    /**
     * Set the text displayed on this tab. Text may be truncated if there is not room to display
     * the entire string.
     *
     * @param resId A resource ID referring to the text that should be displayed
     * @return The current instance for call chaining
     */@NonNullpublicTabsetText(@StringRes int resId) {
        returnsetText(mParent.getResources().getText(resId));
    }

    /**
     * Select this tab. Only valid if the tab has been added to the action bar.
     */publicvoidselect() {
        mParent.selectTab(this);
    }

    /**
     * Returns true if this tab is currently selected.
     */publicbooleanisSelected() {
        return mParent.getSelectedTabPosition() == mPosition;
    }

    /**
     * Set a description of this tab's content for use in accessibility support. If no content
     * description is provided the title will be used.
     *
     * @param resId A resource ID referring to the description text
     * @return The current instance for call chaining
     * @see #setContentDescription(CharSequence)
     * @see #getContentDescription()
     */@NonNullpublicTabsetContentDescription(@StringRes int resId) {
        returnsetContentDescription(mParent.getResources().getText(resId));
    }

    /**
     * Set a description of this tab's content for use in accessibility support. If no content
     * description is provided the title will be used.
     *
     * @param contentDesc Description of this tab's content
     * @return The current instance for call chaining
     * @see #setContentDescription(int)
     * @see #getContentDescription()
     */@NonNullpublicTabsetContentDescription(@Nullable CharSequence contentDesc) {
        mContentDesc = contentDesc;
        if (mPosition >= 0) {
            mParent.updateTab(mPosition);
        }
        returnthis;
    }

    /**
     * Gets a brief description of this tab's content for use in accessibility support.
     *
     * @return Description of this tab's content
     * @see #setContentDescription(CharSequence)
     * @see #setContentDescription(int)
     */@NullablepublicCharSequencegetContentDescription() {
        return mContentDesc;
    }
}

or you can hook directly into (via reflection):

privatefinal SlidingTabStrip mTabStrip;

or you can copy source code and change methods and fields at your discretion.

Solution 3:

It returns null because you're not using any Custom View in the first place. It returns the custom view only when you use it. For using the custom view, your code should be something like this.

tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.custom_view).setText("Page1"));

If you use the above line and then try to call myTabLayout.getTabAt(0).getCustomView(), it'd return you the view that you set.

Solution 4:

Answering for future references.

tabLayout.getTabAt(tabIndex).view

Example- To get the view of first tab in tabLayout. Use tabLayout.getTabAt(0).view

Post a Comment for "How Do I Get The View Of A Tab In A Tablayout?"