shobo yo
shobo yo

Reputation: 43

How to hide and show multiple layouts?

I have 3 relative layouts with visibility set to GONE and 3 buttons. I want that when I press one button to show me a a specific layout and when I press another button to show me a different layout and hide the previous layout.

This is my activity xml:

<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">


<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="12dp"

<RelativeLayout
    android:id="@+id/rel"
    android:visibility="gone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/text1"
        android:text="Text 1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>
<Button
    android:id="@+id/btn2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="12dp"
<RelativeLayout
    android:id="@+id/rel2"
    android:visibility="gone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/text2"
        android:text="Text 2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

<Button
    android:id="@+id/btn3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="12dp"
<RelativeLayout
    android:id="@+id/rel3"
    android:visibility="gone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/text3"
        android:text="Text 3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</RelativeLayout>

Upvotes: 0

Views: 1652

Answers (1)

Aakash
Aakash

Reputation: 5261

I have formatted code according to you, try this:

RelativeLayout layout1,layout2,layout3;
        Button button1,button2,button3;

        @Override
            protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                layout1=(RelativeLayout) findViewById(R.id.rel);
                layout2=(RelativeLayout) findViewById(R.id.rel2);
                layout3=(RelativeLayout) findViewById(R.id.rel3);
                button1=(Button) findViewById(R.id.btn);
                button2=(Button) findViewById(R.id.btn2);
                button3=(Button) findViewById(R.id.btn3);
            button1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                layout1.setVisibility(View.VISIBLE);
                layout2.setVisibility(View.GONE);
                layout3.setVisibility(View.GONE);
              }
            });
    button2.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                layout1.setVisibility(View.GONE);
                layout2.setVisibility(View.VISIBLE);
                layout3.setVisibility(View.GONE);
              }
            });
    button3.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                layout1.setVisibility(View.GONE);
                layout2.setVisibility(View.GONE);
                layout3.setVisibility(View.VISIBLE);
              }
            });
        }

Upvotes: 1

Related Questions