6

I have a webview in a scrollview, when the Activity loads, it forces my scrollview to the bottom (where the webview is) once the webview finishes "loadData". How do I keep this from happening? I've tried this, but it jumps the screen up and down, which I don't want:

ScrollView scroll = (ScrollView)findViewById(R.id.detailsScroll);  
scroll.post(new Runnable()   
{  
    @Override  
    public void run()   
    {  
       ScrollView scroll = (ScrollView)findViewById(R.id.detailsScroll);  
       scroll.fullScroll(ScrollView.FOCUS_UP);  
    }  
});  
Floern
  • 33,559
  • 24
  • 104
  • 119
steve
  • 805
  • 2
  • 9
  • 17
  • 2
    You shouldn't put a webview in a scrollview. Scrollable views inside scrollable views doesn't work. – Falmarri Dec 30 '10 at 22:30
  • ok, so how should I do this if the contents of the Activity are larger than the screen? – steve Dec 30 '10 at 22:37
  • ...and isn't this what admob is doing? It seems like the webview is gaining focus once the contents are loaded, which is causing the scrollview to jump. – steve Dec 30 '10 at 22:51
  • 1
    @steve: "how should I do this if the contents of the Activity are larger than the screen?" -- you can use a `ScrollView`, just keep the `WebView` outside of it. Allocate, say, 2/3rds of the screen to the `WebView` and the rest to the `ScrollView`, or something. – CommonsWare Dec 30 '10 at 22:58
  • @CommonsWare - thanks, that makes sense. I'm going to have to place this in a different Activity I guess, it's too large. – steve Dec 30 '10 at 23:03
  • Isn't that a bit undesirable? What if I want the WebView on "bottom" of the ScrollView, so once a user scrolls completely the WebView can be seen, but only then? – powerj1984 May 06 '11 at 16:45

4 Answers4

10

The answer of fhucho is OK for stoping the webview from scrolling to bottom. But you will lose all accessibility with trackball, etc. So I found an improvement :

public class MyScrollView extends ScrollView {

    @Override 
    public void requestChildFocus(View child, View focused) { 
        if (focused instanceof WebView ) 
           return;
    super.requestChildFocus(child, focused);
    }
}

Hope it helps !

Sylphe
  • 1,553
  • 1
  • 12
  • 13
5

Create a custom ScrollView with this method:

@Override
public void requestChildFocus(View child, View focused) {
    if (child instanceof WebView) return;
}
fhucho
  • 34,062
  • 40
  • 136
  • 186
2

For focus related issues, I found this worked better, adding these to your WebView in XML:

    android:focusable="false"
    android:focusableInTouchMode="false"

Although for the actual question asked, just adding another view that gets the focus by default, instead of the WebView, should have resolved that issue. I think others like me might find this when trying to deal with other general issues with a WebView inside a ScrollView taking the focus. Adding the above to the XML does not stop hyperlinks in the WebView from working.

eselk
  • 6,764
  • 7
  • 60
  • 93
1

add this to your main layout

android:descendantFocusability="blocksDescendants"