1

I want to have a custom SwitchPreference layout and pass the "title" parameter to it, similar to this:

<SwitchPreference
            android:key="@string/key"
            app:passedTitle="@{@string/title}"
            android:layout="@layout/custom_layout"
            android:defaultValue=5 />

that way I can reuse the same layout for the different SwitchPreferences but change the title shown.

I know I can bind data normally as follows:

<include
        layout="@layout/custom_layout"
        app:passedTitle="@{@string/title}"

in conjunction with a custom layout like this:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools" >

    <data>
    <variable
        name="passedTitle"
        type="String"/>
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            style="@style/SettingsSubHeader"
            android:text="@{passedTitle}" />

        <!-- Must have this id or else won't save value! -->
        <Switch
            android:id="@android:id/checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"/>

    </RelativeLayout>

</layout>

However, I can't get the passedTitle value to pass to the custom view if I just set them using the andriod:layout param in SwitchPreference (since I can't use <include>).

Do you have any idea how I can pass a value to my custom layout when using a preference?

Elliptica
  • 3,928
  • 3
  • 37
  • 68
  • Just to confirm, you want to use a custom layout with a SwithPreference? If yes, why not to use a custom Preference? – Ferran Apr 30 '19 at 15:28
  • I could, but I don't need to override any of the behavior of the preference, just the layout. Preferences support that inherently by allowing a person to declare `android:layout="@layout/custom_layout"` in them. I'm just not sure how to use the data-binding ability for layouts with it. That's what I'm trying to discover. – Elliptica May 01 '19 at 04:19
  • @Elliptica did you manage to find out ? – Ninja420 Sep 29 '19 at 11:14
  • No, I ended up just having to use a custom layout for each thing and having to define the variable in each individual layout. – Elliptica Oct 03 '19 at 16:00

0 Answers0