Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between checkbox and checkedtextview in Android? Which to use for RecyclerView?

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?

like image 699
Athira Reddy Avatar asked Oct 20 '19 10:10

Athira Reddy


2 Answers

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.

like image 94
LarsH Avatar answered Oct 29 '22 17:10

LarsH


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

like image 37
mochadwi Avatar answered Oct 29 '22 16:10

mochadwi