Part one and two should be in their own layout. After that, play with the visilibity property of each layout. To hide any view without taking its place, use the value gone for the visibility property.
Ok, here I go. Below is a complete example of how to hide / show grouped views.
main.xml
<RelativeLayout 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/viewsContainer" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="5dp" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextBox One" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="TextBox Two" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="TextBox Three" /> </LinearLayout> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="Hide" /> </RelativeLayout>
activity
public class MyActivity extends Activity implements OnClickListener { private boolean viewGroupIsVisible = true; private View mViewGroup; private Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mViewGroup = findViewById(R.id.viewsContainer); mButton = findViewById(R.id.button); mButton.setOnClickListener(this); } @Override public void onClick(View button) { if (viewGroupIsVisible) { mViewGroup.setVisibility(View.GONE); mButton.setText("Show"); } else { mViewGroup.setVisibility(View.VISIBLE); mButton.setText("Hide"); } viewGroupIsVisible = !viewGroupIsVisible; }
I hope this helps;)
source share