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);
}
});
}
}