Checkbox without the square android - android

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));

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" />

Android, Hide stock toggle when using custom background?

I've got a custom drawable for my toggle switch
// toggle_selector.xlm
<?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>
Which I then apply to the switch
<Switch
android:id="#+id/geoLocationsToggle"
android:background="#drawable/toggle_selector"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textOff=""
android:textOn=""
android:textSize="0dp" />
But the original (stock) toggle still shows overtop the custom background
How do I get rid of the stock switch on top of my custom one?
Thanks in advance.
You should use android:thumb and android:track, not android:background
And you should write a selector for each one of them.

Set background checkbox android when dissable

to day i work with it and I get a problem.
When I set Checkbox like:
checkboxsend.setEnabled(false);
Here i want the background of checkbox not change to disable?
What can i do that..please help me! Thank
The my xml:
<CheckBox
android:id="#+id/checkboxsend"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:enabled="false" />
You will want to use a state drawable as the background for the Checkbox, something like this should get you closer to your goal:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:state_checked="true" android:drawable="#android:drawable/checkbox_on_background"/>
<item android:state_enabled="true" android:state_checked="true" android:drawable="#android:drawable/checkbox_on_background"/>
<item android:drawable="#android:drawable/checkbox_off_background"/>
</selector>
You can make modifications to this in order to match your exact logic.
Your CheckBox will look like this:
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:background="#drawable/custom_checkbox"
android:checked="true"/>
Try this...If you don't want to change the background Color of your check box than....
Instead:
checkboxsend.setEnabled(false);
Use:
checkboxsend.setClickable(false);

different text colour for Toggle button

Is there a way to have different text colours for a toggle button? If i use the android:textColor attribute, it will be applied to both states of the toggle button. Can we have one colour for On and another for Off?
Here's an example ToggleButton element :
<ToggleButton
android:id="#+id/btn"
android:layout_width="200dp"
android:layout_height="200dp"
android:textOn="On"
android:textOff="Off"
android:onClick="onToggleClicked"
android:layout_centerInParent="true"
android:textSize="100sp"
android:textColor="#color/yellow"
/>
create a directory called color under the res directory.
In there create an xml file like, toggle_text_selector.xml
This is what the file could look like
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#android:color/white" android:state_checked="true"/>
<item android:color="#android:color/black" android:state_checked="false"/>
</selector>
Then on your ToggleButton set android:textColor="#color/toggle_text_selector"
and you're done

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