Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show/hide grouped views in Android?

I want to create an activity such as mentioned in photo... as soon as I press the maximize button I want it to become full screen for the activity and part 1 become minimize, and again when I press the restore button I want it to become in a first state: to be able to see part 1 and part 2 ...

I think if we put two layouts it is possible? Isn't it? Please refer me to a resource to help me about this, or show me the code to achieve a solution.

enter image description here

like image 682
Hossein Mansouri Avatar asked Sep 17 '13 14:09

Hossein Mansouri


People also ask

What is view and view group in Android?

View is a basic building block of UI (User Interface) in android. A view is a small rectangular box that responds to user inputs. Eg: EditText, Button, CheckBox, etc. ViewGroup is an invisible container of other views (child views) and other ViewGroup. Eg: LinearLayout is a ViewGroup that can contain other views in it.


2 Answers

Part one and two should be in their own layout. After, play with the visilibity property of each layout. Specifically to hide any view without it continues to occupy its space, use the value gone for the visibility property.

Ok, here I go. Below you have 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 ;)

like image 110
Diego Palomar Avatar answered Oct 14 '22 09:10

Diego Palomar


There is a bit simplified solution, than Diego Palomar produced, without using additional variable. I'll take his code to show:

    public class MyActivity extends Activity implements OnClickListener { 

    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 (mViewGroup.getVisibility() == View.VISIBLE) {
        mViewGroup.setVisibility(View.GONE);
        mButton.setText("Show");
    } else {
        mViewGroup.setVisibility(View.VISIBLE);
        mButton.setText("Hide");
    }

}
like image 35
Anton Avatar answered Oct 14 '22 09:10

Anton