Komanti
Komanti

Reputation: 21

Change from Layout 1 to Layout 2 on Android with button

I have two layouts that I want to change between each other when clicking a button. They are full screen views like this:

<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"
android:background="#FF0000"
tools:context=".FullscreenActivity" >

and I want them to change between them when clicking

 <Button
 android:id="@+id/green_button"
 style="?buttonBarButtonStyle"
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:text="@string/green" />

Am I supposed to change something in src/com.example.layout/fullscreenactivity.java onClick() or where can I change so this happens?

Big thanks in advance

Upvotes: 1

Views: 641

Answers (5)

Swayam
Swayam

Reputation: 16364

layout1 = (FrameLayout)findViewById(R.id.frameLayout1);
layout2 = (FrameLayout)findViewById(R.id.frameLayout2);
layout2.setVisibility(View.GONE);
mButton = (Button)findViewById(R.id.green_button);

mButton.setOnClickListener(new View.OnClickListener() {

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

    }
});

Upvotes: 0

android guy
android guy

Reputation: 108

One thing you can do is to declare both layouts in one xml file and then use setVisibility(View.GONE) to remove particular layout. You can see this in this stackoverflow question.

Upvotes: 0

geeky_chik
geeky_chik

Reputation: 28

Yes, in your activity where you utilize the framelayout and the button, you will have to add a listener for the button and the appropriate actions to happen when the button is clicked (in your case - change the layout)

Upvotes: 0

Kosh
Kosh

Reputation: 6334

rb = (Button) findViewById(R.id.green_button);
rb.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
            setContentView(R.layout.OtherLayout);
// now in order to use other layout button, image or any child function you have to declare it under the                 setContentView(R.layout.OtherLayout);

    }
});

please do accept the answer :)

Upvotes: 1

slezadav
slezadav

Reputation: 6141

Easiest way would be having both FrameLayouts applied, and in onClickListener only change visibility of one to visible and the other to gone

Upvotes: 0

Related Questions