How to Prevent a Scrollview from Scrolling to a Webview After Data Is Loaded

How to prevent a scrollview from scrolling to a webview after data is loaded?

You can simply add this to your LinearLayout: android:focusableInTouchMode="true". It works for me.

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

Disable scrolling in webview?

Here is my code for disabling all scrolling in webview:

  // disable scroll on touch
webview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});

To only hide the scrollbars, but not disable scrolling:

WebView.setVerticalScrollBarEnabled(false);
WebView.setHorizontalScrollBarEnabled(false);

or you can try using single column layout but this only works with simple pages and it disables horizontal scrolling:

   //Only disabled the horizontal scrolling:
webview.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

You can also try to wrap your webview with vertically scrolling scrollview and disable all scrolling on the webview:

<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical" >
<WebView
android:id="@+id/mywebview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none" />
</ScrollView>

And set

webview.setScrollContainer(false);

Don't forget to add the webview.setOnTouchListener(...) code above to disable all scrolling in the webview. The vertical ScrollView will allow for scrolling of the WebView's content.

Prevent WebView from scrolling to top of page after HTML is added

I found a way around my problem. If anyone has another, especially a more direct, answer I would still be interested.

I was, in part, mis-diagnosing my problem. When I add HTML using: document.body.insertBefore(newDiv, topDiv.nextSibling);, I add my new HTML just below the top edge of the document.

I thought I was scrolling back up to the top of the screen (co-ordinates [0,0]), but instead the document length was changing and my current Y position stayed the same. So the co-ordinates I was viewing were constant, but the HTML those co-ordinates displayed changed. The effect was "scrolling up" the page.

I ended up adding a HUGE div of white space above my visible text. Every time I add HTML it shrinks by the exact height of the added HTML. This offsets the added text, allowing my visible screen to stay the exact same. This means NO scrolling, which is a much more fluid UI.

This gives a whole different set things to work around, like stopping the user from scrolling the whole length of my white space and ensuring I don't make my WebView larger than my max size. The trade-offs are well worth it though, as the user experience is drastically improved.

Webview prevent scroll up from specific height

Instead of programmatically scroll down do those changes in xml.

<WebView
android:layout_marginTop="-10dp"
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

This will crop the content that you want to hide from top

There is no way to prevent ScrollView from automatically scrolling when TextView is focused?

You can make a subclass of ScrollView and override the protected method: computeScrollDeltaToGetChildRectOnScreen(Rect rect) and return 0 to disable the autoscrolling effect.

computeScrollDeltaToGetChildRectOnScreen

Compute the amount to scroll in the Y direction in order to get a
rectangle completely on the screen (or, if taller than the screen, at
least the first screen size chunk of it).

Kotlin Example:

class CustomScrollView : ScrollView {
constructor(context: Context?) : super(context) {}
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}

override fun computeScrollDeltaToGetChildRectOnScreen(rect: Rect): Int {
return 0
}
}

Xml Usage:

<?xml version="1.0" encoding="utf-8"?>
<mypackagename.com.CustomScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="I am text 1. Click me."
android:textColor="@color/black"
android:textIsSelectable="true"
android:background="#FF0000"
android:layout_width="match_parent"
android:layout_height="400dp"/>
<TextView
android:text="I am text 2. Click me."
android:textIsSelectable="true"
android:background="#00FF00"
android:textColor="@color/black"
android:layout_width="match_parent"
android:layout_height="1000dp"/>
</LinearLayout>
</mypackagename.com.CustomScrollView>


Related Topics



Leave a reply



Submit