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
The secret is in android:checkMark make it null
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/checked_text_single_choice"
android:layout_width="match_parent"
android:layout_height="52dp"
android:checkMark="@null"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceSmall"
android:drawableLeft="?android:attr/listChoiceIndicatorSingle"
android:drawableRight="@null"
android:textColor="@android:color/black"
/>
An example of @WonderCsabo's suggestion to use CheckBox.
<CheckBox
android:id="@+id/create_account_checkedTextView_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show"
android:gravity="center"
android:checked="true"
# relative layout params
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_above="@+id/hint1" />
I don't think you can. Looking at the source code, it seems like the checkmark is drawn on the right all the time. And, to be honest, I would recommend you stick with that, for consistency -- Android gets knocked all the time because its apps have inconsistent UIs.
In the off chance that somebody is pointing a gun at your head, forcing you to change the placement of the checkmark, subclass CheckedTextView
as OMGPleaseDontShootCheckedTextView
, and override methods like onDraw()
, with tweaked versions of the original that changes the placement of the image and text.
Looking at the source, it's just based on layout direction. Setting it to RTL works well enough for me.
android:layoutDirection="rtl"
Source:
private boolean isCheckMarkAtStart() {
final int gravity = Gravity.getAbsoluteGravity(mCheckMarkGravity, getLayoutDirection());
final int hgrav = gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
return hgrav == Gravity.LEFT;
}
.
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
...
final boolean checkMarkAtStart = isCheckMarkAtStart();
final int width = getWidth();
final int top = y;
final int bottom = top + height;
final int left;
final int right;
if (checkMarkAtStart) {
left = mBasePadding;
right = left + mCheckMarkWidth;
} else {
right = width - mBasePadding;
left = right - mCheckMarkWidth;
}
...
}
}
}
Adding this line makes checkbox on left
android:drawableLeft="?android:attr/listChoiceIndicatorMultiple"