Android checkMarkGravity attribute of CheckedTextView (api 21 )

1k views Asked by At

The select_dialog_singlechoice_material.xml layout from the plattform source uses the android:checkMarkGravity="start"attribute:

<?xml version="1.0" encoding="utf-8"?>
<!--
 Copyright (C) 2014 The Android Open Source Project
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
      http://www.apache.org/licenses/LICENSE-2.0
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="?android:attr/textColorAlertDialogListItem"
android:gravity="center_vertical"
android:paddingStart="@dimen/alert_dialog_padding_material"
android:paddingEnd="@dimen/alert_dialog_padding_material"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:checkMarkGravity="start"
android:ellipsize="marquee" />

However I am unable to use this attribute in my project as I get the following error:

Error:(15) No resource identifier found for attribute 'checkMarkGravity' in package 'android'

Is it possible to use this attribute in some way? I am compiling for api 21, so this should be okay:

minSdkVersion 21
targetSdkVersion 21
3

There are 3 answers

0
Mou On

I dont know if this attribute is correct but doesn´t exists in official docs: http://developer.android.com/intl/es/reference/android/widget/CheckedTextView.html

Anyway, the value appear in this same proyect in this file (line 2052): https://github.com/android/platform_frameworks_base/blob/59701b9ba5c453e327bc0e6873a9f6ff87a10391/core/res/res/values/symbols.xml

0
android developer On

This is correct. The code actually uses it, but the compiler doesn't allow to use it. The code has it in multiple places, and here's where it gets initialized:

public CheckedTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    ....
    mCheckMarkGravity = a.getInt(R.styleable.CheckedTextView_checkMarkGravity, Gravity.END);

And, an example of usage in Android's layout XML files, is in "preference_material.xml" :

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="?android:attr/textColorAlertDialogListItem"
    android:gravity="center_vertical"
    android:paddingStart="?attr/dialogPreferredPadding"
    android:paddingEnd="?attr/dialogPreferredPadding"
    android:checkMark="?android:attr/listChoiceIndicatorSingle"
    android:checkMarkGravity="start"
    android:ellipsize="marquee" />

I've written about this issue here, hoping Google will release it for everyone to be used.

0
androidguy On

The checkMarkGravity has defaulted to END in the CheckedTextView for some time. As of now, the CheckedTextView the XML files for the single and multiple choice do not set checkmark attributes. here is what I see in the former, for example:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeightSmall"
android:textAppearance="?attr/textAppearanceMedium"
android:textColor="?attr/textColorAlertDialogListItem"
android:gravity="center_vertical"
android:paddingStart="@dimen/select_dialog_padding_start_material"
android:paddingEnd="?attr/dialogPreferredPadding"
android:drawableStart="?attr/listChoiceIndicatorSingle"
android:drawablePadding="@dimen/select_dialog_drawable_padding_start_material"
android:ellipsize="marquee" />

So the reason that the checkmark shows up on the left, is that the checkmark drawable is null, and in fact it is setting drawableStart instead. If you were to use your own XML for the list row, you could set the checkmark.

I only found this problem with the multiple choice. In my case, the radio button (single choice) do show up at the end, but the checkbox mode showed up at start. Here's how you can fix this by setting the dialog multi choice item layout:

<style name="AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="alertDialogStyle">@style/AlertDialogStyle</item>
</style>
<style name="AlertDialogStyle" parent="MaterialAlertDialog.MaterialComponents">
    <item name="multiChoiceItemLayout">@layout/common_dialog_multichoice_row</item>
</style>

And this is that layout file:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="?attr/textColorAlertDialogListItem"
android:gravity="center_vertical"
android:paddingStart="@dimen/abc_select_dialog_padding_start_material"
android:paddingEnd="?attr/dialogPreferredPadding"
android:checkMark="@drawable/checkmark_selector"
android:drawablePadding="20dp"
android:ellipsize="marquee" />

where checkmark_selector needs to be a stateful drawable. I used a custom, but you could possibly also use the theme's ?attr/listChoiceIndicatorSingle like the original file.