Preference with multi-line title on android

1.3k views Asked by At

I'm using Android SharedPreferences API to build a settings screen.

I've one setting which I need a long text to explain the user its meaning (I would like to have something like main title and smaller subtitle,but i think it would require to much customization) the settings.xml is like:

<EditTextPreference

    android:defaultValue="15"
    android:inputType="number"

    android:icon="@drawable/ic_timer"
    android:key="@string/pref_comment_interval"
    android:persistent="true"
    android:lines="2"
    android:title="@string/time_between_comments" />

but even setting lines=2 and breaking the line with \n at time_between_comments the text is getting wrapped.

<string name="time_between_comments">Time between comments (in seconds)\nLower is faster</string>

like:
enter image description here

how can i make the text to break the line?

2

There are 2 answers

0
GianhTran On

By default, the title of EditTextPreference is singleLine="true" So we should custom it as below

public class MultiLineTitleEditTextPreference extends EditTextPreference {
    public MultiLineTitleEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public MultiLineTitleEditTextPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MultiLineTitleEditTextPreference(Context context) {
        super(context);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);

        TextView textView = (TextView) view.findViewById(your_package.R.id.title);
        if (textView != null) {
            textView.setSingleLine(false);
        }
    }
}
0
dangVarmit On

This doesn't work for all Preference types, but it works for EditTextPreference.

Add <![CDATA[ \n]]> around the first line of your title like in this example:

    <string name="pref_title_special_with_note"><![CDATA[Special Title\n]]><small><i> ** followed by second line note</i></small></string>

This will display the second line in smaller text size and italic, but that's just decoration.