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.
Related
I'm trying to style my RadioButton as normal buttons and support color change on the buttons, with ripple effect when one of them is clicked.
What happens now, is that all buttons are grey, they ripple in purple when they are clicked but don't change their color to purple permenently - which is the problem I'm trying to solve.
Thisi s how he buttons look right now
RadioButton in whatever.xml
<RadioButton
style="#style/Widget.AppCompat.Button"
android:id="#+id/product_size2"
android:layout_width="48dp"
android:layout_height="48dp"
android:text="S"
android:button="#null"
android:textColor="#color/material_light_white"/>
Relevant items in style.xml
<item name="android:colorButtonNormal">#color/material_grey_700</item>
<item name="android:colorControlHighlight">#color/material_purple_500</item>
I want the buttons to stay purple when they are chosen - solution?
radiobutton_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="#color/material_purple_500" />
<item android:state_checked="false" android:drawable="#color/material_grey_700" />
</selector>
And use it as a Background:
android:background="#drawable/radiobutton_background" />
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?
I would like to simply have the "tick" without the square. Also only as ImageView would be fine. Anyone knows the name of the resource in Android?
Just set selector to drawable left to Checkbox in whatever shape you desire.
Do like this.
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#null"
android:drawableLeft="#drawable/checkImageSelector" />
android:button="#null" will remove the default image of square with tick and drawableLeft will place your image in place of that.
checkImageSelector.xml will be like this.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/check" android:state_selected="true"/>
<item android:drawable="#drawable/unchecked"/>
</selector>
You need two images for checked and unchecked state.
Create a selector with same resources :
checkbox_selector.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/checked_image" android:state_checked="true"/>
<item android:drawable="#drawable/unchecked_image"/>
</selector>
Then set this selector as button :
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/checkbox_selector" />
Activity:
CheckBox.setButtonDrawable((int)getResources().getColor(R.color.transparent));
Fragment:
CheckBox.setButtonDrawable((int)getActivity.getResources().getColor(R.color.transparent));
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" />
I want to create custom ImageButton, but to work like on/ off button. On click on button image to be changed to pressed(until another button is pressed)!
On picture This month button is on , This year off .
How can I create button like this?
Do I need to use and how ?
Thanks
Use the selector XML tag to help you achieve this. Here, see this link about StateListDrawables. So their example shows
a button.xml file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
and then links an actual button to that xml:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/button" />
Sergey Glotov and wheaties thanks for your help
For every ImageButton I create separate selectors for default and pressed state, I create ImageButton in xml:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/month_button"
android:id="#+id/btnMonth"
android:onClick=ButonMonthClick"/>
And onClick event for all ImageButton I change background image programmatically:
btnWeek.setBackgroundResource(R.drawable.week_pressed);
btnMonth.setBackgroundResource(R.drawable.month_default);
Thant solve my problem!