David Kachlon
David Kachlon

Reputation: 201

Android creating a settings page

can someone show me how to make a switch and a label look this way? https://www.androidhive.info/wp-content/uploads/2017/06/android-settings-preferences.png (the top right one, Vibrate) where the label is to the left and the switch is to th right --- here's waht i have so far

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

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TableRow>
        <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" >
            <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Invert Colors"></TextView>
        <Switch
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"/>

        </LinearLayout>
    </TableRow>

</TableLayout>


</android.support.constraint.ConstraintLayout>

but it looks like this

not what i want

Upvotes: 0

Views: 46

Answers (1)

nhoxbypass
nhoxbypass

Reputation: 10152

You can change the layout to RelativeLayout or ConstraintLayout to align this switch to the parent right.

Or use the built-in PreferenceScreen which is designed & recommended for settings activity. Ex:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:key="setting_title_notifications"
        android:title="Notifications">

    <CheckBoxPreference
        android:id="@+id/setting_vibrate"
        android:key="setting_vibrate"
        android:title="Vibrate"
        android:summary="Vibrate on new notification" />
    </PreferenceCategory>
</PreferenceScreen>

Upvotes: 1

Related Questions