0
votes

I have two layouts as shown in the below image. In the second layout I have two buttons. When user clicks on the button1 layout 2 should occupy whole screen and layout1 should be invisible.When button 2 is pressed again it should show the normal view of both layouts.

Initial views :

initial page view

When button 1 is pressed :

when button 1 is pressed

When button2 is pressed original view should be displayed again.

enter image description here

2
have you head of fragments? - phpdroid
show your code. - Ankita
First of all unclear question. Why are you using 2 layouts? From the images, it seems that you just want to remove the layout 1 when button 1 is clicked. So on the click of button 1, use layout1.setVisibility(View.GONE); on the click of button 2 use layout1.setVisibility(View.VISIBLE); - wj07

2 Answers

1
votes

You can use Fragments to accomplish this (or not). Use a FrameLayout in order to contain the layout to be "kicked out" when you press Button 1.

To do that, simply obtain the reference to the FrameLayout (give it an id and then reference it in the onCreate() method), and set in the Button1 onClickListener() setVisibility(View.GONE); for the FrameLayout.

That will get rid of the view.

When you press on Button2, re-instate the FrameLayout by setting in the onClickListener() setVisibility(View.VISIBLE);

PS. A FrameLayout is a great "container" for a single Fragment.

Here's the code to do it:

Layout file: (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<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="com.yourDomain.yourApplicationName.MainActivity">

    <FrameLayout
        android:id="@+id/layout_1"
        android:background="@android:color/holo_purple"
        android:layout_width="match_parent"
        android:layout_height="150dp">
    </FrameLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_green_light">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button 1"/>

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button 2"/>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

MainActivity: (MainActivity.java)

package com.yourDomain.yourApplicationName;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button button1;
    Button button2;

    View frameLayout;

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

        frameLayout = findViewById(R.id.layout_1);

        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);

        setButtonBehavior();
    }

    private void setButtonBehavior() {
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                frameLayout.setVisibility(View.GONE);
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                frameLayout.setVisibility(View.VISIBLE);
            }
        });
    }
}
0
votes

You can use 2 fragments in one layout and then you can use visibilty property of fragments. Fragment is a partical layout that you can put buttons and other tools inside. Here is an explanation of fragments.

http://www.vogella.com/tutorials/AndroidFragments/article.html