Jay
Jay

Reputation: 205

getCompoundDrawables returns null inside of onCreateView, but not later

I want to run the code below in order to tint a button's drawable on pre-lollipop devices, however button.getCompoundDrawables() is returning null for all 4 elements of the array when called inside of the Fragment's onCreateView method.

If I inspect the same Drawable[] array at a later point in time - say upon a button click event - I can see the drawable value has been correctly assigned (3 are null, 1 is valid).

Is there some button life cycle or fragment life cycle that I can rely on the compound drawables array to have been already properly initialized?

Drawable[] drawables = button.getCompoundDrawables();
        if( drawables[2] != null){
            Drawable wrapDrawable = DrawableCompat.wrap(drawables[2]);
            DrawableCompat.setTint(wrapDrawable, color);
            button.invalidate();
        }

Here's the lib versions I'm using:

compile 'com.android.support:appcompat-v7:24.1.1'
compile 'com.android.support:support-v4:24.1.1'
compile 'com.android.support:design:24.2.0'

At request, I'm including also some xml code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout        xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
 [...]  >

<Button
    android:id="@+id/bt1"
    android:background="@android:color/transparent"
    android:textAppearance="@style/ConfigButtonTheme"
    android:text="Sincronizar Música"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:drawableEnd="@drawable/ic_chevron_right_white_24dp"
    android:textAlignment="textStart"
    android:layout_width="match_parent"
    android:layout_height="60dp" />

</LinearLayout>

Upvotes: 16

Views: 4557

Answers (7)

CoolMind
CoolMind

Reputation: 28773

In Kotlin returns a list of drawables:

val drawables = (compoundDrawables zip compoundDrawablesRelative).map {
    it.first ?: it.second
}

Upvotes: 0

Chandra Sekhar
Chandra Sekhar

Reputation: 19492

for android:drawableRight, you should use getCompoundDrawables(), where as for android:drawableEnd, you should use getCompoundDrawablesRelative().

getCompoundDrawablesRelative()

Upvotes: 59

Chinese Cat
Chinese Cat

Reputation: 5428

It doesn't load your drawables within TextView at the beginning. You should use

TextView.post({ 
    // get your drawables here.
})

this function to get your drawables when it's loaded.

Upvotes: -1

Santosh Dangodra
Santosh Dangodra

Reputation: 9

Change android:drawableEnd to android:drawableRight. Not sure why but drawableEnd returns null in onCreate() method and drawableRight works fine.

OR

Another way to do without changing android:drawableEnd to android:drawableRight. It will work 100%

just write your code as follow:

onCreate(){
    //your all statement
    //at the end
    findViewById(android.R.id.content).post(new Runnable() {
   @Override
   public void run() {
     //write your code here you will get all the drawables
   }
   });
}

Upvotes: 0

Andre Haueisen
Andre Haueisen

Reputation: 518

You could configure the drawable programmatically and then set it into the text view like so.

 val textDrawable = resources.getDrawable(R.drawable.ic_arrow_upward_24dp, null)
 val color = ResourcesCompat.getColor(resources, R.color.colorAccent, null)
     textDrawable.setTint(color)

            //setCompoundDrawablesRelativeWithIntrinsicBounds(left, top, right, bottom)
     textView.setCompoundDrawablesRelativeWithIntrinsicBounds(null, textDrawable, null, null)

Upvotes: 0

heloisasim
heloisasim

Reputation: 5016

Change android:drawableEnd to android:drawableRight. Not sure why but drawableEnd returns null in onCreate() method and drawableRight works fine.

Upvotes: 33

CodyEngel
CodyEngel

Reputation: 1501

My guess is that the drawable hasn't been created/inflated yet. Try putting that code in either onActivityCreated, onStart or onResume within the Fragment. These are in order of when they will be called within the lifecycle, ideally you want to do this as soon as possible.

Upvotes: -1

Related Questions