4

I am currently writing a webview, it first loads a twitter page (say, NHL, http://twitter.com/nhl) as you can see, you can find the tweet for NHL, and each NHL tweet has another link for user to click, e.g. bit.ly/ujcNZo

in my webview, if i click that link (i.e. bit.ly/ujcNZo), my webview, after 1 second, doesn't display anything but a twitter icon on a white color background, it should load the content but it didn't.

after some time of investigation, i think it has to do with the fact that the link in the tweet (i.e. bit.ly/ujcNZo) actually opens the link in a separate window (pop up?) and not the current page where the link is posted, i verified this by going to NHL's twitter page on a browser in my laptop.

My Question is, 1. is there a way i can load the content of the external link (bit.ly/ ujcNZo, for instance) in my current webview?

user1118019
  • 3,819
  • 8
  • 40
  • 46
  • I am having same problem with my webview non of answers worked for me. Have u solved this issue as I can see there are no comments since 4 months – nishi Apr 16 '12 at 09:50
  • no sir, unfortunately : /, please do let me know if you solved it... – user1118019 Apr 18 '12 at 16:54

1 Answers1

2

You can control this through the WebViewClient class. Simply extend it and override the default implementation to get the desired configuration then set it as the client for your current webview.

Activity

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

            // set the webViewClient to a new instance of your custom WebViewClient

    webview.setWebViewClient(new WebActivityClient( this ));

}

Custom Client

/** WebViewClient class for WebView in WebActivity */
private class WebActivityClient extends WebViewClient {

    public static final String TAG = "WebActivityClient";
    private Context context;

    /** Constructor used to grab the context */
    public WebActivityClient( Context context ) {
        this.context = context;
    }

    /** Override to load every link within the page inside this webview instead of using
     * android's default application
     * #7 http://developer.android.com/resources/tutorials/views/hello-webview.html */
    @Override
    public boolean shouldOverrideUrlLoading( WebView view, String url ) {
        view.loadUrl(url);
        return true;
    }

}

Hope this helps!

jerrylroberts
  • 3,419
  • 23
  • 22
  • 1
    Hi Jerry thank you for your reply. But your solution didn't work. My code is already doing what you are doing, except I am not passing in the context in the constructor. in fact, in your code, although you are passing the context, but you are no using it anywhere else, so I am not sure what it is for. this is not about loading a webpage in a webview, i already can do that. my problem is loading a link (THAT OPENS AS POPUP) in an existing webview. – user1118019 Dec 28 '11 at 04:28
  • 1
    after further testing, it seems any pop up link (i.e. a hyperlink that will open the content in a separate window) does not TRIGGER the shouldOverrideUrlLoading method...if you open a non-popup link...it always trigger the shouldOverrideUrlLoading method... – user1118019 Dec 28 '11 at 05:33
  • I didn't provide the complete code, just a line to show how to set the WebClient property and a partial implementation of that class. I figured that would point you in the right direction. There is a fine line between helping and doing the work for you hehehe So if you throw a breakpoint on the shouldOverrideUrlLoading it never gets called? If so, provide a test URL and I'll see what happens on my implementation. – jerrylroberts Dec 28 '11 at 12:12
  • Test URL was already provided in my original question, here it is again. http://bit.ly/ujcNZo Thank you for replying and testing for me Jerry, so basically in your webview, display this link http://bit.ly/ujcNZo, and run your emulator or on actual device, tried to click on that link (http://bit.ly/ujcNZo) you displayed on your webview, see if your webview takes that link and redirect you to the actual source. Thabks! – user1118019 Dec 29 '11 at 00:46
  • Hi Jerry, it's just that the implementation you had, I already have those, and that didn't solve my problem, thus I was wondering what the context argument is for. More observation made, it seems if the link is a redirect, e.g. http://bit.ly/ujcNZo (by the way,this is not some sort of bad link, it is a link from Twitter, feel free to get links like this on NFL's twitter if you don't trust me), then shouldOverrideUrlLoading is not called. – user1118019 Dec 29 '11 at 00:55