Center/Align TextView with text embedded in CheckedTextView

111 Views Asked by At

The following layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

    <TextView
        android:text="HEADER"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textAlignment="center"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:textSize="18sp"/>

    <CheckedTextView
        android:text="SYSTEM"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:gravity="center"
        android:checkMark="?android:attr/textCheckMark"
        android:paddingStart="?android:attr/listPreferredItemPaddingStart"
        android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
        android:checked="true" />
</LinearLayout>

produces this:

enter image description here

But preference is to have the HEADER text centered with the SYSTEM text. Obviously the fact that the checkbox is embedded throws the alignment of the text off. I've tried experimenting using the CheckedTextView for the HEADER text as well, but we don't want the checkbox to appear in the HEADER and have not been able to figure out how to make it disappear. Any ideas on how to center align HEADER and SYSTEM while still using the CheckedTextView?

1

There are 1 best solutions below

1
Yunus Kulyyev On

Try this way:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_gravity="center"
android:orientation="vertical">

    <TextView
        android:text="HEADER"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textAlignment="center"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:textSize="18sp"/>

    <CheckedTextView
        android:text="SYSTEM"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:gravity="center"
        android:checkMark="?android:attr/textCheckMark"
        android:paddingStart="?android:attr/listPreferredItemPaddingStart"
        android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
        android:checked="true" />
</LinearLayout>