Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button inside DrawerLayout is not clickable

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.

like image 983
chkm8 Avatar asked Aug 15 '14 03:08

chkm8


People also ask

What is DrawerLayout in android?

DrawerLayout acts as a top-level container for window content that allows for interactive "drawer" views to be pulled out from one or both vertical edges of the window.

How to use Navigation drawer?

The user can view the navigation drawer when the user swipes a finger from the left edge of the activity. They can also find it from the home activity by tapping the app icon in the action bar. The drawer icon is displayed on all top-level destinations that use a DrawerLayout.

How to add DrawerLayout in android?

The drawer icon is displayed on all top-level destinations that use a DrawerLayout . To add a navigation drawer, first declare a DrawerLayout as the root view. Inside the DrawerLayout , add a layout for the main UI content and another view that contains the contents of the navigation drawer.


1 Answers

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>

like image 96
chkm8 Avatar answered Nov 15 '22 01:11

chkm8