Ankur Khandelwal
Ankur Khandelwal

Reputation: 89

Upon pressing floating action button background activity also initiate

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/relativeView"
    android:background="@color/white"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="40dp"
        android:layout_below="@+id/today_date"
        android:orientation="vertical" >    

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.0"

            android:orientation="horizontal" >

            <LinearLayout
                android:orientation="horizontal"
                android:layout_marginBottom="@dimen/activity_vertical_margin"
                android:layout_marginEnd="@dimen/activity_vertical_margin"
                android:id="@+id/ll1"
                android:layout_width="match_parent"
                 android:layout_height="match_parent"
                android:background="@drawable/rectangle"
                android:layout_weight="1.0"
                />   

            <LinearLayout
                android:orientation="horizontal"
                android:layout_marginBottom="@dimen/activity_vertical_margin"
                android:layout_marginEnd="@dimen/activity_vertical_margin"
                android:id="@+id/ll2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/rectangle"
                android:layout_weight="1.0"
                />    
         </LinearLayout>

    <LinearLayout
        android:weightSum="2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:orientation="horizontal" >

        <LinearLayout
            android:orientation="horizontal"
            android:layout_marginBottom="@dimen/activity_vertical_margin"
            android:layout_marginEnd="@dimen/activity_vertical_margin"
            android:id="@+id/ll3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/rectangle"
            android:layout_weight="1.0"
             />

        <LinearLayout
            android:orientation="horizontal"
            android:layout_marginBottom="@dimen/activity_vertical_margin"
            android:layout_marginEnd="@dimen/activity_vertical_margin"
            android:id="@+id/ll4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/rectangle"
            android:layout_weight="1.0"
             >    
        </LinearLayout>    
    </LinearLayout>    
</LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_add_reminders"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_plus"
        app:backgroundTint="#ffffff"
        android:layout_gravity="bottom|end" />

</android.support.design.widget.CoordinatorLayout>

So whenever I press fab the background linear layout's attach activity is also open. So can anyone tell me what is the problem with the code? I don't know why this problem occurred even when put the FAB button outside the linear layout.

Java Code:-

fab=(FloatingActionButton) findViewById(R.id.fab_add_reminders);

        fab.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                //Set reminder dialog box appear
                openDialogToAddReminder();
                return false;
            }
        });


nightLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Constants.SelectedBucket="Night";
                startActivity(new Intent(Reminders.this, ListReminders.class));
            }
        });

Upvotes: 0

Views: 842

Answers (1)

MohanadMohie
MohanadMohie

Reputation: 1044

You are using an OnTouchListener on the FloatingActionButton instead of an OnClickListener, and you are also returning false inside the OnTouchListener, which means the fab doesn't consume the touch event and passes it to the next view below it (nightLL).

Change your code to this:

fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openDialogToAddReminder();
        }
    });

Or, if you want to use an OnTouchListener for any other reason, then use this code:

fab.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    Toast.makeText(FabClickActivity.this, "FAB Touch.", Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });

Upvotes: 1

Related Questions