1

I am currently working on Android project where I need to use multiple layouts in the same activity.

When the user clicks a button in the main layout, then I need to have another layout(which is completely different) with different content.

I tried looking up on the Internet and found that Fragments can be used, but as far as I understand Fragments are to be used when one needs only partial changes in the new layout, whereas I need to use a a completely different layout.

Also, I found include, but that is to use the same layout in multiple activities. So, not what I was looking for.

Does anyone have any idea as to how to accomplish this?

Naman Sancheti
  • 414
  • 1
  • 8
  • 18

3 Answers3

4

XML use framelayout. Put your content where I have mentioned <--Your layout-->

<FrameLayout 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" >



        <LinearLayout
            android:id="@+id/Layout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:orientation="vertical" >

                            <--Your layout-->

                    <Button
                        android:id="@+id/Button1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:text="Button" />
                </LinearLayout>



    <LinearLayout
        android:id="@+id/Layout2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <--Your layout-->
    </LinearLayout>

</FrameLayout>

Code : Set onclick for button and set visibility.

LinearLayout 1ayout1,layout2;
Button button1;

1ayout1 = (LinearLayout) findViewById(R.id.Layout1);
1ayout2 = (LinearLayout) findViewById(R.id.Layout2);
button1=(Button)findViewById(R.id.Button1);
button1.setOnClickListener(this);

1ayout1.setVisibility(LinearLayout.VISIBLE);
1ayout2.setVisibility(LinearLayout.GONE);

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
            1ayout2.setVisibility(LinearLayout.VISIBLE);
            1ayout1.setVisibility(LinearLayout.GONE);
    }
}
Dhinakaran Thennarasu
  • 3,336
  • 4
  • 26
  • 39
0

try to use fragments
you can show many fragments in one activity

Community
  • 1
  • 1
Sepehr Behroozi
  • 1,780
  • 1
  • 17
  • 32
0

I don't think so fragments can be used only for partial changes , Hope it works well with dynamic views as well

http://developer.android.com/guide/components/fragments.html
Sainath Patwary karnate
  • 3,165
  • 1
  • 16
  • 18