Dynamic layout with invisibility, I can not reset element position

0

My question is simple, but I still do not understand much of it.

I have a problem with invisibility, I have a vertical LinearLayout with two buttons and when I click on the first one appears another LinearLayout that I set and the second button goes down (all right up here), however when I click on the first button again for the textviews to disappear the second button does not return to the starting position, what do I have to use for this to be possible?

XML

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/linearLayout">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button" />
        <LinearLayout
            android:visibility="gone"
            android:id="@+id/lol"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="LOLOLOLLOLOL"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="LOLOLOLLOLOL"/>
        </LinearLayout>
    </LinearLayout>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LOLatuabelha"
        android:id="@+id/belha"
        android:layout_below="@+id/linearLayout"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

JAVA

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button botao;
        botao = (Button) findViewById(R.id.button);

        final LinearLayout lol = (LinearLayout) findViewById(R.id.lol);

        botao.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (lol.getVisibility() == View.VISIBLE){
                            lol.setVisibility(View.INVISIBLE);
                        }
                        else{
                            lol.setVisibility(View.VISIBLE);
                        }
                    }
                }
        );
    }
}
    
asked by anonymous 12.03.2015 / 13:19

1 answer

0

It turns out that you're using View.INVISIBLE , which will only actually make your LinearLayout invisible, without "taking it" out of your layout, occupying the same space.

To work the way you want, just change this property to View.GONE , as you used it yourself in your XML :

if (lol.getVisibility() == View.VISIBLE) {
    lol.setVisibility(View.GONE);
} else {
    lol.setVisibility(View.VISIBLE);
}
    
12.03.2015 / 13:34