1

I cannot use library of designs how to change color onclick in this code?

<com.google.android.material.tabs.TabItem
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="new order"
 />
usama
  • 11
  • 1
  • check this answer https://stackoverflow.com/questions/31640563/how-do-i-change-a-tab-background-color-when-using-tablayout – Mohamed Mo'nes Oct 06 '19 at 15:42
  • Possible duplicate of [How do I change a tab background color when using TabLayout?](https://stackoverflow.com/questions/31640563/how-do-i-change-a-tab-background-color-when-using-tablayout) – Kate Orlova Oct 06 '19 at 17:44

2 Answers2

0

I would rather change the color at runtime than adding a style for change on selection to keep ripple feel. Here you set color based on selection at code level

setBackgroundColor(Color.RED);

Check this for more

https://developer.android.com/reference/com/google/android/material/tabs/TabItem

Ankit Tale
  • 1,924
  • 4
  • 17
  • 30
0

Have you tried using an xml color selector?

create an xml file like this in the drawable directory:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorAccent" android:state_selected="true"/>
<item android:drawable="@color/colorPrimary"/>
</selector>

and in your tablayout xml code:

        android:background="@drawable/drawable_xml_selector"

I hope this helps.

ahooee
  • 275
  • 3
  • 14