Creating a custom layout for preferences
Asked Answered
P

2

47

I'm trying to create a custom layout for managing preferences. I know there is the standard and recommended layout provided by PreferenceActivity, but if I want to add, say, a Button, how can I get that?

I plan to design the application so that when the user touches a certain item on the main activity screen, it goes to a 'submenu' of options that the user can enable, which subsequently appears on the next screen (it has to be in this linear progression). The user must set these each time he uses the application. Currently, I'm considering using the standard PreferenceActivity to represent this submenu of options.

Maybe this isn't the right approach?

Edit: I overlooked the solution while Googling. How to add a button to PreferenceScreen

Polythene answered 31/5, 2011 at 22:5 Comment(0)
R
86

You can always create a custom preference layout item and use it in the PreferenceActivity. For example, I did this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingRight="?android:attr/scrollbarSize">

    <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">

        <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" />

        <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:maxLines="2" />

        <ImageView android:id="@+id/ImageView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/go"
            android:layout_alignParentRight="true" />

    </RelativeLayout>

    <!-- Preference should place its actual preference widget here. -->
    <LinearLayout android:id="@android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:orientation="vertical" />

</LinearLayout>

It has some waste, but basically creates 2 lines (heading, subtitle) with an image on the right. You could replace the ImageView with a Button and you'd be all set. You then set the layout for the individual preference in your xml/prefs.xml file like so:

<Preference
    android:title="@string/label_pref_version"
    android:key="@string/pref_version"
    android:layout="@layout/pref" />

After that just fire findViewById in your Activity code and attach a listener to the button. Might have to do some more work if you have multiple buttons in the Activity, but shouldn't be unreasonable.

You can find the source code here : https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/layout/preference.xml

Rotl answered 31/5, 2011 at 22:13 Comment(8)
Thanks! But I found a more elegant solution to my problem that I initially overlooked. #2697733Polythene
This solution here seems to be more flexible actually - compared to the one that has you use listView (mentioned in first comment on this answer). With this solution you can place your custom view elements in line and mixed in with standard preference widgets - that other solution allows custom layouts only before or after the Preference layout.Herakleion
what's the point of adding custom layout to <preference...>. Isn't it exactly same as taking that custom layout and inflating it into any fragment or activity ? Correct me if I am wrong.Peggi
findViewById returns null if called from Activity. In a fragment there is no such method. How do i access my additional view in my custom preference layout?Bacteriostat
Have you found a way to use findViewById()?Defrock
You should be able to do this: have you tried getActivity().findViewById()? Probably needs to be done from the right lifecycle method (i.e. probably in an override of onActivityCreated()).Rotl
Has anyone able to access this layout in PreferenceFragment? @KlemensCharade
I currently have this same issue.Friary
P
13

I plan to design the application so that when the user touches a certain item on the main activity screen, it goes to a 'submenu' of options that the user can enable, which subsequently appears on the next screen (it has to be in this linear progression).

This sounds like a nested PreferenceScreen.

Prana answered 31/5, 2011 at 22:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.