Search code examples
androidandroid-preferencesswitchpreference

Android id for the custom layout for SwitchPreferenceCompat


I have been trying to create custom layouts to style fonts for Preference(s) in a PreferenceFragment. For simple preferences with only title and a summary, I have been able to do so by mentioning a layout resources with android id(s) like:

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

    <TextView
        android:id="@android:id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="@color/primaryTextColour"/>

    <TextView
        android:id="@android:id/summary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textColor="@color/secondaryTextColour"/>
</LinearLayout>

This worked completely fine for preferences like EditTextPreference or a ListPreference. Now, I want to do the same for a SwitchPreferenceCompat. What I am unable to find is theandroid:id for the switch/toggle in SwitchPreferenceCompat. If I use the above mentioned layout, it works, but I do not see the switch any more. The layout that I have written for the purpose is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="100"
    android:minHeight="70dp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:weightSum="80"
        android:orientation="vertical">

        <TextView
            android:id="@android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textColor="@color/primaryTextColour"/>

        <TextView
            android:id="@android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:textColor="@color/secondaryTextColour"/>

    </LinearLayout>

    <android.support.v7.widget.SwitchCompat
        android:id="@android:id/??"
        android:layout_width="wrap_content"
        android:weightSum="20"
        android:layout_height="wrap_content" />

</LinearLayout>

What is the android:idthat I should switch for SwitchCompat here?


Solution

  • Try this id

    android:id="@+id/switchWidget"