Programmatically change specific Tab background

Asked

Viewed 189 times

0

I own a TabLayout in which I programmatically create tabs in this method:

mTabLayout.removeAllTabs();

for(int i = 0; i < list(); i++) {
    mTabLayout.addTab(mTabLayout.newTab().setText(list(i).name), false);
}

TabLayout.Tab newPlanTab = mTabLayout.newTab().setText(R.string.new_plan_tab);
mTabLayout.addTab(newPlanTab, false);

In this last tab that I add, I want to set a background and color of different text to highlight it, but I am not able to access these properties. How can I do that?

EDIT

One solution I found was to use a CustomView, but I’m not sure how to customize this CustomView to be like the others only with the different BG, she must inherit from whom?

  • Is Tablayout, by any chance, working in conjunction with a Viewpager? If yes, there is a very easy way to do it, even you won’t even need to be creating/removing tabs on your own.

  • Nao @Márciooliveira , when a tab is selected there is a request to download information that is modified in the same tab, simply changing the data shown on the screen.

  • Yet you could use Viewpager to load different data in the same Fragment (only in different instances). I’m saying this because Viewpager + Tablayout is almost like one thing, rs, so there are native methods to update Tablayout to certain Viewpager features. Ex: tab name, number of tabs, etc. For example, Chrome implements a Viewpager and adds tabs dynamically.

  • I understand what you’re saying, but what I want is to change the background of just one tab. The implementation of the rest I’ve done and is working, this would just be the icing on the cake.

  • You want to set a different highlight than is already created in the style of Tablayout?

  • Yes @Márciooliveira, because it will be a tab to create a new tab. I talked to a colleague who is Senior in the area and the only solution he remembered was to use customView even, but wanted to get away from it if possible.

  • That’s what I was going to comment on. Natively I don’t think I can.

  • Try creating a theme for this custom view with "Textappearance.Design.Tab" or "Widget.Design.Tablayout" as the parent and put a background attribute.

Show 3 more comments

1 answer

1


As no one had any different idea, even talking to people who work with me on Android, I had to implement my solution using CustomView

XML

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="@color/lightBlueDefault"
        android:id="@+id/tab_plan_new_textview"/>

</FrameLayout>

Java

View view = View.inflate(getContext(), R.layout.tab_plan_new, null);
TextView textView = (TextView) view.findViewById(R.id.tab_plan_new_textview);
textView.setText(R.string.new_plan_tab);
TabLayout.Tab newPlanTab = mTabLayout.newTab().setCustomView(view);
mTabLayout.addTab(newPlanTab, false);

Since Customview does not fill the full Tab height, I chose to just change the text color.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.