Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a layer-list drawable?

Trying to learn some new things and can't figure this one out, any help is appreciated. Given this simple code which is right from Google's documentation:

layers.xml:

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android">     <item android:id="@+id/first_image">       <bitmap android:src="@drawable/android_red"         android:gravity="center" />     </item>     <item android:id="@+id/second_image" android:top="10dp" android:left="10dp">       <bitmap android:src="@drawable/android_green"         android:gravity="center" />     </item>     <item android:id="@+id/third_image" android:top="20dp" android:left="20dp">       <bitmap android:src="@drawable/android_blue"         android:gravity="center" />     </item> </layer-list> 

And main.xml:

<ImageView     android:id="@+myImageView"     android:layout_height="wrap_content"     android:layout_width="wrap_content"     android:src="@drawable/layers" /> 

Question: How do I programmatically reference one of the drawables in the layer-list so I can change it to a different drawable?

Thanks.

So now I have my new drawable that I want to swap in in variable myImage correct? Now how do I get that into the layer-list? I assume I use findDrawableByLayerId and setDrawableByLayerId, but how exactly? Or I could be doing it completely wrong! Help!

What am I trying to do? Change the drawable that's displayed. For example suppose I had another drawable called "android_purple", how would I switch it in for the "android_green" drawable in the layer-list example above?

EDIT:

With my limited knowledge here is my code so far (which does not work, I get error that setDrawableByLayerId is undefined):

Resources res = getApplicationContext().getResources(); BitmapDrawable newImage = (BitmapDrawable) res.getDrawable(R.drawable.android_purple); //Get replacement image, can't use LayerDrawable or get error.    boolean layer_drawable_changed = (setDrawableByLayerId((R.id.second_image), newImage)); //Set new drawable? Error on this line. 
like image 884
ShadowGod Avatar asked Nov 05 '11 05:11

ShadowGod


People also ask

What is a layer list in android?

Layer List. A Drawable that manages an array of other Drawables. These are drawn in array order, so the element with the largest index is be drawn on top.

What is res drawable in android?

A Drawable resource is a general concept for a graphic which can be drawn. The simplest case is a graphical file (bitmap), which would be represented in Android via a BitmapDrawable class. Every Drawable is stored as individual files in one of the res/drawable folders.


2 Answers

Question: How do I programmatically reference one of the drawables in the layer-list so I can change it to a different drawable?

Here's what I would try:

Step #1: Put android:id attributes on the <item> elements as illustrated in the documentation

Step #2: Cast the getDrawable() result to a LayerDrawable

Step #3: Call mutate() if you do not want to affect anyone else using the Drawable (may be optional)

Step #4: Call setDrawableByLayerId() to set/replace a Drawable for a given layer ID

like image 84
CommonsWare Avatar answered Oct 14 '22 01:10

CommonsWare


Step 1:Retrieve the Layer-list from the drawable folder: (Mine was called digits at /res/drawable/digits.xml)

LayerDrawable ld = (LayerDrawable) getResources().getDrawable(R.drawable.digits); 

Step 2: Name the replacing image (not the the image already in layer-list)

Drawable replace = (Drawable) getResources().getDrawable(R.drawable.replacing_image); 

Step 3: Replace the Image using the setDrawableByLayerId(int id, Drawable d) method

boolean testfactor = ld.setDrawableByLayerId(R.id.first_image, replace); 

This returns a boolean value that will return false if replacing the image failed

Step 4: Load main Imageview (basically id of layer-list in the actual xml layout code)

ImageView layoutlist1 = (ImageView)findViewById(R.id.layout_list1); 

Step 5: (re)-Set the Imageview with your updated LayerDrawable by saying:

layoutlist1.setImageDrawable(ld); 

RESOURCES: I'm a software and mobile app developer.

I looked all over the web and alot of Stackflow.com in order to get to this point. All the answers were like the ones that were already on this page. I was trying to do the same thing. Then I thought about the idea of "updating the image" and came to this.

This should/can also work for when you want to put a Bitmap in place of one the items on your list. You just have to use the replace (setDrawableByLayerId) method. to replace on ids. ImageView has a lot of methods.

This was also helpful: LayerList drawable + conversion to a Bitmap

like image 37
Kavita Avatar answered Oct 14 '22 01:10

Kavita