Fix drawableStart resource not found API 19

Hôm nay thử chạy trên máy Android 4.4 (api 19) thì bị lỗi drawableStart resource not found API 19 khi dùng drawableStart cho TextView:

android.content.res.Resources$NotFoundException: File res/drawable/ic_add.xml from drawable resource ID #0x7f020

Mình đã kiểm tra lại trong file build.gradle đã có hỗ trợ vector như dưới đây nhưng vẫn lôi:

android {
  defaultConfig {
    vectorDrawables.useSupportLibrary = true
  }
}

Cuối cùng mình đã khắc phục bằng cách sử dụng thư viện appcompat version 1.1.0-alpha01 (hoặc mới hơn)

implementation “androidx.appcompat:appcompat:1.1.0-alpha01”

Sau đó thay TextView bằng AppCompatTextView, và thay drawableStart bằng drawableStartCompat

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/tvIconSearch"
    android:layout_width="wrap_content"
    android:layout_height="22dp"
    android:background="@drawable/home_header_search_icon_bg_style"
    android:padding="6dp"
    app:drawableStartCompat="@drawable/ic_add" />