Search code examples
androidpreference

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!


Solution

  • 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);
          }
      }
    }