Search code examples
androidpreferenceactivity

Android PreferenceActivity Item Height


I have a PreferenceActivity with two checkboxes. Everything is working correctly, but I have a UI problem; not all of the text for the checkbox fits onto the checkbox row.

Is there a nice way to set the minimum height for these checkbox preferences without hardcoding a value?

EDIT - adding xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
   <CheckBoxPreference
      android:key="alarm_set"
      android:title="@string/set_alarm_title"
      android:defaultValue="true" />
   <CheckBoxPreference
      android:key="store_pages"
      android:title="@string/store_downloaded_pages_title"
      android:summary="@string/store_downloaded_pages"
      android:defaultValue="false" />
</PreferenceScreen>

Solution

  • Based off this question about text preference having the same iussue I use this class.

    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(4); 
        }        
    } 
    

    In the xml it then looks like this.

        <com.iforpowell.android.ipbike.LongSummaryCheckBoxPreference android:key="@string/key_distance_line_enable"
            android:title="@string/title_distance_line_enable" android:summary="@string/summary_distance_line_enable"
            android:defaultValue="true" />
        <com.iforpowell.android.ipbike.LongSummaryCheckBoxPreference android:key="@string/key_altitude_line_enable"
            android:title="@string/title_altitude_line_enable" android:summary="@string/summary_altitude_line_enable"
            android:defaultValue="true" />
    

    I have exactly the same thing for EditTextPreference which has the same problem of a max of 2 lines for the summary in Api <= 7