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).
Related
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);
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.
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.
i have three button which is arranged in table row .I gave background for the three button but it wraps the button .
For starters read my blog :-)
Now my suggestion is to use background definitions with gradients. They look nice and are simpler to create then background images. As I said in the blog you need three of them for the button to work as expected. I have a demo for you here: button_type_0.xml
You will also need to define the colours: colors.xml
and dimensions: dimens.xml
you might also want to consider different dimensions for various dpi values. for example I use half size corners and border for ldpi: ldpi/dimens.xml
Looks all very complicated at the beginning but it is worth it. In the end it will look like this:
declare new a new xml in the drawable folder with the image/color you can specify image for each event state.
and you can you can set this xml as you background
if your xml is 'res/drawable/abc.xml' then set background as
android:background="#drawable/abc"
You can also use ImageButton
<ImageButton
----------------------
android:src="#drawable/youimage"
/>
The benefit for ImageButton is that, no need of different image/color for highlight.
Ok, I am trying to create a custom view called CheckedRelativeLayout.
It's purpose is the same as a CheckedTextView, to be able to use it in a list of items you want selected or in a Spinner.
It's all working fine now, I extended RelativeLayout and implemented Checkable interface.
However, I am stuck on a quite simple problem: Where can I find the Drawable that CheckedTextView and RadioButton use?
I looked at the sourcecode of both, and they seem to use com.android.internal.R. Well... that's internal stuff. So I can't access it.
Any way to get these Drawables or solve the problem somehow?
look under SDK folder /platforms/android-2.0/data/res/
you can access them by either android.R.drawable ( if public ) or need to copy them as drawable to your project
For sake of completeness:
Here some code pieces that show how you I got it working with above accepted answer.
//Image Setup (Once when creating this view)
ImageView indicator = (ImageView) findViewById(R.id.RadioStatusImage);
indicator.setImageDrawable(getResources().getDrawable(android.R.drawable.btn_radio));
//State Change (In some other method)
android.R.attr.state_checked
if (isChecked)
{
indicator.setImageState(android.R.attr.state_checked, false);
}
else
{
indicator.setImageState(View.ENABLED_STATE_SET, false);
}
invalidate();
To use the new default animated radio button drawable, the correct answer is in the comment of #sidon:
?android:attr/listChoiceIndicatorSingle
Using this in the custom position relative to text:
<RadioButton
...
android:gravity="center_horizontal|bottom"
android:drawableTop="?android:attr/listChoiceIndicatorSingle"
android:button="#null"
/>