Dynamic layout with invisibility, cannot restore element position

Asked

Viewed 659 times

0

My point is, simple fact is I don’t know much about this yet.

I’m having a problem with invisibility, I have a vertical Linearlayout with two buttons and when I click on the first appears another Linearlayout that I set and the second button goes down (all right so far)but when I click the first button again for the textviews to disappear the second button does not return to the starting position, which I have to use to make it 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);
                        }
                    }
                }
        );
    }
}

1 answer

0


Turns out you’re using View.INVISIBLE, that will just leave your LinearLayout invisible in fact, without "removing" it from its layout, occupying the same space.

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

if (lol.getVisibility() == View.VISIBLE) {
    lol.setVisibility(View.GONE);
} else {
    lol.setVisibility(View.VISIBLE);
}
  • That’s right, thank you very much for your help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.