Android convert Transparent color to drawable - android

Is there a way how can I set the buttonDrawable of a CheckBox to transparent ?
Setting it like this: setButtonDrawable(Color.TRANSPARENT); doesn't work. I don't remember if there was a solution converting a color as a Drawable and use it like this for example. What I've searched so far and I saw it worked is to set the Transparent Color from XML but I don't have one and I would like to do it programmatically.

you can give new ColorDrawable(Color.TRANSPARENT) a try

I tried this and it worked:
new ColorDrawable(Color.parseColor("#00ffffff"));

Related

How to set layout background tint from string programmatically?

I tried this code:
LinearLayout someLayout=(LinearLayout)view.findViewById(R.id.someLayout);
someLayout.setBackgroundTintList(context.getResources().getColorStateList(Color.parseColor("#ff8800")));
But I'm getting an error: android.content.res.Resources$NotFoundException
I'm getting the color hex from external source so I can't embed it in colors.xml.
Also I want to change the tint, not the background so setBackground is not an option.
I figured I can't use getColorStateList() so I searched for another way to do it.
At the end I was able to set color tint using the following code:
LinearLayout someLayout=(LinearLayout)view.findViewById(R.id.someLayout);
someLayout.getBackground().setColorFilter(Color.parseColor("#ff8800"), PorterDuff.Mode.SRC_ATOP);
This worked as if I changed the backgroundTint property in the xml file, so it's perfect for my problem.
I was able to manage using the following line. change it to your circumstances.
myView.getBackground().setTint(currentView.getResources().getColor(R.color.colorAccent));
For Kotlin ,
I modified #Krestek answer :
someLayout.background.setColorFilter(Color.parseColor("#ff8800"), PorterDuff.Mode.SRC_ATOP)
You can't do this like that because getColorStateList method expect int id of resource, and you are passing RGB color int. You should create color state list following this link
and then set it like this:
.getColorStateList(R.color.your_xml_name)

How to clear color for TopDrawable of Textview?

In my application I change color of Top drawable of Textview using below code.
tvTopDrawable.setColorFilter("#E1BEE7", Mode.SRC_ATOP);
Now I want to clear this color from my drawable.
I have tried tvTopDrawable.clearColorFilter();
But it seems its not working sometime.
I used that same image in other Activity of my Application.
Where to write clearColorFilter() so it clears the color from drawable,Can anyone help with this?
Thanks in Advance.
Try setting color filter to transparent
tvTopDrawable.setColorFilter("#00E1BEE7", Mode.SRC_ATOP);

Apply Color Filter with xml in ImageView

I've got an ImageView and programmatically I can change its color using imageView.setColorFilter(Color.RED).
There is something similar using xml??
Yes, it's android:tint. Here you have more info.

How to reset a button's background color to default?

I read a couple of posts but none of them had the working solution.
Once you do
button.setBackgroundColor(0x00000000);
How do you revert the button's background color back to default color?
use:
btn.setBackgroundResource(android.R.drawable.btn_default);
If the background color was set using
btn.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
it can be reset using:
btn.getBackground().clearColorFilter();
In contrast to button.setBackgroundColor() setting the color this way preserves the button's shape.
Nobody mentioned TRANSPARENT
use it like this
findViewById(R.id.button_id).setBackgroundColor(Color.TRANSPARENT);
Thank me later
this worked better for me :
Button defbtn=new Button(this);
btn.setBackground(defbtn.getBackground());

android change view's background color when state changes

how to use color state list for background?
I know android:background="#drawable/drawable_selector", but android:background="#color/color_selector" will cause exceptions.
but android:background="#FFFFFF" works again, can anyone explains why?
now i want to change a layout's background color(not a drawable) when it's pressed,
how to do it?
dynamically you can change like this. Use this if it is useful for you -
textView.setBackgroundColor(Color.parseColor(getResources().getString(R.string.red)));
put the color in res/values/colors.xml,
like #FFFFFF,
and then create a drawable xml in drawable directory,
,that is ok.

Categories

Resources