How to display a button centered in Android preferences screen?

18 views Asked by At

I am adding a delete button to my preferences screen. I am trying to center the button in my preferences screen, but it is not working.

My preferences XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="@string/account_settings_key">

    <PreferenceCategory android:title="@string/profile_category_title">
        <Preference android:title="@string/profile_title"
            android:selectable="false"
            android:key="@string/profile_key"/>

        <Preference android:title="@string/profile_signout_title"
            android:key="@string/profile_signout_key"/>
    </PreferenceCategory>

    <!-- Title set programmatically to appName -->
    <PreferenceCategory
        android:key="@string/premium_key">
        <Preference android:title="@string/subscribe"
            android:selectable="false"
                    android:key="@string/subscribe_key"/>
        <Preference android:title="@string/manage_subscription"
                    android:key="@string/manage_subscription_key"/>
    </PreferenceCategory>

    <Preference
        android:key="@string/delete_account_key"
        android:layout="@layout/delete_button_layout"
        /></androidx.preference.PreferenceScreen>`

This is my delete button layout

`<Button android:id="@+id/delete_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:textColor="@color/error_red"
    android:textSize="14sp"
    style="@style/TextButton"
    android:text=@string/delete_account

When I view this in Android Studio editor, it shows the delete button centered correctly. But in the screen, it was to the left of the window. I tried android:widgetLayout which pushes to the right. Is there any other way to center the button?

Editor Image enter image description here

Actual image from device enter image description here

0

There are 0 answers