How to create radio group of a real buttons? - android

I'd like to create a button group of a "real" buttons?
I'd like to display to user buttons which contains his attending option: Yes, No and Meybe.
I'd like to use buttons for this display.
How it can be done?

Just create a selector xml file in drawable folder
checkbox_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="#drawable/checkbox_active" />
<item android:state_checked="false" android:drawable="#drawable/checkbox_inactive" />
</selector>
And apply this background to your radio buttons
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/checkbox_background" />

Related

How to set the default background for android checkbox

I have
Checkbox.setButtonDrawable(R.drawable.ic_checkbox_disabled);
Here i am setting to custom image
How to set to default check box background
I tried:
Checkbox.setButtonDrawable(null);
But entire box diseappered
So I need the resource name of the checkbox android uses
Something like:
Checkbox.setButtonDrawable(R.android.resourcename);
Any solution for this
Create a new selector file in drawable folder with name checkbox_selection :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/checked"
android:state_checked="false"/>
<item android:drawable="#drawable/uncheck"
android:state_checked="true"/>
<item android:drawable="#drawable/checkbox"/>
</selector>
Modify your checkbox view in xml file as show below :
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:button="#drawable/checkbox_selection"
android:text="CheckBox"
android:textColor="#color/Black" >
</CheckBox>
In drawable folder, make one file and name anything you want. Then paste this.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/radio_checked_icon" android:state_checked="true"/>
<item android:drawable="#drawable/radio_unchecked_icon" android:state_checked="false"/>
</selector>
radio_checked_icon and
radio_unchecked_icon, these are the custom images you want to put, when your checkbox is clicked, then radio_checked_icon, else radio_unchecked_icon
and then in layout use like this:
<CheckBox
android:id="#+id/tiny_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:button="#drawable/name_of_your_file_present_in_Drawable"
android:padding="#dimen/padding_short" />

Change hover image button without selector on android

I have many buttons for my view. Each button has different image resource. I want to change the click and hover effect for each button like this:
Button 1
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn1_selector"
android:text="name"
/>
btn1_selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/hover_btn" android:state_pressed="true"></item>
<item android:drawable="#drawable/hover_btn" android:state_focused="true">/item>
<item android:drawable="#drawable/btn1"></item>
</selector>
Button 2
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn2_selector"
android:text="name"
/>
btn2_selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/hover_btn" android:state_pressed="true"></item>
<item android:drawable="#drawable/hover_btn" android:state_focused="true">/item>
<item android:drawable="#drawable/btn2"></item>
</selector>
// etc...
The problem is that I have to create many selectors for all buttons. But I just need to change the hover state for all of them with 1 image hover_btn.png. Is there any way to have a hover state by default without creating all selectors for all buttons?

Change button style radio group in Android

I would like to change the default button button style of Android radio buttons to suit my theme. How could I achieve that?
Create a selector drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/radio_selected" android:state_checked="true" />
<item android:drawable="#drawable/radio_normal" />
</selector>
then in your xml of the radio button
android:button="#drawable/(your drawable name)"
example:
<RadioButton
android:id="#+id/radio_button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/your_drawable_name"
android:text="your_text" />
See Adding custom radio buttons in android. Hope this helps you.

Android state list for compound drawable

Is it possible to define state list for compound drawables in Android?
This is my button in layout xml:
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/btn_top"
android:text="#string/button_text" />
This is my drawable xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true" android:drawable="#drawable/btn_top_enabled" />
<item android:drawable="#drawable/btn_top_disabled" />
</selector>
This works for background, but its not working for drawableTop. The first state is always displayed. Is this even possible in Android, and if so am I missing something?
look ad TextView.java source code and you will see that yes, you can use StateListDrawable as a compound Drawable that displays according to TextView state
Try:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true" android:drawable="#drawable/btn_top_enabled" />
<item android:state_activated="true" android:drawable="#drawable/btn_top_enabled" />
<item android:drawable="#drawable/btn_top_disabled" />
</selector>
And
button.setActivated(true);

Specific theme for checkbox

I want to make check-box like this
How can I implement it myself?
Create a selector xml file such as this : you have to cut this image and Save as yellow_show name and another image which you have to cut only square of this box without checkmark and Save that image as yellow_hide..
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/yellow_hide" />
<item android:state_checked="false" android:drawable="#drawable/yellow_show" />
</selector>
save this xml file in your res\drawables\ folder. Then inside your layout file apply it to your checkBox like this:
<CheckBox
android:text="Custom CheckBox"
android:button="#drawable/checkbox_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

Categories

Resources