AlienArt Software
AlienArt Software

Reputation: 127

Clicking Floating ActionButton on right corner makes RecyclerView item get clicked

I want to add Floating Action Button on recyclerview but problem is when i click on Floating Action Button Recyclerview item get clicked how to remove this problem

see below code

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".screens.ShowSubjectsFrag"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/MainList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center">
    </android.support.v7.widget.RecyclerView>
    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_input_add"
        app:layout_anchor="@id/MainList"
        android:layout_margin="@dimen/fab_margin"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

Upvotes: 6

Views: 1027

Answers (3)

Mohammed Rampurawala
Mohammed Rampurawala

Reputation: 3112

Write the View#onClickListener for the FloatingActionButton in your Activity/Fragment because currently your FloatingActionButton is not registered for any event. Had the same problem Cheers

Upvotes: 9

Saket Jain
Saket Jain

Reputation: 1382

You are using RecyclerView inside a RelativeLayout and you have set its width and height both to match_parent! Basically your RecyclerView is hiding your FloatingActionButton.

If I were you, I would do the following:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".screens.ShowSubjectsFrag"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/MainList"
        android:layout_width="match_parent"
        android:layout_height="300dp" <!-- Use a fixed height over here -->
        android:layout_alignParentTop="true">
    </android.support.v7.widget.RecyclerView>
    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_input_add"
        app:layout_anchor="@id/MainList" 
        android:layout_margin="@dimen/fab_margin"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

I also suspect that the layout_anchor is not required. Try removing it as well. Regardless, the main problem seems to me to be that your RecyclerView is hiding your FloatingActionButton. You can check the same in the Design area (If you are using Android Studio).

Let me know if it helps, otherwise I'll deep dive further!

Upvotes: -1

AndiGeeky
AndiGeeky

Reputation: 11474

Please try using below code in your xml :

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/MainList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center">
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/ic_input_add"
    app:layout_anchor="@id/MainList"
    android:layout_margin="@dimen/fab_margin"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

Put both view in different layouts so they can relate to different z levels.

For more information check design practices here.

Thanks..!!

Upvotes: -1

Related Questions