I am trying to use R.layout.simple_list_item_multiple_choice with ListView. CheckedTextView is used in simple_list_item_multiple_choice.xml, but how can I make the checkbox
If you want the checkbox to be on the left, simply just use a CheckBox. Maybe it is not matter of course, but a CheckBox
can contain text. You can define that text by adding an XML attribute android:text, or by calling the setText() method. Actually CheckBox
is inherited from Button
, which inherits from TextView
, that's why it has all the text-related properties.
try this one and hope it will give you some ideas
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connection lost sound alert"
android:layout_weight="10"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:layout_gravity="right"
android:orientation="horizontal" >
<CheckBox android:id="@+id/checkbox_meat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:onClick="onCheckboxClicked"/>
</LinearLayout>
</LinearLayout>
You can assign your drawable to drawableLeft attribute, but it won't make you any good because it not support tinting. Instead I extended CheckedTextView like that:
public class LeftSideCheckedTextView extends CheckedTextView {
public LeftSideCheckedTextView(Context context) {
this(context, null, 0);
}
public LeftSideCheckedTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public LeftSideCheckedTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
Field mCheckMarkGravity = null;
try {
mCheckMarkGravity = this.getClass().getSuperclass().getDeclaredField("mCheckMarkGravity");
mCheckMarkGravity.setAccessible(true);
mCheckMarkGravity.set(this, Gravity.START);
} catch (Exception e) {
Logger.error(e);
}
}
}
Here I access the hidden field mCheckMarkGravity which is used inside CheckedTextView but have no access via style attributes, according to this bug ticket: https://code.google.com/p/android/issues/detail?id=174517
There can be a trick with Checkable Layout like in this post - https://chris.banes.me/2013/03/22/checkable-views/ . Just use some custom layout like this as ListView item layout:
<com.example.custom_view.CheckableLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckedTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:duplicateParentState="true"
.../>
<TextView
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
.../>
</com.example.custom_view.CheckableLinearLayout>
or
android:checkMark=?android:attr/listChoiceIndicatorMultiple"
Create your own row template and set android:drawableLeft on the CheckedTextView.
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:drawableLeft="?android:attr/listChoiceIndicatorMultiple"
/>
or
android:drawableLeft="?android:attr/listChoiceIndicatorSingle"
If anyone is still looking for a solution without making a Checkbox in the layout. Try the solution here.
Android ListView ArrayAdapter - checkbox/radio button arrangement