Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable a view behind my SlidingDrawer in Android?

I have a SlidingDrawer that pops up from the bottom of the screen and fills the screen about 80%. Even though the SlidingDrawer view is in focus, it is still possible to click on items, buttons and other elements in the view that is behind the SlidingDrawer. When SlidingDrawer is active/pulled up/in focus, I want to disable the entire view behind it so it will not be able to recieve clicks and touches. Is there a good way to disable an entire view? I have tried setEnable(false) and setClickable(false) but neither of them work.

Help?

like image 733
Sara Avatar asked Apr 07 '10 23:04

Sara


3 Answers

Here's a way to get around this problem (I needed a solution also) - grab the linearLayout that holds the contents and add a click listener. Have the click listener respond to clicks (throw away, whatever) - and this then stops it propagating to the view below the sliding drawer - it works for me - and it doesn't block the other items in the drawer.

like image 182
Joe... Avatar answered Sep 30 '22 22:09

Joe...


In side Layout do android:clickable="true"

For Example Following file is drawer.xml

Inside LinearLayout android:id="@+id/drawer_view" clickable="true"

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">


        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </FrameLayout>      

        <LinearLayout
            android:id="@+id/drawer_view"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_gravity="start"
            android:clickable="true"
            android:background="@color/drawer_background">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:background="@drawable/order_list_selector"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="@dimen/user_image_w_h"
                    android:layout_height="@dimen/user_image_w_h"
                    android:scaleType="fitCenter"
                    android:id="@+id/drawerUserImage"
                    android:src="@drawable/ic_user_icon"
                    android:layout_gravity="center_vertical" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:layout_gravity="center_vertical"
                    android:orientation="vertical"
                    android:layout_marginLeft="5dp"
                    android:padding="5dp">

                    <TextView
                        android:id="@+id/drawerUserNameTextView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Mansukh Ahir"
                        android:textColor="@android:color/black"
                        android:textSize="@dimen/font_large" />

                    <TextView
                        android:id="@+id/drawerEmailIdTextView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textSize="@dimen/font_very_small"/>
                </LinearLayout>
            </LinearLayout>

            <View
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:background="@color/holo_gray_light" />

            <ListView
                android:id="@+id/drawerListSlideMenu"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:choiceMode="singleChoice"
                android:dividerHeight="1dp" />
        </LinearLayout>

    </android.support.v4.widget.DrawerLayout>
like image 38
Mansukh Ahir Avatar answered Sep 30 '22 22:09

Mansukh Ahir


Joe's answer did not do the trick for me. My scenario is a bit diferent. I have a FrameLayout with two children. Only one of the children has to be 'active' at a given moment, and while the second is active the first should no longer process any input. My solution:

    public static void changeVGstate(ViewGroup current, boolean enable)
{
    current.setFocusable(enable);
    current.setClickable(enable);
    current.setEnabled(enable);

    for (int i = 0; i < current.getChildCount(); i++)
    {
        View v = current.getChildAt(i); 
        if (v instanceof ViewGroup)
            changeVGstate((ViewGroup)v, enable);
        else
        {
            v.setFocusable(enable);
            v.setClickable(enable);
            v.setEnabled(enable);
        }
    }
}

Enjoy!

like image 24
kellogs Avatar answered Sep 30 '22 23:09

kellogs