Dor Shinar
Dor Shinar

Reputation: 1512

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.

Upvotes: 8

Views: 12247

Answers (3)

Rakshit Nawani
Rakshit Nawani

Reputation: 2604

try the following Xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="10"
    tools:context="com.example.pager.MainActivity" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="Anything you want here" />

    <CheckBox
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="8" />

</LinearLayout>

It works fine

Upvotes: 3

manzhda
manzhda

Reputation: 79

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

Upvotes: 2

dmestrovic
dmestrovic

Reputation: 722

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"

Upvotes: 10

Related Questions