Pardeep Kumar
Pardeep Kumar

Reputation: 950

How to hide layout when clicking the are outside it

I have a RelativeLayout which has two child Relative Layouts.Lets call them rel1 and rel2. Rel1 has one title at the top . then a profile picture in the center . and two horizontally placed buttons at the bottom .

Rel2 has one listview . Rel2 is aligned at the bottom of the parent layout . Rel2 takes 50% of the screen and initially its not visible . When i click on one of the buttons in rel1 the rel2 becomes visible . Since rel2 takes half of the screen so , upper half of rel1 is visible and lower half of rel1 gets covered by rel2 .

My requirement is that when i click the upper half of the rel1 (which is not covered by rel2) , rel2 should hide.

How do i do this . should i add onclick listener on rel1 layout . If i add onclick on rel1 , what will happen when i click the children of rel1 layout . will the click event go to its children or the rel1 ..

Please help.

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible" >

        <RelativeLayout 
android:id="@+id/rel1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:paddingBottom="23dp"
            android:paddingTop="12dp" >

            <TextView
                android:id="@+id/title"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
                android:ellipsize="end"
                android:gravity="center_horizontal"
                android:maxLines="1"
                android:singleLine="true"
                android:text="Title"
                android:textSize="20sp" />

        <ImageView
            android:id="@+id/center_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/Image"
            android:contentDescription="center IMage" />

            <LinearLayout
        android:id="@+id/buttonView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="12dp"
        android:baselineAligned="false"
        android:orientation="horizontal" >

            <ImageButton
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_marginRight="1dp"
                android:background="@drawable/button1"
                android:contentDescription="@string/str_avoid_warning"/>
            <ImageButton
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_marginLeft="1dp"
                android:background="@drawable/button2"
                android:contentDescription="@string/str_avoid_warning"/>
    </LinearLayout>

        <RelativeLayout
            android:id="@+id/rel2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:visibility="gone">

            <ListView
                android:id="@+id/listview"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:cacheColorHint="@android:color/transparent"
                android:fadingEdgeLength="7dp"
                android:overScrollMode="never"
                android:divider="@android:color/transparent"
                android:dividerHeight="10.0sp"
                android:requiresFadingEdge="vertical">
            </ListView>
        </RelativeLayout>

    </RelativeLayout>

Upvotes: 3

Views: 3162

Answers (4)

sandeep singh
sandeep singh

Reputation: 7

make first relativelayout clickable and add click listener on first it will hide second

 Rel1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {             
     if(Rel2.getVisibility()==View.VISIBLE) {
        Rel2.setVisibility(View.GONE);
      } 
}
});

Upvotes: 0

Yash Jain
Yash Jain

Reputation: 374

 <RelativeLayout 
        android:id="@+id/rel1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingBottom="23dp"
        android:onClick="hideMe"
        android:clickable=true
        android:paddingTop="12dp" >


 public void hideMe(View view){
 ViewGroup viewgroup = (ViewGroup) getLayoutInflater().inflate(R.layout.rel2, null);
 viewgroup.setVisibility(View,GONE);//CHECK THE OTHER OPTION BELOW
 }

View.GONE -Everything is removed, Space is available for other widgets View.INVISIBLE -Everything will be removed but space is not available to other wigets

Upvotes: 0

Abhishek Patel
Abhishek Patel

Reputation: 4328

Try This it may be help to you

private RelativeLayout llRoot;
private RelativeLayout llContent;

llRoot = (RelativeLayout) findViewById(R.id.rel1);//in onCreate()
llContent = (RelativeLayout) findViewById(R.id.rel2);//in onCreate()

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    Rect ContentBounds = new Rect();
    llRoot.getHitRect(ContentBounds);

    if (ContentBounds.contains((int) ev.getX(), (int) ev.getY())) {
        //Do Your stuff here

    }

    return super.dispatchTouchEvent(ev);
}

Upvotes: 3

antonio
antonio

Reputation: 18242

Make both rel1 and rel2 clickable adding android:clickable="true" and then add an OnClickListener to rel1.

Upvotes: 0

Related Questions