Search code examples
androidresizewindow

how to shrink an android window to fit content


I have a popup window containing a simple TextView. If I update the message with a longer one then the window automatically expands to fit the text, but if the message is shorter, it remains the same size. How can I retrigger a "resize" on the window?

updateText method:

public void updateText(String message) {
    TextView text = (TextView)view.findViewWithTag("text");
    text.setText(message);
    text.setGravity(Gravity.CENTER_HORIZONTAL);
    popupWindow.showAtLocation(view, Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 100);
}

window definition:

<?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="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/shape1"
    >
  <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="1dp"
      android:orientation="vertical" >
    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:orientation="vertical" >
      <TextView
      android:tag="text"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Now is the time for all good men to come to the aid of the party" />
    </LinearLayout>
  </LinearLayout>
</LinearLayout>

Thank you


Solution

  • Set width for all LinearLayouts and TextView to wrap_content.