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"/>
Related
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" />
I am trying to make a icon button that when the user clicks on it, as a click effect, the color of the icon changes. for this, i tried a selector xml file that select different png files, this is a simple selector code that I thaught it would work, but there must be something that I am missing, the xml file looks like this:
<?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/continue_pressed"/>
<item android:state_pressed="false"
android:drawable="#drawable/continue_not_pressed"/>
</selector>
however when I try to run the app on emulator, it crashes! and there is nothing in log so i can tell what the problem is. but when i try just one png file for the background of the button it works!
the xml code for the button looks like this:
<Button
android:id="#+id/about_icon1"
android:background="#drawable/button_continue"
android:layout_width="100dp"
android:layout_height="100dp"
android:alpha="0.5" />
How can I fix this?
Your selector only has state_pressed true and false (this can't be used) and it doesn't have a default one. Try do something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/button_focused" android:state_focused="true"/>
<item android:drawable="#drawable/button_normal"/>
</selector>
And in the xml where you set your Button layout:
<Button
android:id="#+id/about_icon1"
android:background="#drawable/your_selector_xml_name"
android:layout_width="100dp"
android:layout_height="100dp"
android:alpha="0.5" />
Notice you should put the selector code above in another xml file and that xml file should be in your drawable directory. So that when you set the background for your Button it will use the selector xml file to be your Button's 'background'.
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've been trying to customize the toggle button look but with no success.
Here is how I want it to look like:
Can someone give me a tutorial?
create toggle_selector.xml in res/drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/toggle_on" android:state_checked="true"/>
<item android:drawable="#drawable/toggle_off" android:state_checked="false"/>
</selector>
apply the selector to your toggle button
<ToggleButton
android:id="#+id/chkState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/toggle_selector"
android:textOff=""
android:textOn=""/>
Note: for removing the text i used following in above code
textOff=""
textOn=""
I don't know if this is the best solution but it worked fine for me:
1.- Decide how big do you want the toggle button. In my case width 56dp and height 76dp.
2.- Create the icon set 56px-76px for mdpi, 84px-113px hdpi, same for xhdpi and xxhdpi
3.- Move the icons in the corresponding drawable folder. In my case 20 icons 5 in each folder, named ic_name1_on, ic_name1_off [...] ic_name5_off
4.- Create the following xml files in a new folder called drawable (if it does not exist yet):
ic_name1_toggle.xml
ic_name1_toggle_bg.xml
ic_name2_toggle.xml
(...)
ic_name5_toggle_bg.xml
5.- In ic_name1_toggle.xml the code must be:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="false"
android:drawable="#drawable/ic_name1_off" />
<item
android:state_checked="true"
android:drawable="#drawable/ic_name1_on" />
</selector>
6.- In ic_name1_toggle_bg.xml the code must be:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+android:id/background"
android:drawable="#android:color/transparent" />
<item android:id="#+android:id/toggle"
android:drawable="#drawable/ic_name1_toggle" />
</layer-list>
7.- Finally in your layout.xml:
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="56dp"
android:layout_height="76dp"
android:background="#android:color/transparent"
android:button="#drawable/ic_name1_toggle_bg"
android:textOff=""
android:textOn="" />
I think you need to define a custom background for your button.
Take a look at the Developer Guide on customizing a Button's background.
However, in step Three, Create a new XML file in the res/drawable/ directory Use this Xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_da"
android:state_checked="true" />
<item android:drawable="#drawable/button_nu" />
</selector>
The element android:state_checked="true" is what defines that state as the checked background.
Let me know if this works for you.
Create selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_da" android:state_checked="true"/>
<item android:drawable="#drawable/btn_nu"/>
</selector>
and use it as background for your ToggleButton.
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" />