Scrollview not scrolling in Android bottomsheet
Hope you figured out this by now, but change View bottomSheet
to NestedScrollView bottomSheet
.
I solved the issue by doing below things -
First: don't use ScrollView when you are using CoordinatorLayout instead user NestedScrollView its works much better with CoordinatorLayout.
Second: put a blank view with
android:layout_height
at the bottom, but inside you NestedScrollVieweg -<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical"> <ImageView android:background="@drawable/username" android:id="@+id/userImage_info_search" android:layout_gravity="center" android:layout_height="100dp" android:layout_margin="20dp" android:layout_width="100dp" /> <LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent"> <View android:background="@android:color/black" android:layout_height="1dp" android:layout_width="match_parent"></View></LinearLayout><LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal" android:padding="10dp" android:weightSum="3"> <TextView style="@style/Bottomsheetstyle" android:id="@+id/txtNamelabel_info_search" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_weight="1" android:layout_width="0dp" android:text="Name" /> <TextView style="@style/Bottomsheetstyle" android:id="@+id/txtName_info_search" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_weight="2" android:layout_width="0dp" android:text="" /></LinearLayout><LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent"> <View android:background="@android:color/black" android:layout_height="1dp" android:layout_width="match_parent"></View></LinearLayout><LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal" android:padding="10dp" android:weightSum="3"> <TextView style="@style/Bottomsheetstyle" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_weight="1" android:layout_width="0dp" android:text="Number" /> <LinearLayout android:layout_height="wrap_content" android:layout_weight="2" android:layout_width="0dp" android:orientation="horizontal"> <TextView style="@style/Bottomsheetstyle" android:gravity="center_vertical" android:id="@+id/txtNumber_info_search" android:layout_gravity="center_vertical" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_weight="1.4" android:layout_width="0dp" android:text="+XX (XXX) XXX-XXXX" /> <ImageView android:background="@drawable/call_save" android:id="@+id/call_info_search" android:layout_height="wrap_content" android:layout_weight="0.3" android:layout_width="0dp" /> <View android:layout_gravity="center" android:layout_height="5dp" android:layout_width="5dp"></View> <ImageView android:background="@drawable/comment_save" android:id="@+id/sms_info_search" android:layout_height="wrap_content" android:layout_weight="0.3" android:layout_width="0dp" /> </LinearLayout></LinearLayout><LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent"> <View android:background="@android:color/black" android:layout_height="1dp" android:layout_width="match_parent"></View></LinearLayout><LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal" android:padding="10dp" android:weightSum="3"> <TextView style="@style/Bottomsheetstyle" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_weight="1" android:layout_width="0dp" android:text="Email" /> <TextView style="@style/Bottomsheetstyle" android:id="@+id/txtEmail_info_search" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_weight="2" android:layout_width="0dp" android:text="" /></LinearLayout><LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent"> <View android:background="@android:color/black" android:layout_height="1dp" android:layout_width="match_parent"></View></LinearLayout><View android:background="@android:color/transparent" android:layout_height="@dimen/somedp" android:layout_width="match_parent" />
Addition to the answer of "jobbert":
If you always return "false" it could happen that the bottomsheet is not working at all. This happend to me, when I also use a viewpager inside the bottom-sheets coordinatorlayout. To actually fix it, one needs to check whether the touch was inside the nestedscrollview or not. This can be calculated easily and leads to the most general solution:
override fun onInterceptTouchEvent(parent: CoordinatorLayout, child: V, event: MotionEvent): Boolean { val nested = child.findViewById<NestedScrollView>(R.id.nested) //NestedScrollView var x = event.x var y = event.y val position = IntArray(2) nested.getLocationOnScreen(position) var nestedX = position[0] var nestedY = position[1] var boundLeft = nestedX var boundRight = nestedX + nested.width var boundTop = nestedY var boundBottom = nestedY + nested.height if ((x > boundLeft && x < boundRight && y > boundTop && y < boundBottom) || event.action == MotionEvent.ACTION_CANCEL) { //Touched inside of the scrollview-> pass the touch event to the scrollview return false } //touched outside, use the parents computation to make the bottomsheet work return super.onInterceptTouchEvent(parent, child, event)}