Change specific Tab background programmatically

0

I have a TabLayout in which I create the tabs programmatically 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);

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

EDIT

A solution I found was to use a CustomView , but I'm not sure how to customize this CustomView to be the same as the other only with the different BG, should it inherit from whom?

    
asked by anonymous 31.07.2017 / 23:10

1 answer

1

Since no one had a different idea, even talking to people working 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 entire height of the Tab, I've just opted to just change the color of the text.

    
03.08.2017 / 01:44