Search code examples
androidandroid-layoutandroid-collapsingtoolbarlayoutcalendarview

How to disable touch of top transparent layer?


I need to customise a Calendar having feature of Collapsing month view to week view. I am using MaterialCalendarView Library for this. For that i have taken a frame having MaterialCalendarView as a bottom layer and CollapsingToolbarLayout on the top as a transparent view. Now the problem is, i am not able to use my calendarView as it is a bottom layer and disable touch of CollapsingToolbarLayout.

I have tried setting android:clickable="false" of CoodinatorLayout and all of its child elements and also tried using : collapsingToolbarLayout.setEnabled(false); and setting touch listener to false, but no luck.

This is my XML file:

-----some code-----
---
--
-
<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:animateLayoutChanges="true">

        <com.prolificinteractive.materialcalendarview.MaterialCalendarView
            android:id="@+id/calendarView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            app:mcv_allowClickDaysOutsideCurrentMonth="false"
            app:mcv_firstDayOfWeek="monday"
            app:mcv_tileHeight="36dp" />

        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/coordinator_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:focusedByDefault="false"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/app_bar_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:clickable="false"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:focusedByDefault="false">

                <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/collapsing_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:clickable="false"
                    android:focusable="false"
                    android:focusableInTouchMode="false"
                    android:focusedByDefault="false"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed">

                    <android.support.v7.widget.Toolbar
                        android:id="@+id/dummy_toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="72dp"
                        android:background="@android:color/transparent"
                        android:clickable="false"
                        android:focusable="false"
                        android:focusableInTouchMode="false"
                        android:focusedByDefault="false"
                        android:visibility="invisible"
                        app:contentInsetStart="0dp"
                        app:layout_collapseMode="pin">

                        <View
                            android:id="@+id/dummy_toolbar_view"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="@android:color/transparent"
                            android:clickable="false"
                            android:focusable="false"
                            android:focusableInTouchMode="false"
                            android:focusedByDefault="false" />

                    </android.support.v7.widget.Toolbar>

                    <View
                        android:id="@+id/transparent_view"
                        android:layout_width="match_parent"
                        android:layout_height="252dp"
                        android:background="@android:color/transparent"
                        android:clickable="false"
                        android:focusable="false"
                        android:focusableInTouchMode="false"
                        android:focusedByDefault="false" />

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

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

            <android.support.v4.widget.NestedScrollView
                android:id="@+id/nested_scroll_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/bg_dark_gray"
                android:fillViewport="true"
                android:scrollbars="none"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:orientation="vertical">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/recycler_occasions"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:focusable="false" />

                    <TextView
                        android:id="@+id/txt_login"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginStart="@dimen/dp_50"
                        android:layout_marginTop="@dimen/dp_30"
                        android:layout_marginEnd="@dimen/dp_50"
                        android:layout_marginBottom="@dimen/dp_100"
                        android:background="@drawable/btn_gradient_bg"
                        android:fontFamily="@font/poppins_semibold"
                        android:gravity="center"
                        android:padding="@dimen/dp_16"
                        android:text="@string/next"
                        android:textAllCaps="true"
                        android:textColor="@color/white"
                        android:textSize="@dimen/btn_text_size" />
                </LinearLayout>

            </android.support.v4.widget.NestedScrollView>
        </android.support.design.widget.CoordinatorLayout>
    </RelativeLayout>

And this is what i am doing in java file:

collapsingToolbarLayout.setEnabled(false);
dummyToolbarView.setEnabled(false);
appBarLayout.setEnabled(false);
coordinatorLayout.setEnabled(false);

collapsingToolbarLayout.setOnTouchListener((v, event) -> {
      calendarView.dispatchTouchEvent(event);
      return false;
});
appBarLayout.setOnTouchListener((v, event) -> {
      calendarView.dispatchTouchEvent(event);
      return false;
});
coordinatorLayout.setOnTouchListener((v, event) -> {
      calendarView.dispatchTouchEvent(event);
      return false;
});
transparentView.setOnTouchListener((v, event) -> {
      calendarView.dispatchTouchEvent(event);
      return false;
});
dummyToolbarView.setOnTouchListener((v, event) -> {
      calendarView.dispatchTouchEvent(event);
      return false;
});


collapsingToolbarLayout.setClickable(false);
coordinatorLayout.setClickable(false);
appBarLayout.setClickable(false);
transparentView.setClickable(false);

After disabling touch of all the views, still onClick is getting called, that's why i have disable click also.

By doing this, i am able to handle touch of MaterialCalendarView when it is in collapsed mode, but when it is expanded then i am not able to use it, some other view is taking the click.

Please help me out with this. I want to access MaterialCalendarView touch and disable touch of views inside CoordinatorLayout except NestedScrollView.

Thanks in advance.


Solution

  • Thanks everyone for the efforts.

    I resolved this by overriding dispatchTouchEvent. Thanks @Abyss for this post - Solution 2 works for me: https://stackoverflow.com/a/21971924/6384924

    @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
            return super.dispatchTouchEvent(ev);
        }
    

    with dispatching touch of calendar on every view's touch

    collapsingToolbarLayout.setOnTouchListener((v, event) -> {
                calendarView.dispatchTouchEvent(event);
                return false;
            });
            dummyToolbarView.setOnTouchListener((v, event) -> {
                calendarView.dispatchTouchEvent(event);
                return false;
            });
            appBarLayout.setOnTouchListener((v, event) -> {
                calendarView.dispatchTouchEvent(event);
                return false;
            });
            coordinatorLayout.setOnTouchListener((v, event) -> {
                calendarView.dispatchTouchEvent(event);
                return false;
            });
            transparentView.setOnTouchListener((v, event) -> {
                calendarView.dispatchTouchEvent(event);
                return false;
            });
            dummyToolbar.setOnTouchListener((v, event) -> {
                calendarView.dispatchTouchEvent(event);
                return false;
            });