4

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?

double-beep
  • 5,031
  • 17
  • 33
  • 41
kkudi
  • 1,625
  • 4
  • 25
  • 47

2 Answers2

2

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.

inazaruk
  • 74,247
  • 24
  • 188
  • 156
  • the second approach suits me better I think. The only problem is that MaxLines doesn't work. setTextSize works though but is less of a neat solution. Isn't there any way you can tell the text view to wrap its contents and increase automatically rather than having a static height? – kkudi Jun 03 '11 at 11:20
  • `setMaxLines(N)` is supposed to do exactly this. It will increase `TextView` size up to N lines. There layout were `TextView` is located might restrict the height also. Let me check. – inazaruk Jun 03 '11 at 11:22
  • Ok, if you look at http://codesearch.google.com/codesearch/p?hl=en#uX1GffpyOZk/core/res/res/layout/simple_list_item_multiple_choice.xml&q=layout.simple_list_item_multiple_choice&sa=N&cd=1&ct=rc, you'll notice the android:layout_height is set to specific height (using current theme value). This can be overriden in your code. I've update answer to show how to set the height of `TextView`. – inazaruk Jun 03 '11 at 11:29
  • I get a ClassCastException on ViewGroup.LayoutParams. – kkudi Jun 03 '11 at 11:31
  • I've updated the code. I can't run and check the code right now, so it might not work correctly :) But you should get the idea of what I'm trying to achieve. – inazaruk Jun 03 '11 at 11:37
0

You don't need to implement a custom Adapter. Just define a custom layout for the list items and pass it to the adapter instead of android.R.layout.simple_list_item_multiple_choice.

You can search the Android sources (come with the SDK on you computer) to how the original simple_list_item_multiple_choice layout looks like and adapt it to your needs.

Flo
  • 27,355
  • 15
  • 87
  • 125