Set drawable in ImageSpan programmatically? - android

Is there is any other way besides passing drawable to the constructor to set drawable in ImageSpan? I couldn't find any other way

Related

Setting a shape drawable as background programmatically without a drawable reference

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.

Set vector drawable programmatically pre Lollipop

Very common problem with a twist to which I couldn't find a solution for.
I am setting my vector programmatically. I want to be able to change the tint color programmatically too.
Found some solutions such as
Programmatically tint a Support Vector
ImageView iv = ....
Drawable d = VectorDrawableCompat.create(getResources(), R.drawable.ic_exit_to_app_24dp, null);
d = DrawableCompat.wrap(d);
DrawableCompat.setTint(d, headerTitleColor);
iv.setImageDrawable(d);
The main problem comes with
iv.setImageDrawable(d);
I found that prelolipop only accepts setting view's drawable with
iv.setImageResource(int resource)
I couldn't find any solutions for setting it with a drawable file.
Use AppCompatImageView which has setImageDrawable() method.

Configurable background drawable

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))

Vector drawable color is not getting updated inside onBindViewHolder

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.

ImageButton difference between setBackground and setImageResource and setImageDrawable

What is the difference between android:setBackground & android:setImageResource when used with an ImageView
I was trying to change the icon of imageButton onclick. when I used:
flashBTN.setImageResource(R.drawable.torch_icon_grey);
I am getting the button covered in grey and the correct drawable doesn't show.
And when I am trying :
flashBTN.setBackground(R.drawable.torch_icon_grey);
I am getting error:setBackground ca't be applied to int
Bonus: How setImageDrawable is difference?
you can read this. I think you are talking about this in xml:
android:background -> A drawable to use as the background, it could be just a color in HEX notation or a drawable.
android:src -> Sets a drawable as the content of this ImageView.
However in java you can use setImageDrawable(Drawable drawable) for setting a drawable as the content of this ImageView.

Categories

Resources