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
Related
I've got two images. I want to do so: when user opens an app with a Light theme - the first image is being used as a background for layouts. When user opens the app with a Dark theme - the second image is being used as a background for layouts.
To solve this problem with text colors, we can just use styles.xml and colors-day/night.xml and one line of code: <item name="android:textColor">#color/textColor</item>
I've tried to the same with images and two styles files: <item name="android:background">#drawable/day</item>
But this feature applies background to each element on a screen, not only to the main layouts.
I know, that I can do it programatically by changing a background with if statements and layout.setBackgroundResource(R.drawable.day/night);
But maybe it can be done with XML as in the case of the text color?
To solve this problem with text colors, we can just use styles.xml and
colors-day/night.xml and one line of code: #color/textColor
You need to do the same thing, but instead of res/values/colors.xml, it should be done on the drawable res/drawable/day
So, you'll have a drawable & drawable-night folders in the res directory each of which should have a unique version of the day image with the same name.
They should look like in Android Studio (Android preview):
And normally use the the attribute name="android:background">#drawable/day</item>
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.
I want to perform shared element transition when switching from Activity A to Activity B. The problem is that transition animation is not working smoothly when shared imageViews scale types are different. I am noticing a "bounce" in the very beginning of transition (on not animated imageView scaleType change I guess). The same "bounce" I am noticing when coming back from Activity B to Activity A.
Details:
Activity A contains imageView with scaleType: centerCrop.
Actvity B contains imageView with scaleType: fitXY.
Both ImageView have android:transitionName="sharedView", of course.
My transition set in xml looks like this:
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<changeBounds/>
<changeImageTransform/>
</transitionSet>
In documentation about this config is written:
In combination with ChangeBounds, ChangeImageTransform allows
ImageViews that change size, shape, or ImageView.ScaleType to animate
contents smoothly.
So why my transition is not working smoothly? Or how to animate scaleType change during shared element transition if changeBounds with changeImageTransform doesn't seem to work?
Worth to mention that if I make scale types equal (e.g. centerCrop and centerCrop) of shared views - then transition works smoothly and everything is ok.
I've solved this problem in my case. Here is something you need to check:
Make sure your Shared element is ImageView for both source and target activity (Don't use any container).
Set the transitionName for the ImageView (not its container).
After I updated this, changeImageTransform will do its job changing scaleType smoothly between 2 ImageViews
Had same problem. Solved it by setting same padding property for both ImageView's. I don't know why it works so, but hope it can help someone.
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.
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)