title: Custom Preference Android Kotlin
body: <p>I'd like to subclass <code>Preference</code> to create a custom preference item in Kotlin.  I am unable to get the custom preference to inflate in the Preference screen.  If I remove this custom preference from my preference screen, the rest of the preferences I have implemented (not shown here) are working fine.  <strong>There are many similar <em>seeming</em> questions here, but none of the ones I've found directly deal with the issue of creating a Kotlin implementation of a custom preference.</strong></p>
  
  <p>Please help me with a working example <strong>that you've tested</strong> that shows three things: </p>
  
  <ol>
  <li><code>custom_preference.xml</code></li>
  <li><code>CustomPreference.kt</code></li>
  <li><code>preference_screen.xml</code> (Parent Preference screen to display custom preference)</li>
  </ol>
  
  <p>Here is my code:
  A custom <code>xml</code> preference item that displays a string (let's keep it simple for the example, although my preference will end up having significantly more functionality)</p>
  
  <p><strong>custom_preference.xml</strong></p>
  
  <pre><code>&lt;Preference 
      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="@android:id/widget_frame"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".CustomPreference"&gt;
      &lt;TextView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="This is a custom preference" /&gt;
  &lt;/Preference&gt;
  </code></pre>
  
  <p>A class that extends <code>Preference</code> and includes the proper constructors.</p>
  
  <p><strong>CustomPreference.kt</strong>  </p>
  
  <pre><code>package com.example.myApp
  
  import android.content.Context
  import android.support.v7.preference.Preference
  import android.support.v7.preference.PreferenceViewHolder
  import android.util.AttributeSet
  import com.example.myApp.R
  import com.example.myApp.R.layout.custom_preference
  
  class CustomPreference (context: Context,
                              attrs: AttributeSet? = null,
                              defStyleAttr: Int = R.attr.preferenceStyle,
                              defStyleRes: Int = defStyleAttr)
      : Preference(context, attrs, defStyleAttr, defStyleRes) {
      override fun onBindViewHolder(holder: PreferenceViewHolder?) {
          super.onBindViewHolder(holder)
          layoutResource = custom_preference
      }
  }
  </code></pre>
  
  <p>The custom preference declaration in the <code>PreferenceScreen</code>.  </p>
  
  <p><strong>preference_screen.xml</strong>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"&gt;
      &lt;com.example.CustomPreference
          app:key="custom_preference_key"
          app:title="This is a custom preference" /&gt;
  &lt;/android.support.v7.preference.PreferenceScreen&gt;
  </code></pre>
  
  <p>Note: I manually renamed class names here for the example.  Also, I <strong>must</strong> use the support libs instead of Androidx for this project.  </p>