1

Settings page

Where do I get this style of Preference Screen? I'm specifically interested in creating the round rectangle shape around each list of preferences belonging to one preference category. I'm not sure if this is possible using the android:layout tag with a Preference?

1 Answers1

0

You can create a custom_background.xml in your Drawable directory as follows:

Right-click on Drawable directory -> New -> Drawable Resource File -> enter file name(here it is custom_background) and in root element select/enter "shape".

Now in your custom_background.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#2196f3" />
    <corners android:radius="10dp" />
</shape>

You can also have attributes like padding, stroke etc in custom_background.xml .

Now for each Preference, you can have a separate ConstraintLayout (or any other layout which you are using) under your main layout and then give custom_background.xml in the background attribute of that Preference layout as follows:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:padding="4dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/your_preference_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:background="@drawable/custom_background">
        
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Give padding in main_layout according to your desired look.
Let me know if this solves your query.

Levi
  • 187
  • 2
  • 17
  • Hi thanks this is a very thorough answer. The ConstraintLayout is confusing me though as it's not available in the `PreferenceScreen`. –  Jun 20 '21 at 18:16
  • It doesn't matter what layout you are using. You just have to give your custom_background.xml in the background attribute of your layout – Levi Jun 20 '21 at 18:44
  • I'm using a preference xml. I pasted it in this link: https://codeshare.io/Jbrm0n It's basically just generic preferences. –  Jun 20 '21 at 18:46
  • okay now I get it, you can refer this link https://stackoverflow.com/questions/53834600/custom-preference-android-kotlin – Levi Jun 20 '21 at 18:56
  • Hey. So I'm aware of this but I was wondering if there was a way to add the rounded rectangle background without having to create a whole new preference from scratch (since then I'd also have to manually add the values the default SharedPreferences file) –  Jun 20 '21 at 19:11