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.
Related
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 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))
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.
Is there is any other way besides passing drawable to the constructor to set drawable in ImageSpan? I couldn't find any other way
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));