7

I am a beginner in Android Development. When I learned about CheckBox, it's a widget extending compoundbutton and CheckedTextView, widget extending TextView and implements Checkable Interface. When I searched on google, I found no results. Actually, what is the difference between them, If I am using ListView or RecyclerView with CheckBox ability. Which is the better option CheckBox or CheckedTextView?

Athira Reddy
  • 1,004
  • 14
  • 18

4 Answers4

7

To supplement the other answers, here is a visual comparison between CheckBox and CheckedTextView. This is running in an emulated Pixel 3 under Android R. Each of these screenshots shows three views inside a vertical LinearLayout.

If you don't specify the checkMark attribute, you get no checkbox on the CheckedTextView:

visual comparison with no checkMark attribute

with android:checkMark="?android:attr/listChoiceIndicatorSingle":

visual comparison with checkMark=listChoiceIndicatorSingle

with android:checkMark="?android:attr/listChoiceIndicatorMultiple":

visual comparison with checkMark=listChoiceIndicatorMultiple

There are other differences, but they're covered by other answers.

LarsH
  • 27,481
  • 8
  • 94
  • 152
2

As recommended from @LarsH I've turned my comment into an answer instead.

The differences is CheckedTextView doesn't have a checked/click event, see here why

Therefore if you want to have checked/click event for free (you can customize the checked/click listener by yourself) choose CheckBox and its alignment by default is (left aligned TextView & right aligned CheckBox)

You might want to align the TextView's text position (optional: if you want to align into different position, e.g: right aligned TextView & left aligned CheckBox)

Then use it on your RecyclerView holder

mochadwi
  • 1,190
  • 9
  • 32
  • 87
0

CheckBox: Is a specific type of 2 states button that can be either checked or unchecked.

CheckedTextView: Is a TextView with 2 states check and unchecked.

The text is the different between both views, so depend in what you want to do.

About the second question, RecyclerView was created as a ListView improvement.

extmkv
  • 1,991
  • 1
  • 18
  • 36
  • Sorry. I mean CheckBox or CheckedTextView with RecyclerView? – Athira Reddy Oct 20 '19 at 11:19
  • 2
    it is the `TextView` left aligned & `CheckBox` right aligned provided from Google you've got for free @AthiraReddy . Unfortunately `CheckedTextView` doesn't have checked/click event, see here: https://stackoverflow.com/a/2806669/3763032 so, I choose `CheckBox` but customize the TextView align/position (optional) when using `RecyclerView` – mochadwi Mar 05 '20 at 05:44
  • @mochadwi You're the only one who really answered the title question. If you turned your comment into an answer, I think it would be helpful to people who are wondering about this. – LarsH Sep 18 '20 at 13:49
0

so to set item in recyclerview first define the itemLayout as below

<CheckBox
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/dialog_checklist_checkbox"
  android:layout_width="match_parent"
   android:layout_height="wrap_content"
  android:text="text"
  android:padding="5dp"
  android:gravity="center"
 />

now in recyclerview you can use the checbox properties such as

holder.checkBox.setText("position"+position);

to set the text of respective checkbox.

you can also use other properties of checkbox such as

 holder.checkBox.setOnCheckedChangeListener();

and recyclerview is improved version of listview so try to use recyclerview.

Naman
  • 11
  • 5