2

I need help with styling my custom preference according to the app style(sheet).

I've created a custom preference that shows two settings side-by-side (first name and last name). All is fine, I can edit them using a custom dialog etc, but I am having trouble styling it to look like the other list preferences.

Image below shows what I am trying to do.

View list

I'd like to make the top preference (my custom one) look like the bottom one (straight from settings.xml). As you can see the padding, text color, size etc are all difference; this is possibly because "my" preference uses Linear Layouts + Text Views and not a standard preference list view item.

My custom preference XML came from this answer Android: Creating custom preference

This is my app theme

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

and this is the code for my preference:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:weightSum="2"
              android:id="@android:id/widget_frame"
              android:orientation="horizontal">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:layout_weight="1"
                  android:id="@android:id/widget_frame"
                  android:orientation="vertical">
        <TextView
                android:id="@+id/firstname"
                style="@android:style/TextAppearance.DeviceDefault.SearchResult.Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/setting_firstname" />

        <TextView
                android:id="@+id/firstname_data"
                style="@android:style/TextAppearance.DeviceDefault.SearchResult.Subtitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="moo" />
    </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:layout_weight="1"
                  android:id="@android:id/widget_frame"
                  android:orientation="vertical">
        <TextView
                android:id="@+id/lastname"
                style="@android:style/TextAppearance.DeviceDefault.SearchResult.Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/setting_lastname" />
        <TextView
                android:id="@+id/lastname_data"
                style="@android:style/TextAppearance.DeviceDefault.SearchResult.Subtitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Moo" />
    </LinearLayout>
</LinearLayout>

I have two questions basically:

  1. What is / where can I find the XML that creates a preference list item - is it is standard list view item or its own custom XML?
  2. How do I make my preference look like a standard one?

I tried to assign android:id="@android:id/widget_frame" to the linear layout (as suggested in the Stackoverflow answer above)

Also tried various style= on the Text views (style="@android:style/TextAppearance.DeviceDefault.SearchResult.Subtitle") but I don't know if this is correct and what other options are available

Other info

My fullname preference is just standard code :

public class FullnamePreference extends DialogPreference {

My settings.xml contains this:

    <com.example.Settings.FullnamePreference
            android:key="fullname"/>
    <Preference
            android:key="version"
            android:summary="@string/setting_version_summary"
            android:title="@string/setting_version" />
Community
  • 1
  • 1
mogoman
  • 2,286
  • 24
  • 28
  • Well, for one thing, your text is dark because you are basing your app theme on a light theme (light theme, hence dark text). Try basing your theme on `Theme.Appcompat` (a dark theme to match preferences) for starters. – kris larson Mar 14 '16 at 01:29
  • If you really want a light theme for your app with just the dark theme for preferences, you can add a dark theme based on `Theme.AppCompat` to your styles, then assign it to the layout using the attribute `app:theme="R.style.AppThemeDark"`on your layout. – kris larson Mar 14 '16 at 01:35
  • I'll try that, thanks. But even if I change the theme, the text color will change, not the size, layout and spacing which is my real problem – mogoman Mar 14 '16 at 07:46

0 Answers0