Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android PreferenceScreen title in two lines

I have PreferenceScreen with long title especially in some languages. I'm able to set multiple lines title for CheckBoxPreference or ListPreference with this: Android preference summary . How to set 3 lines in summary? , but how to set 2-lines title for PreferenceScreen? I can change style like here: How can I change font size in PreferenceScreen but this doesn't look perfect and it's inconsistent with preference style (font, size ...).

thanks!

like image 960
Miroslav Michalec Avatar asked Feb 09 '12 22:02

Miroslav Michalec


3 Answers

If you want just for a single preference, using setSingleLineTitle or app:singleLineTitle="false" in the XML file.

If you wish to apply it on all preferences, you can do it in multiple ways, when extending PreferenceFragmentCompat :

1.going over all preferences and setting it:

override fun setPreferenceScreen(preferenceScreen: PreferenceScreen?) {
    if (preferenceScreen != null)
        setAllPreferencesToHaveMultiLineTitles(preferenceScreen)
    super.setPreferenceScreen(preferenceScreen)
}

private fun setAllPreferencesToHaveMultiLineTitles(preference: Preference) {
    preference.isSingleLineTitle = false
    if (preference is PreferenceGroup)
        for (i in 0 until preference.preferenceCount)
            setAllPreferencesToHaveMultiLineTitles(preference.getPreference(i))
}

2.Doing the same in the adapter, but not recommended as it might not work some day (reaches hidden API of the library) :

override fun onCreateAdapter(preferenceScreen: PreferenceScreen?): RecyclerView.Adapter<*> {
    return object : PreferenceGroupAdapter(preferenceScreen) {
        @SuppressLint("RestrictedApi")
        override fun getItem(position: Int): Preference {
            val item = super.getItem(position)
            item.isSingleLineTitle = false
            return item
        }
    }
}

3.Similar, but without using isSingleLineTitle (but again not recommended for the same reason) :

override fun onCreateAdapter(preferenceScreen: PreferenceScreen?): RecyclerView.Adapter<*> {
    return object : PreferenceGroupAdapter(preferenceScreen) {
        @SuppressLint("RestrictedApi")
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PreferenceViewHolder {
            val holder = super.onCreateViewHolder(parent, viewType)
            setPreferenceTitleTextViewToHaveMultipleLines(holder.itemView)
            return holder
        }
    }
}

fun setPreferenceTitleTextViewToHaveMultipleLines(v: View) {
    if (v is TextView && v.getId() == android.R.id.title)
        return v.setSingleLine(false)
    if (v is ViewGroup)
        for (i in 0 until v.childCount)
            setPreferenceTitleTextViewToHaveMultipleLines(v.getChildAt(i))
}
like image 112
android developer Avatar answered Nov 09 '22 08:11

android developer


Use the method setSingleLineTitle(false)

This was added in API 26, so you should be able to use the support library version for older devices

like image 36
Ramprakash K Avatar answered Nov 09 '22 07:11

Ramprakash K


By default the title is set to single line. You need to extend Preference and get title textview and set single line to false. Use this class instead of the regular PreferenceScreen:

public class TwoLinePreference extends Preference {

  public TwoLinePreference(Context ctx, AttributeSet attrs, int defStyle) {
    super(ctx, attrs, defStyle);
  }

  public TwoLinePreference(Context ctx, AttributeSet attrs) {
    super(ctx, attrs);
  }

  public TwoLinePreference(Context ctx) {
    super(ctx);
  }

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

    TextView textView = (TextView) view.findViewById(android.R.id.title);
    if (textView != null) {
        textView.setSingleLine(false);
      }
  }
}
like image 20
Codes12 Avatar answered Nov 09 '22 08:11

Codes12