title: Customizing the layout of a PreferenceScreen
body: <p><strong>My requirements</strong></p>
  
  <p><em>Note: I need to support Android API 15 and onwards.</em></p>
  
  <p>In my PreferenceFragment I am dynamically adding PreferenceScreen's to a PreferenceCategory.  </p>
  
  <pre><code>PreferenceScreen as = mgr.createPreferenceScreen(context);
  as.setTitle(name); 
  myCategory.addPreference(as);
  </code></pre>
  
  <p>When the settings Activity renders these PreferenceScreens are rendered with just a title in it's row, see the image below.
  <img src="https://i.stack.imgur.com/inPnd.png" alt="PreferenceScreen shown in a settings activity"></p>
  
  <p>I want to customize what is shown in this row. Ideally I would like to have a title with a summary below, an icon to the left of the title/summary and on certain rows an icon on the right hand side. See the mockup image below.</p>
  
  <p><img src="https://i.stack.imgur.com/FdGvf.png" alt="Desired rendering of my Preference Screen"></p>
  
  <p><strong><a href="http://developer.android.com/reference/android/preference/Preference.html#setWidgetLayoutResource(int)" rel="noreferrer">PreferenceScreen.setWidgetLayoutResource(int widgetLayoutResId)</a></strong></p>
  
  <p>I know I can use "setWidgetLayoutResource" to add an icon to the right of the row layout (and a summary with "setSummary") using the following code in my settings activity</p>
  
  <pre><code>PreferenceScreen as = mgr.createPreferenceScreen(context);
  as.setTitle(name); 
  as.setSummary(summary);
  as.setWidgetLayoutResource(R.layout.as_widget);
  myCategory.addPreference(as);
  </code></pre>
  
  <p>where "as_widget.xml" is</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;ImageView
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/ic_action_favorite"/&gt;
  </code></pre>
  
  <p>this will produce UI like the following
  <img src="https://i.stack.imgur.com/K5TaL.png" alt="Layout using setWidgetLayoutResource"></p>
  
  <p>This gets me closer to what I would like but still not exactly what I would like (missing the icon at the beginning of the row).</p>
  
  <p><strong><a href="http://developer.android.com/reference/android/preference/Preference.html#setLayoutResource(int)" rel="noreferrer">PreferenceScreen.setLayoutResource(int layoutResId)</a></strong></p>
  
  <p>I believe if I use this, then I can control the rendering of the whole row. I have seen examples of this on stackoverflow, such as </p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/8143838/setting-preference-layout-and-changing-the-attribute-in-it">Setting preference layout and changing the attribute in it</a></li>
  <li><a href="https://stackoverflow.com/questions/6194116/creating-a-custom-layout-for-preferences">Creating a custom layout for preferences</a></li>
  </ul>
  
  <p>From my understanding the layout file you specify has to have the following</p>
  
  <ul>
  <li>its root View with the id "@android:id/widget_frame"</li>
  <li>a view with the id android:id="@+android:id/title" (this is where the
  string specified in PreferenceScreen.setTitle is placed)</li>
  <li>a view with the id android:id="@+android:id/summary" (this is where
  the string specified in PreferenceScreen.setSummary is placed)</li>
  </ul>
  
  <p>However when I try and do this, Android Studio highlights "@+android:id/summary" with the error "Can not resolve symbol '@+android:id/summary'. When the application runs my rows are rendered completely blank.</p>
  
  <p>My java is</p>
  
  <pre><code>PreferenceScreen as = mgr.createPreferenceScreen(context);
  as.setTitle(name); 
  as.setSummary(summary);
  as.setLayoutResource(R.layout.as_row_layout);
  myCategory.addPreference(as);
  </code></pre>
  
  <p>And my layout is</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@android:id/widget_frame"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:minHeight="?android:attr/listPreferredItemHeight"
      android:gravity="center_vertical"
      android:paddingRight="?android:attr/scrollbarSize"&gt;
  
      &lt;ImageView
          android:id="@+id/icon1"
          android:src="@drawable/ic_action_email"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center" /&gt;
  
      &lt;RelativeLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginLeft="15dip"
          android:layout_marginRight="6dip"
          android:layout_marginTop="6dip"
          android:layout_marginBottom="6dip"
          android:layout_weight="1"&gt;
  
          &lt;TextView android:id="@+android:id/title"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:singleLine="true"
              android:textAppearance="?android:attr/textAppearanceLarge"
              android:ellipsize="marquee"
              android:fadingEdge="horizontal" /&gt;
  
          &lt;TextView android:id="@+android:id/summary"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_below="@android:id/title"
              android:layout_alignLeft="@android:id/title"
              android:textAppearance="?android:attr/textAppearanceSmall"
              android:textColor="?android:attr/textColorSecondary"
              android:maxLines="4" /&gt;
  
      &lt;/RelativeLayout&gt;
  
      &lt;ImageView
          android:id="@+id/icon2"
          android:src="@drawable/ic_action_favorite"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>Extending PreferenceScreen</strong></p>
  
  <p>I looked at extended PreferenceScreen to overwrite how it renders, but it appears this class has now been made final so all examples on the internet that do it that way can not be used.</p>