I have been working on android on some time, but there is a common problem which i face many times. The problem is there are multiple view where in i assign the view a background drawable for background, such as:
As can be seen in the above example the background drawable for both the views is same but only difference in the both background drawables xml file is the solid and stroke colour. Is there any other better way to do this where i just create a generic background drawable and change the colour of the components from xml and id not possible in xml instead of creating a new drawable each time for each solid colour and stroke colour variation.
No this is not possible in XML. However, it is possible using Java.
ShapeDrawable shapeDrawable = (ShapeDrawable) ContextCompat.getDrawable(getActivity(), R.drawable.name);
shapeDrawable.getPaint().setColor(ContextCompat.getColor(mContext,R.color.colorToSet))
Related
Using a drawable reference to myshape.xml I can set a background like this:
v.setBackgroundResource(R.drawable.myshape);
But what if I want/need to set it all programatically without using drawable reference? Is it possible somehow to supply the <shape/> as a string to set such background without a need of the external resource file drawable\myshape.xml?
You can set the background using a drawable, via the call to v.setBackground() function.
The question should have been, can you create a shape drawable programatically, not via resource.
You can create some shapes programatically, not from a string. Something like this
ShapeDrawable drawable = new ShapeDrawable (new OvalShape());
There are other shapes and options, but you cannot parse a string.
is there any systemical possible way (i mean in code) to make a blur background depends on the image user open, the color must be similar to the image that will open.
for example, the background on this page is grey.
You will need to get the dominant color from the image you are using then create a gradient drawable between a starting color and your dominant color. There are multiple ways to find dominant colors which you can read up on here: Finding the dominant color of an image in an Android #drawable
From there you create a drawable and set the background of your view to that drawable:
// get the drawable from the image view
// could also be pulled from resources if available
Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
int color = getDominantColor(bm);
GradientDrawable gradient = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
new int[] {0xFFF,color});
gradient.setCornerRadius(15f);
contentView.setBackground(gradient);
See this library to make blur image https://github.com/wasabeef/Blurry
I am setting background color of vector drawable dynamically using:
Drawable mDrawable = ContextCompat.getDrawable(context, item.getCategoryIconId());
mDrawable.setColorFilter(new
PorterDuffColorFilter(ContextCompat.getColor(context, item.getCategoryColorId()), PorterDuff.Mode.MULTIPLY));
holder.expenseCatIcon.setImageDrawable(mDrawable);
But while setting color to same Vector drawable again inside onBindViewHolder it gets updated for every row of recyclerview where I am using that drawable.
Above I added the view of which I am talking about, I have a different activity where I can create items like "food", "gult", "loot" and "ffd" etc.
And I made sure that the resource color id is different, result from Log:
Name:food Color:2131623945
Name:gult Color:2131624100
Name:loot Color:2131624174
Name:ffd Color:2131624084
What my understanding is that when I am setting the color filter to vector drawable the color comes in last gets applied to that vector drawable and that results in same color for all the same drawable resource.
Is there any thing I could do to make this work?
As the views get recycled so your applied changes show for all drawables in onBindHolder. Maybe add some conditional statements if you want a different output.
Android SDK provides the following icons.
Is there a way to set a color to those .. and if possible, how to do so?
<ImageView
android:id="#+id/share_button"
style="#style/IconNav"
android:src="#android:drawable/ic_menu_share"/>
<ImageView
android:id="#+id/bookmark_button"
style="#style/IconNav"
android:src="#android:drawable/ic_input_get"/>
UPDATE
After doing a complete refresh of the project, it turns out the tint attribute in Xml did the trick.
For the short answer
.. this is the solution that worked for me - adding the property to the ImageView xml:
android:tint="#color/grass_dark"
The answer from #goldenb is a thorough run through of the different ways to solve for this, so am marking that one as the answer.
You can indeed use a tint as a way of changing an ImageView's colour, BUT you should be reminded that the android:tint will always be applied on top of the original colour.
as stated by blogger danlew
ImageView's tint mixes the tint color with the original asset. What you want is for the tint color to take over entirely; instead it
applies the tint on top of the existing color. So, for example, if the
source asset is black, and you want it to be #77FFFFFF (a translucent
shade of white), you'll actually end up getting that shade of white
with a black background beneath it.
android:tint is limited to ImageView. You want to be able to tint any Drawable in any View.
One possible alternative would be for you to use android ColorFilter
According to the official documentation:
A color filter can be used with a Paint to modify the color of each pixel drawn with that paint. This is an abstract class that should never be used directly.
There are lots of more or less complex things you can do with ColorFilter but how can you apply this then?
One simple example from another so question is:
//White tint
imageView.setColorFilter(Color.argb(255, 255, 255, 255));
or
imageView.setColorFilter(ContextCompat.getColor(context,R.color.COLOR_YOUR_COLOR))
Or a more complete answer here in SO from here
ImageView redCircle = (ImageView) findViewById(R.id.circle_red_imageview);
ImageView greenCircle = (ImageView) findViewById(R.id.circle_green_imageview);
ImageView blueCircle = (ImageView) findViewById(R.id.circle_blue_imageview);
// we can create the color values in different ways:
redCircle.getDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY );
greenCircle.getDrawable().setColorFilter(0xff00ff00, PorterDuff.Mode.MULTIPLY );
blueCircle.getDrawable().setColorFilter(getResources().getColor(R.color.blue), PorterDuff.Mode.MULTIPLY );
You should check these links if you want to learn more
SO - What is the difference between background, backgroundTint, backgroundTintMode attributes in android layout xml?
setColorFilter()
Fast Android asset theming with ColorFilter
SO-Modifying the color of an android drawable
You can use a bitmap with a tint. Add this to your drawables folder.
ic_input_get_colored.xml :
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#android:drawable/ic_input_get"
android:tint="#color/yourDesiredColor"/>
I have an image in a resource file.
Drawable draw = getResources().getDrawable(R.drawable.my_icon);
The image has a transparent background.
Is there a way to programmatically set the background color to the Drawable before using the end product further in my code?
I think Drawing with PorterduffXferMode may help you in your case. This way you can merge two images (your image and a overlay completly in your color you want to replace the transparent pixels with) in many different ways.
Different porterduff modes explaned:
http://www.ibm.com/developerworks/java/library/j-mer0918/
Android example:
http://www.vogella.com/code/ApiDemos/src/com/example/android/apis/graphics/Xfermodes.html
This way you draw the result inside a new Bitmap. (SRC_OVER should work in your case if your image is the src and the background is used as the dst)
setColorFilter() with Porterduff SRC will break the transparent of drawable.
I used this in my code, and it work
disabledIcon = ContextCompat.getDrawable(getContext(), resId);
disabledIcon = DrawableCompat.wrap(disabledIcon);
disabledIcon.mutate(); // to not share its state with any other drawable
DrawableCompat.setTint(disabledIcon, ContextCompat.getColor(getContext(), R.color.button_text_disabled));