Button inside DrawerLayout cannot be pressed

I searched for a while with this problem and I have not yet found a solution. For my DrawerLayout project, I will not use listview, but linearlayout as a container for my views. I test it first by adding a button inside the linear line, but I cannot click it. Heres XML.

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

<LinearLayout
        android:id="@+id/left_drawer"
        android:background="@color/white"
        android:orientation="vertical"
        android:layout_gravity="start"
        android:layout_width="250dp"
        android:layout_height="match_parent">
    <Button
            android:id="@+id/btn_test"
            android:text="TEST"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    <EditText

            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

</LinearLayout>

<FrameLayout
        android:id="@+id/container_fragment2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="1dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        >
</FrameLayout>

In java, I also tried to put the text in the button after loading it and its working. The only thing that doesn't work is when I click on it. the box will only be closed.

/**Init Drawer*/
private void initDrawer(){

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerContainer = (LinearLayout) findViewById(R.id.left_drawer);

    //mDrawerContainer.setOnClickListener(new DrawerItemClickListener());
    mDrawerContainer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(context,"initDrawer",Toast.LENGTH_SHORT).show();
        }
    });

    btn_test = (Button)mDrawerContainer.findViewById(R.id.btn_test);
    btn_test.setOnClickListener(new View.OnClickListener() { //this is not working,i cant click the button
        @Override
        public void onClick(View v) {
            Log.e("MyActivity","test");
            Toast.makeText(context,"test",Toast.LENGTH_SHORT).show();
        }
    });
    btn_test.setText("hooray"); //this one is working,it change the text to hooray once loaded


}

I look forward to your entrance guys

Many thanks.

+4
source share
4 answers

. linearlayout .

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



<FrameLayout
        android:id="@+id/container_fragment2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="1dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        >
</FrameLayout>

<LinearLayout
        android:id="@+id/left_drawer"
        android:background="@color/white"
        android:orientation="vertical"
        android:layout_gravity="start"
        android:layout_width="250dp"
        android:layout_height="match_parent">
    <Button
            android:id="@+id/btn_test"
            android:text="TEST"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    <EditText

            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

</LinearLayout>

+6

!

you have to set a TouchListener for your drawer layout.
eg/

    gestureDetector = new GestureDetector(this, new CustomGestureListener());

        mDrawerLayout.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        });
0

Uninstall OnClickListenerwhich you installed on DrawerLayout itself.

0
source

check this ---- drop your button with the layout, where u hold the button here is my xml

    <RelativeLayout
        android:id="@+id/whatYouWantInLeftDrawer"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#EBF4FA">
        <RelativeLayout
            android:id="@+id/top_view_rel"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@color/blue_color">

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_margin="10dp"
                android:visibility="visible">
                <TextView
                    android:id="@+id/name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/drawer_driver_name"
                    android:textColor="@color/text_color"
                    android:textSize="16sp"
                    android:textStyle="bold" />


                <TextView
                    android:id="@+id/name_tv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/name"
                    android:layout_marginLeft="135dp"
                    android:text="name"
                    android:textAllCaps="true"
                    android:textColor="@color/text_color"
                    android:textSize="16sp"
                    android:visibility="gone" />
                <Button
                    android:id="@+id/edit"
                    android:layout_width="30dp"
                    android:layout_height="25dp"

                    android:layout_alignParentRight="true"
                    android:background="@color/colorTransparent"
                    android:drawableRight="@drawable/edit1"
                   />

In action -

 leftDrawerRel = (RelativeLayout)findViewById(R.id.whatYouWantInLeftDrawer);
    edit= (Button)leftDrawerRel.findViewById(R.id.edit);
    edit.setOnClickListener(this);
0
source

All Articles