user235925
user235925

Reputation: 838

Android Show/Hide Widget Resize Fill Parent

I have an EditText with a Button next to it. The button is hidden at first so the EditText takes up the full screen, which is good. When they tap the EditText I have the button appear next to the EditText, and it resizes itself accordingly. However, when I hide the Button (I set visibility to gone), the EditText does NOT resize to full screen (leaving a gap to the right of the EditText). Any tips?

I have tried putting the EditText and Button in both a LinearLayout and a TableLayout (with stretchable column, etc) and I see the same behavior. I also tried doing some runtime calls to removeView/addView stuff and that didnt work. I also tried calling invalidate() on both the EditText and its parent.

Thanks!

Upvotes: 1

Views: 2234

Answers (1)

Song
Song

Reputation: 157

I encountered the same question, I want to change the size of an EditText when hiding and showing a Button which stands next to it, but the EditText's size will not shrink after call setVisibility(View.VISIBLE) on the button. I solved it by adding a android:layout_weight="1" to the EditText.

The layout xml looks like this:

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal">
    <EditText 
        android:id="@+id/search_text" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1"
        />
    <Button 
        android:id="@+id/hide_btn" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn"
        />
</LinearLayout>

Upvotes: 1

Related Questions