Android: Adding a Drawable to an existing LayerDrawable - java

I have a LayerDrawable which I construct with an array of Drawables of 5 Drawables.
Now let’s say in run-time I want to add another Drawable to my LayerDrawable, in response to an event. How do I do that without having to re-create the LayerDrawable, this time with an array of Drawables of 6 Drawables?
Thanks.

After LayerDrawable is created, new Drawables can not be added to it.
See the source of LayerDrawable: array of drawables is saved in mLayerState.mChildren and is only set in Constructor.
However, setDrawableByLayerId(..) can be used to exchange an existing Drawable with a new one.

You can addLayer since Api level >= 23:
https://developer.android.com/reference/android/graphics/drawable/LayerDrawable.html#addLayer(android.graphics.drawable.Drawable)

Related

How can I create a ListView of videos, and images?

How can I create a ListView of videos?
I explain myself a little better, I want to make an app with a listview that contains short videos.
I create an activity list_item where I want to put images and text. For example in the first list item I want you to put a thumbnail and a description, for example: horse accident and in the second list_item put another thumbnail with another description, for example: squirrel attack.
I know how to create a list view that shows the same image in all the list_items, but I don't know how to make a list view that contains different videos, images and text for each list_item.
The images, the video and are stored in the assets and raw folder, respectively.
Do you have any idea how to fix this?
Use this library for videos using ListView :
implementation 'cn.jzvd:jiaozivideoplayer:6.2.12'
use this link for documentation : https://github.com/lipangit/JiaoZiVideoPlayer

android: getBackground.setAlpha() changes alpha of all activities with same background-image

For example, if I add the following line to my 2nd activity, the alpha value for the background image will also change in my 1st activity, because I used the same image.
findViewById(R.id.main).getBackground().setAlpha(100);
But I don't want that. The two activities should have different alpha values, but still have the same drawable as background image. How can I achieve this?
You should call mutate() to create a separate instance before setting the alpha. As per the documentation:
A mutable drawable is guaranteed to not share its state with any other drawable. This is especially useful when you need to modify properties of drawables loaded from resources. By default, all drawables instances loaded from the same resource share a common state; if you modify the state of one instance, all the other instances will receive the same modification.

display image from array in android

I declared the following array which contains images:
private int[] LEAVES = {
R.drawable.leaf_green,
R.drawable.leaf_red,
R.drawable.leaf_yellow,
R.drawable.leaf_other,
};
If I would want to show one image from the array how can I refer to a specific image? let's say I would want to refer to LEAVES[1]. How can I display it?
Actually setImageResource method sets a drawable as the content of ImageView. It takes id of drawable as argument. Here we have array of drawable ids. we can pick drawable id from array to use. below code shows syntax of of the method.
imageView.setImageResource(LEAVES[1]);

Temporarily change drawable color

In one app I'm developing I'm trying to programatically create an ImageButton that is a copy of the selected ImageButton, but the image is colorized in a different way, let's say red.
If I use the PowerDuff.Mode.MULTIPLY:
clonebutton.getDrawable().setColorFilter(0xFFFF0000,Mode.MULTIPLY);
Then even the original ImageButton changes its color to red since they share the same drawable. Is there a way to apply the filter only on the clonebutton without using two different drawables? For instance is it possible in some way to put a colorize layer on top of clonebutton without editing the drawable?
Update
I set the drawable as mutable:
Drawable d = swipebutton.getDrawable();
d.mutate();
d.setColorFilter(0xFFFF0000,Mode.MULTIPLY);
swipebutton.setImageDrawable(d);
This prevents my clonebutton to share the state of its drawable to other views.
Drawable buttonBackground = clonebutton.getDrawable();
buttonBackground = buttonBackground.mutate();
buttonBackground.setColorFilter(0xFFFF0000,Mode.MULTIPLY);
Make this drawable mutable. This operation cannot be reversed. A
mutable drawable is guaranteed to not share its state with any other
drawable. This is especially useful when you need to modify properties
of drawables loaded from resources. By default, all drawables
instances loaded from the same resource share a common state; if you
modify the state of one instance, all the other instances will receive
the same modification. Calling this method on a mutable Drawable will
have no effect.
On Lollipop you don't have to do this programmatic, i.e. colorfilters, at all if you don't want to. You can do it just by setting a tint on an xml drawable.
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/ic_back"
android:tint="#color/red_tint"/>
This might not work if you have an unlimiited number of colors but if they are limited this is a really good option. Check out my blog post for more information.
Drawable d=clonebutton.getDrawable()
d.setColorFilter(0xFFFF0000,Mode.MULTIPLY);
clonebutton.setDrawable(d);
try this: or take the below code as per your need
switch(v.getId())
{
case R.id.bt1:
Drawable d=b11.getBackground();
d.setColorFilter(Color.CYAN,PorterDuff.Mode.MULTIPLY);
b11.setBackgroundDrawable(d);
b12.setBackgroundResource(android.R.drawable.btn_default);
break;
case R.id.bt2:
//b2.getBackground().setColorFilter(Color.CYAN,PorterDuff.Mode.MULTIPLY);
Drawable dd=b12.getBackground();
dd.setColorFilter(Color.CYAN,PorterDuff.Mode.MULTIPLY);
b12.setBackgroundDrawable(dd);
b11.setBackgroundResource(android.R.drawable.btn_default);
break;
}
switching color of buttons after onclick

How do I insert multiple image in Edit Text in android

I am trying to insert multiple images in one editText. I have used following code to attach image to EditText
txt = (EditText)findViewById(R.id.text);
Bitmap bitmap = BitmapFactory.decodeFile(attach);
Drawable drawable = new BitmapDrawable(bitmap);
txt.setBackgroundDrawable(drawable);
But the problem is it only attach one file and show, If i used array then only the last image is only show. at any how it only shows one Image. Is there any way to show multiple images in one Editbox. Thanks in advance.
Because you can only have one background.... if you want more then there is a layer Drawable which you can use and also you can put the button in a frame layout and add a couple of imageViews below/over it for the rest of the images.
But the best solution will probably be instead of having a couple of bitmaps to just make a single one in Photoshop or equivalent photo editing app and place that one.
According to docs: http://developer.android.com/reference/android/view/View.html#setBackgroundDrawable(android.graphics.drawable.Drawable)
You are not able to provide more than one Drawable item in argument
And I don't know if you understand what is .setBackgroundDrawable(d) method purpose, but it is not meant to be used for displaying images inside text but to set Background of EditText View.
So you're not inserting images in EditText but setting its background,
look for some Rich Text Edit components usable for Android
such as http://code.google.com/p/android-richtexteditor/
or others..
You can probably use a LayerDrawable with the constructor LayerDrawable(Drawable[] layers) to chain together several images and display them as an EditText's background, though as Marek said, I suspect you're looking for something other than setting images in the background.

Categories