Question

I have an ImageButton which I'd like to highlight - eg. flash when a download is finished.

I'm trying to set the background to a TransitionDrawable and reset it back when the transition is finished, but I get weird results:

  • The padding of the button disappears once the transition is activated and isn't restored when I restore the original drawable when it is finished.
  • The transition only happens on the stroke (border) of the button but not the gradient that fills the area of the background.

This is the Java code I am using to activate the transition, reverse it and restore the original drawable background:

downloadButton.setBackgroundResource(R.drawable.animate_background);
Drawable background = downloadButton.getBackground().getCurrent();
if (background instanceof TransitionDrawable) {
  TransitionDrawable transition = (TransitionDrawable) background;
  transition.startTransition(300);
  downloadButton.postDelayed(new Runnable() {
    @Override
    public void run() {
      // reverse the transition after it completes
      Drawable background = downloadButton.getBackground().getCurrent();
      if (background instanceof TransitionDrawable) {
        TransitionDrawable transition = (TransitionDrawable) background;
        transition.reverseTransition(200);
        downloadButton.postDelayed(new Runnable() {
          @Override
          public void run() {
            // restore original background
            downloadButton.setBackgroundResource(R.drawable.custom_button);                                 
          }
        }, 200); // after reverse transition
      }
    }
  }, 300); // after transition
}

This is the button in the layout xml:

<ImageButton
    android:id="@id/download_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_button"
    android:padding="14dp"
    android:src="@drawable/download_icon" />

This is the xml of the the custom_button drawable:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
    <shape>
        <solid android:color="#343434" />
        <stroke  android:width="1dp" android:color="#171737" />
        <corners android:radius="5dp" />
    </shape>
</item>
<item>
    <shape>
        <gradient  android:startColor="#343434"  android:endColor="#171737" android:angle="270" />
        <stroke    android:width="1dp"     android:color="#171717" />
        <corners   android:radius="5dp" />
    </shape>
</item>

The transition "animate_background" xml:

<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/animate_start" />
  <item android:drawable="@drawable/animate_end" />
</transition>

Animate start xml (animate end is very similar, just different colors for gradient and stroke)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <gradient  android:startColor="#34F434"  android:endColor="#174717" android:angle="270" />
    <stroke    android:width="2dp"  android:color="#17F717" />
    <corners android:radius="5dp" />
</shape>
Was it helpful?

Solution

Solved by setting padding manually after setting the background to transition - and I had to use twice the padding value that was set in the xml for the button.

eg.

downloadButton.setBackgroundResource(R.drawable.animate_background);
downloadButton.setPadding(28, 28, 28, 28);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top