4

How to use drawableLeft with an xml icon? I have the following button:

<Button
   android:id="@+id/vitimas"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_marginBottom="10dp"
   android:layout_marginLeft="20dp"
   android:layout_marginRight="20dp"
   android:drawableLeft="@drawable/ic_person_black_24dp"
   android:drawableStart="@drawable/ic_person_black_24dp"
   android:background="@drawable/botao_verde"
   android:text="Vítimas"/>

In old APIs such as 16, the app stops working due to drawableLeft, I tried to use an ImageButton but the same happens, if I use app: srcCompat it works, however the icon is not stay in left, I need it to be stay in left and the text in the middle

The icon is from the Vector Asset package.

Woton Sampaio
  • 469
  • 6
  • 24
  • Another way of doing this is. You can create your own linearlayout or anyother layout and add text and image in it. Then align it as per your need. And apply your clickListeners on LinearLayout – Arshad Apr 23 '18 at 16:28
  • 2
    https://stackoverflow.com/a/40523623/4168607 – ADM Apr 23 '18 at 16:50

5 Answers5

8

AppCompatTextView supports app:drawableLeftCompat, app:drawableTopCompat, app:drawableRightCompat, app:drawableBottomCompat, app:drawableStartCompat and app:drawableEndCompat compound drawables, supporting backported drawable types such as VectorDrawableCompat.

Include this in your gradle file

implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
implementation 'androidx.appcompat:appcompat-resources:1.1.0-rc01'

In your text view you can use

app:drawableLeftCompat
app:drawableStartCompat
Darish
  • 11,032
  • 5
  • 50
  • 70
2

Best way I've found:

static public void setLeftDrawable(View view, Context c, int drawable) {
        Drawable leftDrawable = AppCompatResources.getDrawable(c, drawable);

        if (view instanceof TextInputEditText) {
            ((TextInputEditText) view).setCompoundDrawablesWithIntrinsicBounds(leftDrawable, null, null, null);

        } else if (view instanceof BootstrapEditText) {
            ((BootstrapEditText) view).setCompoundDrawablesWithIntrinsicBounds(leftDrawable, null, null, null);

        } else if (view instanceof EditText) {
            ((EditText) view).setCompoundDrawablesWithIntrinsicBounds(leftDrawable, null, null, null);

        } else if (view instanceof TextView) {
            ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(leftDrawable, null, null, null);
        }
    }

Then just call passing the View, the Context and the Drawable, very easy!

Woton Sampaio
  • 469
  • 6
  • 24
-1

add this in your gradle file

 android {
   defaultConfig {
     vectorDrawables.useSupportLibrary = true
    }
 }
-1

Older versions still do not support vector drawables in views such as TextView.

For that, you need to use the method setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom) to add a drawable to a TextView, CheckboxView, and the like.

TextView tv = findViewById(R.id.tv);
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);

Do not nest views just to get this effect, it's bad for performance and unnecessary.

for a list of similar methods see the docs.

Ace
  • 2,108
  • 22
  • 29
-1

Here I added code for gradle:

 android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
    }  
 }

after adding this code to your application class:

 @Override
public void onCreate() {
    super.onCreate();
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

after this create one layer file for the vector drawable :

ic_person_black.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_person_black_24dp"/></layer-list>

and now you are able to add this file to drawable left.

android:drawableLeft="@drawable/ic_person_black.xml"
Mahesh Keshvala
  • 1,349
  • 12
  • 19