20

I am trying to make a Preference screen that just has an about, contact, and legal option, all of which when clicked just show text blurb and icon in a separate page, no shared preferences or anything.

I am having trouble understanding the hierarchy in order to display the text. I would like the flow to be: settings -> about -> the about text

Currently I have this, which gives me the category and option, but I don't know what to make it in order to display new text.

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
            android:title="Info">
        <Preference android:title="About"/>


    </PreferenceCategory>
...
</PreferenceScreen>

I don't know what option to use to make the about clickable into a textview.

Jonik
  • 80,077
  • 70
  • 264
  • 372
Nick
  • 9,285
  • 33
  • 104
  • 147

5 Answers5

45

A.Grandt's solution gives really nice imitation of text block within Preference Activity, but I would add an android:persistent="false" attribute, which avoids storing unnecessarily this 'pseudo preference' into SharedPreferences file for your app's settings.

To sum up. Imitation of TextView within Preferences Activity as A.Grandt suggested would be:

<Preference
    android:key="pref_static_field_key"
    android:selectable="false"
    android:persistent="false"
    android:title="you can omit the title"
    android:summary="Multi line description\ncan go here."/>

Although you can use \n for line breaking, yet it won't resize the Preference Item itself, so there is a possibility that these multilines will not be fully visible within a still single-line Preference Item.

Krzysiek
  • 7,895
  • 6
  • 37
  • 38
  • Using this approach, is there any way to set the text block value (summary) *in code*? (Use case: show app version name at the top of the preference screen.) – Jonik Apr 12 '17 at 13:12
  • [That turned out pretty easy](http://stackoverflow.com/a/43371960/56285); in code, find the preference and set the summary for it. – Jonik Apr 12 '17 at 14:09
25

I had the same problem, and needed to show a static text block. Though in my case it was in lined into the preference page.

The tag does allow for linking though that doesn't solve the need for static text. However, the Preference tag itself does. You can have it show text, the usual title line, as well as the text summary underneath, both are optional, and then make it un-selectable. It'll give the illusion of a static text.

<Preference
    android:key="pref_static_field_key"
    android:selectable="false"
    android:title="you can omit the title"
    android:summary="Multi line description\ncan go here."/>
A.Grandt
  • 2,242
  • 2
  • 20
  • 21
  • Note: The Preference tag also have an attribute called android:fragment, that seems to be linking to a preference fragment. That, combined with the above seems to be what you need for your About box. – A.Grandt Aug 22 '13 at 13:24
13

You cannot add a formatted textblock inside a PreferenceScreen, that's not what it's meant to be. However, you can add your About text inside another activity (a LinearLayout with some formatted TextViews may be enough). Call this by passing an intent inside the preference:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >    
    <Preference 
        android:key="about"   
        android:title="About">           
            <intent android:action="your.package.path.to.Activity"/>           
    </Preference>
</PreferenceScreen>
Sergi Juanola
  • 6,531
  • 8
  • 56
  • 93
  • Can you expand on this more? Do I have to call an intent to activity to display a text block? – Nick Aug 09 '12 at 13:33
  • well, a text block on its own is not meant to be in a preference page. Unless you add a summary to that about (which will just show unformatted text), your only option is to open a new activity or just defining the about page as another thing, rather than a preference page – Sergi Juanola Aug 09 '12 at 13:34
  • Okay, I see what you mean. Can I make it so my preference when clicked will take it to another preference in which I use Title and summary to display the text with a single intent? Basically make another XML preferences file that will be displayed when "About" from the primary preference screen is clicked? – Nick Aug 09 '12 at 13:40
  • 1
    No no, each preference can hold a title and a summary. I was talking about adding your about inside that summary. Anyway, shouldn't it be better to write down that About page inside a, say, more classical layout? A `LinearLayout`, for instance. Because a `PreferenceScreen` is good for showing a list of different topics or a list of links at most (which, in my opinion, would fit better in a `ListView`). For the sake of customisation, I'd go for another thing rather than a `PreferenceScreen` holding your about text. – Sergi Juanola Aug 09 '12 at 13:45
9

I wanted to add a text block, but set the actual text content programmatically. My specific need: show app version name at the top of the preference screen.

I got it done following the approach by A.Grandt and Krzysiek, and setting the summary in code.

dev_preferences.xml:

<Preference
    android:key="pref_app_version" 
    android:persistent="false"
    android:selectable="false"
    android:title="@string/app_name" />

The preference fragment (extends PreferenceFragment):

  private static final String KEY_APP_VERSION = "pref_app_version";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.dev_preferences);

        findPreference(KEY_APP_VERSION).setSummary(BuildConfig.VERSION_NAME);
    }

I also ended up using a custom layout (as in this answer) by setting android:layout="@layout/dev_preferences_header" in the <Preference> entry. (Not mandatory, but this way you can easily customise the looks of the preference "header".)

dev_preferences_header.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"       
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:padding="@dimen/spacing_small">

    <TextView
        android:id="@android:id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@android:id/summary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@android:id/title" />

</RelativeLayout>
Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372
1

In my Settings page I want to show last backup date-time of my application data.

(Inspired by: https://developer.android.com/reference/android/content/SharedPreferences.Editor)

1) In my preference.xml:

<PreferenceCategory app:title="Backup last execution">
    <EditTextPreference
        app:key="backup"
        app:selectable="false"
        app:summary="never"
        app:useSimpleSummaryProvider="true" />
</PreferenceCategory>

2) In my class Utility { companion object }:

fun Utility.Companion.storeToPreferences(key:String, value:String){
       val sharedPref: SharedPreferences =
           PreferenceManager.getDefaultSharedPreferences(context)
       val editor = sharedPref.edit()
       editor.putString(key, value)
       editor.apply()
}

3) My call:

 Utility.storeToPreferences("backup", LAST_BACKUP)
Caesar
  • 470
  • 5
  • 9