views:

182

answers:

1

I am trying to make an AlertDialog that scales in size to the content. The textual content can be larger than the dialog, so it has to be able to scroll. If I use a RelativeLayout everything renders properly no matter how much info there is, but when there is not enough text to fill up the TextView it leaves a lot of extra space. This is the code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="6dip"
    android:paddingLeft="12dip"
    android:paddingRight="12dip"
    android:paddingBottom="2dip"
>
<CheckBox 
    android:id="@+id/saleListingCheckbox" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="Save sale"
  android:textColor="#fff"
  android:layout_alignParentBottom="true"
    >
</CheckBox>
<ScrollView 
    android:id="@+id/saleListingLinear"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_above="@id/saleListingCheckbox"
    android:layout_alignParentTop="true"
    >
    <TextView
        android:id="@+id/saleListingTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:textColor="#fff"
       android:textSize="7pt"
       android:paddingBottom="4dip"
    />
</ScrollView>
</RelativeLayout>

java code:

@Override
protected boolean onTap(int index) {
    if (overlays.isEmpty()) {
        return false;
    }
    final SaleOverlayPushPin saleItem = overlays.get(index);

    AlertDialog alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    LayoutInflater inflater = context.getLayoutInflater();
    dialogView = inflater.inflate(R.layout.sale_listing, null);

    textView = (TextView) dialogView.findViewById(R.id.saleListingTextView);

    checkbox = (CheckBox) dialogView.findViewById(R.id.saleListingCheckbox);
    checkbox.setOnCheckedChangeListener(this);

    alertDialog.setView(dialogView);

    alertDialog.setTitle(saleItem.getTitle());
    textView.setText(saleItem.getSnippet());
    checkbox.setTag(saleItem);
    checkbox.setChecked(saleItem.isSelected());

    alertDialog.show();

    return true;
}

And here is what it looks like with little data and a lot of data:

alt textalt text

I have been able to make it work using a LinearLayout, but then I have a different problem where if the text content is larger than the dialog it cuts off the checkbox. Here is the code and the screenshots:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="6dip"
    android:paddingLeft="12dip"
    android:paddingRight="12dip"
    android:paddingBottom="2dip"
    android:orientation="vertical"
>
<ScrollView 
    android:id="@+id/saleListingLinear"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
  android:layout_weight="1"
  android:layout_gravity="top"
    >
    <TextView
        android:id="@+id/saleListingTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:textColor="#fff"
       android:textSize="7pt"
       android:paddingBottom="4dip"
    />
</ScrollView>
<CheckBox 
    android:id="@+id/saleListingCheckbox" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="Save sale"
  android:textColor="#fff"
  android:layout_weight="1"
  android:layout_gravity="bottom"
    >
</CheckBox>
</LinearLayout>

alt textalt text

I would prefer that the "little data" behavior works like the LinearLayout and the "lot of data" work like the RelativeLayout. Is this possible?

UPDATE: bart's solution for the LinearLayout works perfectly. Removing the layout_weight from the CheckBox was the key.

However the RelativeLayout still does not work as expected. It makes the CheckBox unviewable if the data in the TextView is large enough. I would still love to know the solution for the RelativeLayout if at all possible. See below:

alt text

+1  A: 

Working layout for RelativeLayout (in general you should not use alignParentTop and alignParentBottom simultaneously if you do not want additional space):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="6dip"
    android:paddingLeft="12dip"
    android:paddingRight="12dip"
    android:paddingBottom="2dip"
>

<ScrollView 
    android:id="@+id/saleListingLinear"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"

    android:layout_alignParentTop="true"
    >
    <TextView
        android:id="@+id/saleListingTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:textColor="#fff"
       android:textSize="7pt"
       android:paddingBottom="4dip"
    />
</ScrollView>

<CheckBox 
    android:id="@+id/saleListingCheckbox" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="Save sale"
    android:textColor="#fff"
    android:layout_below="@id/saleListingLinear"
    >
</CheckBox>
</RelativeLayout>

Working layout for LinearLayout (remove weight from Checkbox):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="6dip"
    android:paddingLeft="12dip"
    android:paddingRight="12dip"
    android:paddingBottom="2dip"
    android:orientation="vertical"
>
<ScrollView 
    android:id="@+id/saleListingLinear"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
  android:layout_weight="1"
  android:layout_gravity="top"
    >
    <TextView
        android:id="@+id/saleListingTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:textColor="#fff"
       android:textSize="7pt"
       android:paddingBottom="4dip"
    />
</ScrollView>
<CheckBox 
    android:id="@+id/saleListingCheckbox" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="Save sale"
  android:textColor="#fff"
  android:layout_gravity="bottom"
    >
</CheckBox>
</LinearLayout>
bart
Thanks but the RelativeLayout you suggest makes the CheckBox unviewable if the TextView has enough data to make a scrollbar. The LinearLayout works perfectly though. Thanks!
Nemi