27

I have created one activity which is containing one map, image and another text view, and I have added "scrollview" tag for it. But after the activity starts it scrolls to the end of the page automatically. Please tell me why this happens and how to stop it from moving to end so I can scroll it by myself.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.radikallab.earch.DetailsActivity"
android:background="@drawable/background10">


<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="Title"
        android:textStyle="italic"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/background4"
        android:id="@+id/iv"/>


    <TextView
        android:id="@+id/rat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="Title"
        android:textStyle="italic"/>

    <TextView
        android:id="@+id/addr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="17sp"
        android:text="Title"
        android:textStyle="italic"/>

    <WebView

        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:layout_marginTop="10dp"
        android:id="@+id/webView"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

Anand Tripathi
  • 14,556
  • 1
  • 47
  • 52

6 Answers6

71

I also face the same scenario once but adding the descendantFocusability attribute to the ScrollView's containing LinearLayout, solve my issue.

android:descendantFocusability="blocksDescendants"

you can use this as :

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants" >
Punit Sharma
  • 2,951
  • 1
  • 20
  • 35
  • Is there any other way to block any sort of auto-scrolling that does not disable focus? I have TextViews with selection capability inside of the LinearLayout. Using this method disables text selection. – Damn Vegetables Jun 24 '21 at 23:12
12

Set android:focusableInTouchMode="true" in your linear layout:

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:focusableInTouchMode="true"
android:layout_height="match_parent">
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Rohit Sharma
  • 2,017
  • 1
  • 20
  • 22
4

What worked for me is a combination of the answers from Punit Sharma and FAЯAƸ.

My issue was a scrollable view (RecyclerView) inside another scrollable view (NestedScrollView) -- same as your WebView inside a ScrollView.

Wrapping my inner scrollable view inside a FragmentLayout with attribute android:descendantFocusability="blocksDescendants" solved my auto-to-bottom-scrolling issue.

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        ....

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:descendantFocusability="blocksDescendants"
            ...>
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/image_gridview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
       </FrameLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
jhavatar
  • 3,236
  • 1
  • 18
  • 11
3

In case you do want to set this manually using Java; Try this- it really works: Use FOCUS_UP to scroll to the top and FOCUS_DOWN to scroll it to the Bottom.

scrollView.post(new Runnable() {
                        public void run() {
                            scrollView.fullScroll(View.FOCUS_UP);
                        }
1

Actually i found a simple answer for this just add "setfocusable(false)" in the associated java file.....in this case the "webview" coz its scroll there first....so add "webview.setfocusable(false)" in the associated webview java class.

0

Actually, we will face the scrolling problem if child inside ScrollView supports scrolling too like ListView and in your case WebView.

I found a similar question with some workaround. It supports vertical scrolling but horizontal scrolling is not supported if HTML page exceeds the WebView width.

See this for reference.

Community
  • 1
  • 1
Faraz
  • 2,144
  • 1
  • 18
  • 28