android: CheckedTextView cannot be checked?
Asked Answered
G

7

52

Initially I wanted a checkmark where the text is placed on the left of the checkmark. After searching on this site I found out the best workaround is android:CheckedTextView? However, I found out that the checkmark cannot be changed manually by users. Is it by design?

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/autoupdatecheckboxview" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:gravity="center_vertical" 
 android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
 android:paddingLeft="6dip" 
 android:paddingRight="6dip" 
 android:text="Pop up a message when new data available" 
 android:typeface="sans" android:textSize="16dip"/> 
Germayne answered 13/4, 2010 at 5:41 Comment(1)
Then CheckedTextView supposed to be named as ListCheckedTextView? goddamn googlePiton
T
38

You probably want to just use a regular CheckBox (which inherits from Button and thus TextView). CheckedTextView is designed to work with list views. Example CheckBox layout XML is below:

<CheckBox
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Pop up a message when new data available"
    android:textSize="16dip" />
Transcription answered 15/4, 2010 at 5:41 Comment(2)
What if I don't want to add another view?Exotoxin
This doesn't really answer the question. Consider that people are aware that a CheckBox is more suitable, but still would prefer using CheckedTextView. They would be interested in knowing how to make it work, not that they shouldn't.Aubrey
E
132

It is possible, and somewhat simple to implement what you are looking for. Yes, CheckedTextView is used primarily for having a single Checkable view in the row of a ListView, which controls its children's checkable states using choiceMode. However, since CheckBox does not appear to support a right-aligned checkbox on its own, and CheckedTextView is a right-aligned checkbox, it makes sense to want to use what's there.

Because ListView controls the checked state of a list item, the CheckedTextView itself does not respond to click events, and is not clickable or focusable by default. It does respond to pressed and focused states, however -- that means it can receive focus and click events, and looks correct as far as a checkbox should look. The only thing missing is that it does not toggle its checked state on click. Therefore, a quick OnClickListener that calls .toggle() will give you the end result you're looking for.

In summary, you need 3 things: clickable, focusable, and onClickListener:

    CheckedTextView chkBox = (CheckedTextView) findViewById(R.id.CheckedTextView01);
    chkBox.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)
        {
            ((CheckedTextView) v).toggle();
        }
    });

and layout file:

<CheckedTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/CheckedTextView01"
    android:checked="true"
    android:clickable="true"
    android:focusable="true"
    android:text="Label on Left Side of Checkbox."
    />
Eurythermal answered 10/5, 2010 at 21:44 Comment(4)
Thanks for the explanation: it wasn't obvious, why this kind of controls might be non-clickable by default.Cb
Great answer. To make the CheckedTextView behave even more similar to a list item you should set the background to <item name="android:background">?android:attr/listChoiceBackgroundIndicator</item>.Caterina
+1 for this as well #12642029Unfortunate
In Kotlin I had to use "with (view as CheckedTextView) {isChecked = !isChecked}" or using "if (checkbos.isChecked)... failed" because isChecked has the wrong value.Involutional
T
38

You probably want to just use a regular CheckBox (which inherits from Button and thus TextView). CheckedTextView is designed to work with list views. Example CheckBox layout XML is below:

<CheckBox
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Pop up a message when new data available"
    android:textSize="16dip" />
Transcription answered 15/4, 2010 at 5:41 Comment(2)
What if I don't want to add another view?Exotoxin
This doesn't really answer the question. Consider that people are aware that a CheckBox is more suitable, but still would prefer using CheckedTextView. They would be interested in knowing how to make it work, not that they shouldn't.Aubrey
T
9

You can use and toggle CheckedTextView by the following way:

In layout:

<CheckedTextView
        android:id="@+id/cv_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Some text here" 
        android:textSize="18sp"
        android:gravity="center_vertical"
        android:clickable="true"
        android:checkMark="@drawable/btn_check_off"
        android:focusable="true"
        android:checked="false"
        android:onClick="toggle"/>

In your activity:

public void toggle(View v)
{
    CheckedTextView cView = (CheckedTextView) v.findViewById(R.id.cv_file_name);
        if (cView.isSelected())
        {
            cView.setSelected(false);
            cView.setCheckMarkDrawable (R.drawable.btn_check_off);
        }
        else
        {
            cView.setSelected(true);
            cView.setCheckMarkDrawable (R.drawable.btn_check_on);
        }
}

And don't forget to put drawables. I get it from SDK ...\android-sdk-windows\platforms\android-10\data\res\drawable-mdpi\

Termination answered 1/2, 2012 at 9:37 Comment(0)
E
3

simple answer is to add your own onClickListener, and use : isChecked() method instead of isSelected().

CheckedTextView chkBox = (CheckedTextView) findViewById(R.id.CheckedTextView01);
chkBox.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v)
    {
        if(((CheckedTextView) v).isChecked()){
            ((CheckedTextView) v).setChecked(false);
        }else{
            ((CheckedTextView) v).setChecked(true);            
        }
    }
});

and get the status using view.isChecked() method.

Extender answered 8/5, 2015 at 9:16 Comment(0)
W
2

This layout looks and behaves the same way as CheckedTextView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/dropdownListPreferredItemHeight"
    android:gravity="center_vertical" >

    <TextView
        android:id="@android:id/text1"
        style="?android:attr/spinnerDropDownItemStyle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ellipsize="marquee"
        android:singleLine="true" />

    <CheckBox
        android:id="@android:id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:duplicateParentState="true"
        android:focusable="false" />

</LinearLayout>

Only extra legwork is to set an OnClickListener on the root view and call checkBox.toggle() on the CheckBox.

Wayfarer answered 3/7, 2013 at 20:16 Comment(0)
G
1

Here is my use in SingleChoiceDialog

1.select_dialog_singlechoice.xml

<?xml version="1.0" encoding="UTF-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="@style/PopupSelectList"
    android:checkMark="@drawable/radio"
    android:ellipsize="marquee"
    android:gravity="center_vertical"
    android:paddingLeft="12.0dip"
    android:paddingRight="10.0dip" />

2.style.xml

<style name="PopupSelectList">
    <item name="android:textSize">16.0sp</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:background">@drawable/list_item_selector</item>
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:minHeight">@dimen/dialog_select_height</item>
</style>

3.radio.xml

<?xml version="1.0" encoding="UTF-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/dot_selected" android:state_checked="true" android:state_window_focused="false"/>
    <item android:drawable="@drawable/dot_normal" android:state_checked="false" android:state_window_focused="false"/>
    <item android:drawable="@drawable/dot_normal" android:state_checked="false"/>
    <item android:drawable="@drawable/dot_selected" android:state_checked="true"/>
</selector>

4.In Adapter's getView

CheckedTextView title = (CheckedTextView) convertView
                .findViewById(android.R.id.text1);
        title.setText(mItems[position]);
        title.setSelected(mCheckedItem == position ? true : false);
                    title.setCheckMarkDrawable(position == mCheckedItem ?                   R.drawable.dot_selected
                : R.drawable.dot_normal);
Glauce answered 26/10, 2013 at 3:55 Comment(2)
where is variable mCheckedItem its no in my ArrayAdapter.Highminded
You can put your CheckedTextView at anywhere you want.Glauce
R
0

If you want more fine-grained control over the label and the checkbox, another alternative is to use RelativeLayout and the android:layout_alignParentRight attribute:

<RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/my_checkbox_label" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="my checkbox label" />
    <CheckBox
        android:id="@+id/my_checkbox" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" />
</RelativeLayout>

you can then adjust the margin/etc of the textview and checkbox to suit your needs.

Reminisce answered 26/9, 2011 at 13:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.