Preference screen display text block
Asked Answered
P

5

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.

Purchase answered 9/8, 2012 at 13:22 Comment(0)
E
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>
Exponent answered 9/8, 2012 at 13:30 Comment(4)
Can you expand on this more? Do I have to call an intent to activity to display a text block?Purchase
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 pageExponent
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?Purchase
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.Exponent
A
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.

Aril answered 30/10, 2014 at 17:49 Comment(2)
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.)Airship
That turned out pretty easy; in code, find the preference and set the summary for it.Airship
C
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."/>
Cyclotron answered 22/8, 2013 at 13:21 Comment(1)
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.Cyclotron
E
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>
Exponent answered 9/8, 2012 at 13:30 Comment(4)
Can you expand on this more? Do I have to call an intent to activity to display a text block?Purchase
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 pageExponent
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?Purchase
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.Exponent
A
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>
Airship answered 12/4, 2017 at 14:8 Comment(0)
L
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)
Lubeck answered 1/4, 2020 at 17:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.