androidtextviewlistactivityword-wrapandroid-wrap-content

How to wrap text using simple_list_item_multiple_choice?


I'm using a SimpleCursorAdapter to display a single CheckedTextView. I know this is done best using simple_list_item_multiple_choice and android.R.id.text1.

adapter = new SimpleCursorAdapter(getApplicationContext(), 
              android.R.layout.simple_list_item_multiple_choice, rules, 
              new String[]{Constants.KEY_RULE}, new int[]{android.R.id.text1});

If the text from KEY_RULE is more than two lines, android doesn't wrap, instead it hides it. Is there an easy work-around for this without having to implement my own adapter?

Find my xml code below:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <TextView android:id="@+id/header" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/rule_selection_for_revision_list_title"
        android:padding="4dip" android:background="#000000" android:textColor="#ffffff" android:textStyle="bold" />

  <ListView
    android:id="@android:id/list"
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_weight="1.0" />

    <TextView android:textSize="18sp" android:textStyle="bold"
        android:id="@id/android:empty" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:gravity="center_vertical|center_horizontal"
        android:text="There are no rules to consider." />

</LinearLayout>

Is there any way I can at least reduce the font size so that it fits to two lines?


Solution

  • There are several options.

    You can define your custom layout for item in the list. This allows you to fully customize your item UI. More details can be found in List View tutorial.

    The other approach can be used if you still want to use standard item layout but only fix one small thing in it. You can extend Adapter and modify the view in getView function. Here is an example:

    class MySimpleCursorAdapter extends SimpleCursorAdapter
    {
        public MySimpleCursorAdapter(Context ctx, int layout, 
                                     Cursor cursor, String [] from, int [] to)
        {
            super(ctx, layout, cursor, from, to);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View view = super.getView(position, convertView, parent);
    
            TextView textView = (TextView)view.findViewById(android.R.id.text1);
    
            //You can increase number of lines in the text view
            textView.setMaxLines(5);
    
            LayoutParams params = textView.getLayoutParams();
            if(params.height > 0)
            {
                int height = params.height;
                params.height = LayoutParams.WRAP_CONTENT;                              
                textView.setLayoutParams(params);
                textView.setMinHeight(height);
            }
    
            //Or you can make your font smaller
            textView.setTextSize(customTextSize);
    
            //or whatever you like, even apply new custom style
            //...
    
            return view;
        }   
    
    }
    

    You should probably choose the first approach (custom layout), but sometimes the second approach is feasible too.