3

I would like to change the selected tab indicator of a TabLayout, with a custom icon, that should follow the same behavior (as the user slides towards the next tab, the icon moves accordingly).

There's no default method in the class since you can only reach a tab's layout customization. I decided to take a look to the TabLayout's source code, but 1) I can't find in the class the point in which there's the definition of the shape that the tab indicator will assume when drawn, and 2) I can't even fully import the class. I also would like – if possible – to avoid any third-party library, since the project is already very heavy.

Do you have any suggestions? I can see two possible approaches:

  1. finding a way to customize the TabLayout class in order to replace the "squarish shape" with a custom icon.
  2. finding a way to coordinate the user's touch gestures on the ViewPager with the movements of a custom View (ie. my icon).
Gian Segato
  • 2,359
  • 3
  • 29
  • 37

1 Answers1

0

One solution I can think of at the moment is you'll have to create a custom view layout file that represents your desired "selected tab" and annotate android:tabStripEnabled="false" to your TabLayout in the XML file.

After that, you'll have to register a listener on the tablayout instance, and check which index is being selected, and with the given index, call TabLayout.getTabAt(i).setCustomView(yourSelectedCustomView).

Take a look here on how to create a custom view for TabLayout : https://stackoverflow.com/a/48258731/4974461

atjua
  • 541
  • 1
  • 9
  • 18