Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Display GIF as button compound drawable programmatically

For the past few days, I am trying to set the icon of one of my Buttons as a GIF file programmatically. My GIF is located in my assets.

This is the code that I tried. It partially works, but the GIF seems to be stopped and is not playing, like just an ordinary PNG file.

AssetManager assetManager = context.getAssets();
Drawable drawable = null;
    try {
        drawable = Drawable.createFromStream(context.getAssets().open("test.gif"), null);
        button.setCompoundDrawablesWithIntrinsicBounds(drawable, 0, 0, 0)
    } catch (IOException e) {
        e.printStackTrace();
    }

I am expecting the drawable to be playing, just like a GIF.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any help is appreciated. Thank you.

>Solution :

AssetManager assetManager = context.getAssets();

GifDrawable drawable = null;
try {
    drawable = new GifDrawable(context.getAssets(), path);
    button.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
} catch (IOException e) {
    e.printStackTrace();
}

Hey, hope this helps you with your question, you could add this to your .gradle file.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading