Search code examples
androidbuttondrawableandroid-vectordrawable

Android App stops when Button has drawable<xyz>


I have a problem adding an icon to a button either left of text or above. This is my test scenario:

My app already has a main activity with a button to start the newly added test activity.

I add an "Empty Activity" to my app, place a button on it and start the app, the activity shows, everything's fine.

Now I set the button's 'drawableTop="@drawable/ic_add_circle' where ic_add_circle contains the Android Studio material icon "add circle outline" in 24x24dp.

In the design editor everything look fine, but when I start my app and try to show the activity, the app stops immediately. I tried this with drawableTop, Left, Right, Bottom - always the same. What am i doing wrong?

This is the Activity's xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="me.myself.i.test.myActivity">
<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    android:drawableTop="@drawable/ic_add_circle"
    android:text="Button"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

... the icon

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
<path
    android:fillColor="#FF000000"
    android:pathData="M13,7h-2v4L7,11v2h4v4h2v-4h4v-2h-4L13,7zM12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,20c-4.41,0 -8,-3.59 -8,-8s3.59,-8 8,-8 8,3.59 8,8 -3.59,8 -8,8z"/>
</vector>

... and the java code

package me.myself.i.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class myActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);
    }
}

Solution

  • Using vector icons as left/right/bottom/top drawable of any view will end up in app crash for pre-lollipop devices as vector drawables are supported in lollipop and higher. But you can set the drawable programmatically like this to avoid the crash in pre-lollipop devices:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);
    
        Button button = findViewById(R.id.button4);
        button.setCompoundDrawablesWithIntrinsicBounds(null, 
        AppCompatResources.getDrawable(this, R.drawable.ic_add_circle), null, null);
    }