1

I want to implement an address bar like the default Browser app which can be hidden if a user scrolls a webpage downward and shown again when the user scrolls the webpage to the top then scroll upward.

Thanks!

basicsharp
  • 446
  • 3
  • 8
  • You could always read the [Browser source code](http://android.git.kernel.org/?p=platform/packages/apps/Browser.git;a=summary). – Josh Lee Mar 16 '11 at 19:43

2 Answers2

0
    try {
        Method m = WebView.class.getMethod("setEmbeddedTitleBar", new Class[] { View.class });
        m.invoke(mWebView, url_bar);
    }
    catch(Exception e) {
    }  

when mWebView is the web view (..) and the url_bar is the view you want at the top.

edit
read more here and pay attention to the comments in the accepted answer.

Community
  • 1
  • 1
Royi
  • 745
  • 8
  • 22
0

set an onTouchListener for you webview that checks wv.getScrollY() to see if it is 0. Like this:

mWebView.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent me){
            if(me.getAction() == MotionEvent.ACTION_UP){ //You might try with MotionEvent.ACTION_MOVE also, but you'll get way more calls.
                if(mWebView.getScrollY() == 0){
                    //You are at the top, do what you need to do in order to show your address bar.
                }
            }

            return false;
        }
    });

This example will work if the user scrolls with their finger and releases their finger all the way at the top. Which isn't going to handle all of the possible ways that the user scrolled to the top. I think to handle them all the ideal solution is to Override WebView and add a Listener to it that will send you a call whenever getScrollY() changes from anything to zero.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • I want the same scroll behavior of the Android built-in browser, not just check when WebView's scroll is at the top. – basicsharp Mar 19 '11 at 08:56