Search code examples
androidtextviewandroid-preferencespreferenceactivitylongtext

How to handle long text in preferences on Android?


Background

I'm making an app that has some settings, and I want to use the built in PreferenceActivity or PreferenceFragment for the job

The problem

Some of the preferences have a long title which I cannot shorten, plus I think that if I ever localize the app (translate to multiple languages) I would face the same problem (for example in German, which has quite long words, sometimes).

What you get in this situation is just the beginning of the text and then "..." (or less dots, which doesn't make much sense btw) in the end of it.

Example:

enter image description here

What I've tried

I know that the PreferenceActivity extends from ListActivity, so I can change its adapter to whatever I wish, but that would remove the way it works.

I also know that I can extend from each of the types of the preferences classes, use the "onCreateView" method to have a reference to the created view and then access its children, but this is weird, no? I mean, it's almost like assuming that it will never change the way it looks.

EDIT: Here's a sample code of what I've tried:

Extend from each of the preferences classes, and in each of them , use:

...
@Override
protected View onCreateView(final ViewGroup parent)
  {
  final View view=super.onCreateView(parent);
  ViewUtil.handlePreferenceTitleTextView(view);
  return view;
  }
...

//ViewUtil.java :

private void handlePreferenceTitleTextView(final View v)
  {
  final TextView titleTextView=(TextView)v.findViewById(android.R.id.title);
  if(titleTextView!=null)
    titleTextView.setSingleLine(false);
  }

It works, but I don't think it's recommended as Google might change the way preferences views work.

The question

How to handle long text in preferences' titles on Android ?

Is it possible to make it have an ellipsize / marquee (so that it will have an animation to show everything) ? Or maybe auto fit the font size? Or set it to have word wrap ? Or a horizontal scrollView that will allow the user to scroll to read the rest of the text?

Is there maybe a convention of how to handle such cases? Maybe long clicking to show a toast/dialog for seeing the whole text?


Solution

  • 2021: When using androidx.preference (likely what everyone has)

    You can simply use app:singleLineTitle="false"

    <Preference
            app:key="your_key"
            app:singleLineTitle="false"
            app:title="LONG TITLEEEEEEE EEEEEEEEE EEEEEEEEEEEEEEE" />
    

    Works for Switches etc too