Android preference summary . How to set 3 lines in summary?
Asked Answered
C

2

14

Summary of preference is allowed only 2 lines . If I want to display 3 lines or more in summary . How can I do ?

Cumulonimbus answered 18/7, 2011 at 6:51 Comment(1)
Hi you are using TextView or other function to display contentDecuple
B
28

You can create you Preference class by extending any existing preference:

public class LongSummaryCheckboxPreference extends CheckboxPreference
{
    public LongSummaryCheckboxPreference(Context ctx, AttributeSet attrs, int defStyle)
    {
        super(ctx, attrs, defStyle);        
    }

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

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

        TextView summary= (TextView)view.findViewById(android.R.id.summary);
        summary.setMaxLines(3);
    }       
}

And then in preferences.xml:

 <com.your.package.name.LongSummaryCheckBoxPreference 
    android:key="@string/key"
    android:title="@string/title"
    android:summary="@string/summary" 
    ... />

The drawback is that you need to subclass all preference types you need 3 lines summary for.

Burnside answered 18/7, 2011 at 7:3 Comment(2)
Work great and upvote!!. If you use androidx.preference, you can use onBindViewHolder instead of onBindView.Piperine
I also had to add this: summary.setSingleLine(false);Paulinepauling
H
9

Using androidx.preference.PreferenceCategory I got something like that:

Java:

public class LongSummaryPreferenceCategory extends PreferenceCategory {

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

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

    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);
        TextView summary= (TextView)holder.findViewById(android.R.id.summary);
        if (summary != null) {
            // Enable multiple line support
            summary.setSingleLine(false);
            summary.setMaxLines(10); // Just need to be high enough I guess
        }
    }
    
}

Kotlin:

class LongSummaryPreferenceCategory @JvmOverloads constructor(
  context: Context, 
  attrs: AttributeSet? = null
): PreferenceCategory(context, attrs) {

  override fun onBindViewHolder(holder: PreferenceViewHolder) {
    super.onBindViewHolder(holder)
    val summary = holder.findViewById(android.R.id.summary) as? TextView
    summary?.let {
      // Enable multiple line support
      summary.isSingleLine = false
      summary.maxLines = 10 // Just need to be high enough I guess
    }
  }
}
Holster answered 9/3, 2020 at 10:37 Comment(1)
Working fine, I've converted it to Kotlin and added the Kotlin version :)Preeminence

© 2022 - 2024 — McMap. All rights reserved.