Search code examples
androidtextviewxml-drawable

Android TextView DrawableTint on pre v23 devices


Is there any way we can tint the Drawable used in the TextView? DrawableTint works only on API level 23 and above.

Currently I'm using a Vertical Linear Layout to achieve my requirement.

<LinearLayout style="@style/ChoiceIllustratorIconTextContainerStyle">

  <ImageView
    style="@style/ChoiceIllustratorImageStyle"
    android:contentDescription="@string/cd_university"
    android:src="@drawable/ic_account_balance_white_24dp" />

  <TextView
    style="@style/ChoiceIllustratorTextStyle"
    android:text="@string/ci_text_university" />

</LinearLayout>

And it looks like,enter image description here

Android studio is suggesting me to use Compound Drawble with TextView to achieve this. And I'm able to achieve it, but I cannot find a way to Tint the drawable.

<TextView
   style="@style/ChoiceIllustratorTextStyle"
   android:drawablePadding="4dp"
   android:drawableTop="@drawable/ic_account_balance_white_24dp"
   android:text="@string/ci_text_university" />

Solution

  • AndroidX appcompat library supports tinting in TextView since version 1.1.0-alpha03 [ref].

    Add dependencies to appcompat library

    dependencies {
      implementation "androidx.appcompat:appcompat:1.1.0"
    }
    

    Then drawable in TextView can be tinted from XML like this

    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      app:drawableStartCompat="@drawable/ic_plus"
      app:drawableTint="@color/red" />
    

    Don't forget to include

    xmlns:app="http://schemas.android.com/apk/res-auto"
    

    and to extend your Activity from AppCompatActivity.