Stevenjobs
Stevenjobs

Reputation: 71

how can i add a webView but when someone click on any link it should open in default browser?

I have a WebView and I wanted to when someone click on a link it should open in a browser not in my app. How can I do that?

I want to show a webpage but when someone click on it it should open in a new browser that's all I want. How can i do that?

Here is the code that I am using

public class tab2 extends Fragment {

WebView webView ;
ProgressBar progressBar;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.tab2, container, false);



    webView = (WebView) view.findViewById(R.id.webview1);
    progressBar = (ProgressBar)view.findViewById(R.id.progressBar1);
    webView.setWebViewClient(new myWebClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://facebook.com");

    return view;
}

public class myWebClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
    }




    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub
        progressBar.setVisibility(View.VISIBLE);
        view.loadUrl(url);
        return true;

    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);

        progressBar.setVisibility(View.GONE);
    }
}


}

Upvotes: 1

Views: 52

Answers (1)

Zaggo0
Zaggo0

Reputation: 83

Instead of

view.loadUrl(url);

You could try to use:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);

Upvotes: 1

Related Questions