android – Preference screen display text block-ThrowExceptions

Exception or error:

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.

How to solve:

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>

###

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.

###

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

###

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>

###

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)

Leave a Reply

Your email address will not be published. Required fields are marked *