Get back the old drawable in Compound button? - android

How to remove the current drawable and go to old drawable state;
RadioButton radioButton = (RadioButton) findViewById(R.id.opt1);
radioButton.setButtonDrawable(R.drawable.ic_done);
For the next question, it should go to the old drawable.
So I have tried radioButton.setButtonDrawable(0) but it removed the drawable.
Is there any way to getDefaultDrawable() according to Theme.AppCompat?

You can do something like this:
Before changing background
RadioButton radioButton;
Drawable defaultRadioButtonBackground = radioButton.getBackground();
When you want to change back to default background
radioButton.setBackground(defaultRadioButtonBackground);

Related

Change background color of programmatically created radio button

I used various method but I couldn't find a way to change color of a RadioButton that's created programmatically.
Is there any other way than using a background Image?
with minSDKVersion is 21. The below way I'm creating my RadioButton.
RadioButton radio = new RadioButton(co);
To change RadioButton button color programmatically use this.
because your minSdk is 21. You can use setButtonTintList this way.
RadioButton raPrivate = new RadioButton(co);
int textColor = Color.parseColor(#000000);
raPrivate.setButtonTintList(ColorStateList.valueOf(textColor));
Try this if you want to change background color of Radio Button:
radioButton.setBackgroundColor(Color.GREEN);

Android: set background color of radiobutton text programatically

When i try to change background color of radio button text programatically like this:
rb.setBackgroundResource(R.drawable.mycustombackground);
The background covers both the button and the text.
I only want to change the background color of the text part (not the radio button).
How to achieve that?
Ok, lets see if it works:
Spannable span = new SpannableStringBuilder(rb.getText().toString());
span.setSpan(new BackgroundColorSpan(Color.RED), 0, rb.getText().toString().length(), SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE);
rb.setText(span);
Replace Color.RED with whichever color you want. Don't forget you have to grab it from the resources if you want it. I don't think it works with drawables.

How to set an image as a RadioButton argument programmatically?

I have a RadioGroup which consists of five RadioButtons. What I'd like to achieve is setting an image as a RadioButton argument. So far found this way:
radio0.setBackgroundResource(<drawable ID, int>);
but it gives something like this (descriptions "isSelected" added in an image editor):
Is there any way to programmatically set background properties and move the image to the right?
Or maybe there is another way to set an image next to RadioButton instead of using it as a background image?
I'm interested only in programmatical solutions, as the RadioGroup is cleared up and filled again during loop work.
A friend of mine helped me to figure out this issue.
Instead of the code line posted in the question I use this one now:
radio0.setCompoundDrawablesWithIntrinsicBounds(0, 0, <drawable ID, int>, 0);
The third parameter corresponds to android:drawableRight XML parameter. More on this: click.
Thanks to it a picture is set to the right of the RadioButton and finally it looks like it should.

Getting ListView drawables and applying it manually

i am trying to apply the style that a list item has, when it is selected, to a View (In my case a TextView)
Example
here it would be the orange-painted style. I found that this look is defined by a Drawable Object. So i tried, so get the drawable and applied it to my view
TextView tv = new TextView(this);
tv.setText("Hello, Android");
tv.setBackgroundDrawable(new ListView(this).getSelector());
setContentView(tv);
but it doesn't work.
Does anybody have an idea how to do that?
Ok I figured out how to do that. I found out hat ListView uses the ColorStateList list_selector_background which is defined in android.R.drawable. Using ResourceBrowser I got to know that the fourth color is the selected drawable so i added the following to my code
StateListDrawable listDrawables= (StateListDrawable)getResources().getDrawable(android.R.drawable.list_selector_background);
listDrawables.selectDrawable(3);
Drawable highlightDrawable = listDrawables.getCurrent();
Now I can set my Background using higlightDrawable. I don't know if it is possible to access the drawable in xml I did not try it yet. Thanks for help!
It would be a pain to do it by code.
You need to implement a selector and let android know which resource needs to be used when the view is pressed, enabled or focused.
Here is sample example on how to achieve the same.

RadioButton Image

How would I modify the .xml and .java files so that a RadioButton shows one image when selected and another image when not selected?
Example for RadioButton I am learning from: http://www.androidpeople.com/android-radiobutton-example/
Here is an example that uses StateListDrawable and RadioButtons, and seems to do what you want.
you need to set its android:button property to a StateListDrawable that has at least two states: selected=true and a default one (unselected).

Categories

Resources