MAY3AM
MAY3AM

Reputation: 1212

set OnClickListener to LinearLayout in Navigation Drawer

I have a Navigation Drawer in my activity that contain a LinearLayout and a ListView. Issue is When user click on Linear Layout, instead firing this view onClickListener, My Fragment onclickListener fired and wrong action happened!

enter image description here

This is my Navigation Drawer XML File:

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

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

    <LinearLayout
        android:id="@+id/linearDrawer"
        android:layout_width="260dp"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:background="#80ff0000"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/userContent"
            android:layout_width="260dp"
            android:layout_height="80dp"
            android:background="@android:color/black"
            android:gravity="center_vertical" >

            <RelativeLayout
                android:id="@+id/userDrawer"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/transparent" >

                <ImageView
                    android:id="@+id/ImgBackground"
                    android:layout_width="260dp"
                    android:layout_height="80dp"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:contentDescription="@string/app_name"
                    android:scaleType="centerCrop"
                    android:src="@drawable/parsvid_view_video_image_placeholder" />

                <FrameLayout
                    android:id="@+id/avatar_holder"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="13.5dp"
                    android:layout_marginTop="10dp" >

                    <com.meg7.widget.SvgImageView
                        android:id="@+id/avatar_strok"
                        android:layout_width="60.5dp"
                        android:layout_height="60.5dp"
                        android:scaleType="centerCrop"
                        android:src="#B3ffffff"
                        app:svg_raw_resource="@drawable/mask" />

                    <com.meg7.widget.SvgImageView
                        android:id="@+id/user_avatar_logged"
                        android:layout_width="55.5dp"
                        android:layout_height="55.5dp"
                        android:layout_gravity="right"
                        android:layout_marginRight="2.5dp"
                        android:layout_marginTop="2.5dp"
                        android:scaleType="centerCrop"
                        android:src="@drawable/user_avatar"
                        app:svg_raw_resource="@drawable/mask" />
                </FrameLayout>

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="5dp"
                    android:layout_marginTop="20dp"
                    android:layout_toLeftOf="@+id/avatar_holder" >

                    <TextView
                        android:id="@+id/txt_user_name_drawer"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="right"
                        android:text=""
                        android:textAllCaps="true"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textColor="@android:color/white" />

                    <TextView
                        android:id="@+id/txt_user_email"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/txt_user_name_drawer"
                        android:gravity="right"
                        android:text=""
                        android:textAllCaps="true"
                        android:textColor="@android:color/white"
                        android:textSize="12sp" />
                </RelativeLayout>
            </RelativeLayout>
        </LinearLayout>

        <View
            android:id="@+id/viewSeparator"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#FFffbb00" />

        <ListView
            android:id="@+id/drawer_list"
            android:layout_width="260dp"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:background="#FF002bcc"
            android:cacheColorHint="@color/transparent"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="1dp" />
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

And my java code is:

LinearLayout mUserContent;
mUserContent = (LinearLayout) findViewById(R.id.userContent);
mUserContent.setClickable(true);
mUserContent.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        Toast.makeText(getApplicationContext(), "Clicked!!", Toast.LENGTH_LONG).show();

        if(LoginSession.isLoggedIn()){
            // do something
        }else{
            // do another thing
        }
    }
});

Where is the problem?

Upvotes: 0

Views: 1160

Answers (1)

asylume
asylume

Reputation: 544

Add android:clickable="true" to linearDrawer:

<LinearLayout
    android:id="@+id/linearDrawer"
    android:layout_width="260dp"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:background="#80ff0000"
    android:orientation="vertical"
    android:clickable="true" >

Or programmatically:

LinearLayout mLinearDrawer = (LinearLayout) findViewById(R.id.linearDrawer);
// method 1
mLinearDrawer.setClickable(true);
// method 2
mLinearDrawer.setOnClickListener(null);

You set clickable to smaller view, so it didn't catch all area.

Upvotes: 1

Related Questions