5

I've been searching for a while with this issue and i cant find solution yet. For my design of DrawerLayout, I will not be using listview but linearlayout as container for my views. I test it first by adding a button inside the linearlayout but i cant click it. Heres the 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 text in the button once it is being loaded and its working.The only thing that is not working is when i click it. the drawer will just close.

/**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


}

Im looking forward for your input guys,

Thanks a lot.

chkm8
  • 1,302
  • 1
  • 13
  • 34

4 Answers4

9

Its so ironic, I just change the positions of my layout. I transferred the linearlayout below the fragment and its now working.

<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>

chkm8
  • 1,302
  • 1
  • 13
  • 34
  • I included a toolbar as a last child of the DrawerLayout, which was blocking click events, it worked after putting it as the one-but-last child and have the drawer fragment as last child like you have now – Klaasel Jul 04 '18 at 07:43
  • Thank you! That fixed it for me too – Sharone Lev Dec 22 '19 at 21:07
0

Check this ! reference

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;
            }
        });
Community
  • 1
  • 1
Devrath
  • 42,072
  • 54
  • 195
  • 297
0

Remove the OnClickListener that you've set on the DrawerLayout itself.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
0

check it---- cast your button with layout where u keep 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 activity--

 leftDrawerRel = (RelativeLayout)findViewById(R.id.whatYouWantInLeftDrawer);
    edit= (Button)leftDrawerRel.findViewById(R.id.edit);
    edit.setOnClickListener(this);
Mohammad Adil
  • 503
  • 6
  • 13