Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i change the size of a CheckBox Title or make it Wrap in a PreferenceScreen xml?

I'm writing a preference screen in in xml for my Android application. My issue is that some of the titles of the preferences are too long and will not wrap the page. Consider my example:

 <CheckBoxPreference
                android:title="Language Detection (BETA)"
                android:defaultValue="false"
                android:summary="Automatically decide which language to use"
                android:key="lan_det_pref"
                android:textSize="15px"
                android:lines="4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                />

Maybe I'm missing something but I have tried using number of other properties with no positive results. I have tried android:singleLine, android:lines, etc., and none of these seem to effect the title of the preference. Also is there a way to scale down the size of a CheckBox title?

Any help would be greatly appreciated.

Thanks :)

like image 217
MarcusB416 Avatar asked Nov 24 '10 14:11

MarcusB416


2 Answers

A CheckBoxPreference is not derived from View, so the usual view attributes don't apply.

Instead, a CheckBoxPreference is bound to a view, which has a predefined layout.

You can derive a class from CheckBoxPreference and override onBindView. In your class' onBindView, find the CheckBox view and adjust its view attributes to your liking.

class MyCBPref extends CheckBoxPreference{
  public MyCBPref( Context context, AttributeSet attrs){
    super(context, attrs);
  }
  protected void onBindView( View view){
    super.onBindView(view);
    makeMultiline(view); 
  }
  protected void makeMultiline( View view)
  {
    if ( view instanceof ViewGroup){

        ViewGroup grp=(ViewGroup)view;

        for ( int index = 0; index < grp.getChildCount(); index++)
        {
            makeMultiline(grp.getChildAt(index));
        }
    } else if (view instanceof TextView){
        TextView t = (TextView)view;
        t.setSingleLine(false); 
        t.setEllipsize(null);
    }
  }
}

Then in your layout, reference your class instead of CheckBoxPreference:

<com.mycorp.packagename.MyCBPref android:title="..." ... />
like image 111
Thorstenvv Avatar answered Oct 05 '22 23:10

Thorstenvv


You could customize you checkbox like this: MyPreference.xml

<CheckBoxPreference android:key="testcheckbox" 
    android:title="Checkbox Title" 
    android:summary="Checkbox Summary..." 
    android:layout="@layout/custom_checkbox_preference_layout">
</CheckBoxPreference>

and custom_checkbox_preference_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingLeft="16dip"
    android:paddingRight="?android:attr/scrollbarSize">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="horizontal">
        <ImageView
            android:id="@+android:id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="false"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />

        <TextView android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignLeft="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary"
            android:maxLines="4" />

    </RelativeLayout>

    <!-- Preference should place its actual preference widget here. -->
    <LinearLayout android:id="@+android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" />

</LinearLayout>

the point is android:singleLine="false"".

like image 23
user1482130 Avatar answered Oct 06 '22 00:10

user1482130