Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView requestFocus: How to prevent scrolling to top?

Is there any way to prevent an Android WebView from scrolling its content to the top when it receives a requestFocus ?

I need to manipulate several layers in an Activity, changing the focus between them. However, using requestFocus to move the focus back to the WebView always causes the WebView to jump to the top of the HTML page it is displaying. While I can reposition it afterwards, doing so throws off the order of other events being processed and thus adds significant further complexity to that code. The MUCH simpler solution would be to just prevent the WebView from repositioning its content content on what should just be a simple focus change. Is there a way to do this? (I've tried overriding onOverScrolled and onScrollChanged, but these don't seem to be called by the scrolling that is being done by the WebView on requestFocus.)

like image 987
BillB Avatar asked Oct 19 '22 10:10

BillB


1 Answers

After many attempts

WebSettings webSettings = mWebView.getSettings();
webSettings.setNeedInitialFocus(false);

the setNeedInitialFocus(false) may solve the problem.

like image 121
likaci Avatar answered Oct 21 '22 06:10

likaci