Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text on right of CheckBox in Android

I'm trying to imitate the CheckBoxs like in the Settings app on my phone. It looks like that: CheckBox in my Galaxy S4 5.0.1 Settings app

I've tried using separate TextViews, but that way only the checkmark is clickable, rather than the text and the checkmark.
Is there a way to accomplish that?
I also tried a CheckedTextView but couldn't find the right drawable to use.

like image 733
Dor Shinar Avatar asked Dec 24 '22 19:12

Dor Shinar


2 Answers

As far as I know this should work

<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
    android:text="Text"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:button="@null"
    android:drawableRight="?android:attr/listChoiceIndicatorMultiple"/>

With android:button="@null" you remove standard checkbox button / image, and after you just add checkbox image as right drawable (or use drawableEnd to support RTL)

android:drawableRight="?android:attr/listChoiceIndicatorMultiple"

like image 59
dmestrovic Avatar answered Jan 03 '23 02:01

dmestrovic


Is there a way to accomplish that? I also tried a CheckedTextView but couldn't find the right drawable to use.

CheckedTextView will solve your issue perfectly:

Please look for example on simple_list_item_checked.xml (it is android embedded layout)

CheckedTextView have next attribute:

<!-- Drawable used for the check mark graphic. -->
<attr name="checkMark" format="reference"/>

So you only need to set checkMark to proper selector (If you do not have resource you could use btn_check.xml. It is inside android):

<CheckedTextView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:checkMark="@drawable/your_selector_with_proper_checked_states"
/>
like image 43
manzhda Avatar answered Jan 03 '23 01:01

manzhda