-3

I make my custom tablayout view to customize font for tablayout android but it give me crash on mTabs.add(position, tab); what is wrong with my code?

@Override
public void addTab(@NonNull Tab tab, int position, boolean setSelected) {
    MyTextView myTextView = (MyTextView)
    View.inflate(getContext(), R.layout.tab_item_template, null);
    myTextView.setText(tab.getText());
    tab.setCustomView(myTextView);

    mTabs.add(position, tab);
    super.addTab(tab, position, setSelected);
}

the error is

java.lang.IndexOutOfBoundsException: Invalid index 10, size is 0

1 Answer 1

0

try this to set custom tabs in your tab layout

TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setText("ONE");
tabOne.setTextColor(ContextCompat.getColor(this,R.color.colorAccent));
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_favourite, 0, 0);// if you want to set icon on that tab
tabLayout.getTabAt(0).setCustomView(tabOne);
Sign up to request clarification or add additional context in comments.

2 Comments

not fixes error is on mTabs.add(position, tab); when i comment it. tablayout works fine but i cant change color of text and everything else
It doesn't depend on TextView, I said when I want to add tab to ArrayList it makes error that ArrayList is empty and index 10 is out of bound

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.