Sliding tab layout text is uppercase
Asked Answered
W

4

7

Hi I'm using Sliding tab layout in my app and it all works great. The only thing I dont understand is why my tabs text are in uppercase.

I've printed the text the tabs get inside of the sliding tab class and they are not in uppercase. I've looked around and there is no toUpperCase method being called.

Here is the code from the class that sets the text:

  private void populateTabStrip() {
        final PagerAdapter adapter = mViewPager.getAdapter();
        final View.OnClickListener tabClickListener = new TabClickListener();

        for (int i = 0; i < adapter.getCount(); i++) {
            View tabView = null;
            TextView tabTitleView = null;

            if (mTabViewLayoutId != 0) {
                // If there is a custom tab view layout id set, try and inflate it
                tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip,
                        false);
                tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
            }

            if (tabView == null) {
                tabView = createDefaultTabView(getContext());
            }

            if (tabTitleView == null && TextView.class.isInstance(tabView)) {
                tabTitleView = (TextView) tabView;
            }

            if (mDistributeEvenly) {
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tabView.getLayoutParams();
                lp.width = 0;
                lp.weight = 1;
            }

            tabTitleView.setText(adapter.getPageTitle(i));
            tabTitleView.setTextColor(getResources().getColor(R.color.da_blue));
            tabView.setOnClickListener(tabClickListener);
            String desc = mContentDescriptions.get(i, null);
            if (desc != null) {
                tabView.setContentDescription(desc);
            }

            mTabStrip.addView(tabView);
            if (i == mViewPager.getCurrentItem()) {
                tabView.setSelected(true);
            }
        }
    }

I'm sure I can do it through a style but really not sure which one to use. This is what I have in my styles:

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:actionBarTabTextStyle">@style/ActionBarTabTextStyle.Tabtheme</item>
        <item name="actionBarTabTextStyle">@style/ActionBarTabTextStyle.Tabtheme</item>
    </style>


    <style name="ActionBarTabTextStyle.Tabtheme" parent="@android:style/Widget.Holo.ActionBar.TabText">
        <item name="android:textAllCaps">false</item>
    </style>
</resources>
Wakeup answered 4/2, 2015 at 8:9 Comment(1)
Are you seeing this in L? This is default behavior. You can refer to this answer if you don't want all caps. #26959409Gnathonic
W
6

Found the answer it was in the createDefaultTabView() method.

Needed to change textView.setAllCaps(true) to false.

protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setLayoutParams(new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

    TypedValue outValue = new TypedValue();
    getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
            outValue, true);
    textView.setBackgroundResource(outValue.resourceId);
    textView.setAllCaps(false); **// Changed to false and it did the trick**

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, padding, padding, padding);

    return textView;
}
Wakeup answered 17/2, 2015 at 7:40 Comment(1)
Trying to change TAB_VIEW_TEXT_SIZE_SP value in above method but changes not reflectedRompish
C
21

If you use "android.support.design.widget.TabLayout" needed to set app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"

Costard answered 8/1, 2016 at 13:28 Comment(1)
Not working for com.google.android.material.tabs.TabLayoutPriscilapriscilla
W
6

Found the answer it was in the createDefaultTabView() method.

Needed to change textView.setAllCaps(true) to false.

protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setLayoutParams(new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

    TypedValue outValue = new TypedValue();
    getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
            outValue, true);
    textView.setBackgroundResource(outValue.resourceId);
    textView.setAllCaps(false); **// Changed to false and it did the trick**

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, padding, padding, padding);

    return textView;
}
Wakeup answered 17/2, 2015 at 7:40 Comment(1)
Trying to change TAB_VIEW_TEXT_SIZE_SP value in above method but changes not reflectedRompish
V
1

just add this linetabTitleView.setAllCaps(false); in populateTabStrip()

Verena answered 17/9, 2017 at 21:41 Comment(0)
M
0

You can do it by use your style values and call it app:tabTextAppearance="@style/TextTabLayout" in your tablayout tag.

in your style values just add this, you can also add your text size or font family.

<style name="TextTabLayout">
   <item name="android:textAllCaps">false</item>
</style>
Melodiemelodion answered 17/6, 2022 at 13:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.