Open External Links in the Browser With Android Webview

Open external links in the browser with android webview

The problem is you need to send an Intent to the default web browser to open the link. What you are doing is just calling a different method in your Webview to handle the link. Whenever you want another app to handle something you need to use Intents. Try this code instead.

private class CustomWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("message2space.es.vu")) {
view.loadUrl(url);
} else {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
return true;
}
}

opening link in external browser from page in WebView

Well, there is no such thing

instead it must be handled from android application code. you can add a parameter to the url when u need it to open in external browser, ( here it is external=true ) and then check for that parameter in your webview url loading as below:

        webView.setWebViewClient(new WebViewClient(){
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if((String.valueOf(request.getUrl())).contains("external=true")) {
Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
view.getContext().startActivity(intent);
return true;
} else {
view.loadUrl(String.valueOf(request.getUrl()));
}

return true;
}
});

Open External Links inside Webview

So i found the solution it is pretty much same for both GET and POST requests.

    webView = (WebView) findViewById(R.id.dashboard);

String url = "http://www.example.test";
String postData = "json=" + JSON;

webView.postUrl(url,postData.getBytes());

webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView viewx, String urlx) {
viewx.loadUrl(urlx);
return false;
}
});

how to open external links in webview

You should load the url from which you get the intent in OnResume() method.
Please have a look at the code below.

private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

webView = findViewById(R.id.external_url_content);
webView.getSettings().setJavaScriptEnabled(true);
}

@Override
protected void onResume() {
super.onResume();
Uri url = getIntent().getData();
if (url != null) {
Log.d("TAG", "URL Foud");
Log.d("TAG", "Url is :" + url);
webView.loadUrl(url.toString());
}
}

Also the intent-filters in manifest are as follows

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="http"/>
<data android:scheme="https"/>
</intent-filter>
</activity>

You can find more about Lifecycle Activity over here

Webview link opens in external browser

To help you better understand, I'm leaving my simple browser app code here for you.

Here SimpleBrowser is the MainActivity.

public class SimpleBrowser extends Activity implements OnClickListener {
WebView ourBrowser;
EditText url;
Button go, go_Back, go_Forward, refresh, clr_History;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browser_webview);

ourBrowser = (WebView) findViewById(R.id.WVBrowser);
/* WebView Settings pretty important */
ourBrowser.getSettings().setJavaScriptEnabled(true);
ourBrowser.getSettings().setLoadWithOverviewMode(true);
ourBrowser.getSettings().setUseWideViewPort(true);

ourBrowser.setWebViewClient(new ourViewClient());
try {
ourBrowser.loadUrl("http://www.mybringback.com");
} catch (Exception e) {
e.printStackTrace();
}

go = (Button) findViewById(R.id.btn_Go);
go_Back = (Button) findViewById(R.id.btn_GoBack);
go_Forward = (Button) findViewById(R.id.btn_GoForward);
refresh = (Button) findViewById(R.id.btn_RefreshPage);
clr_History = (Button) findViewById(R.id.btn_ClrHistory);
url = (EditText) findViewById(R.id.eT_webbrowser);

go.setOnClickListener(this);
go_Back.setOnClickListener(this);
go_Forward.setOnClickListener(this);
refresh.setOnClickListener(this);
clr_History.setOnClickListener(this);

}

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_Go:
String newWebaddress = url.getText().toString();
ourBrowser.loadUrl(newWebaddress);

/* Hiding the keyboard after the EditText data */
InputMethodManager ipmm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
ipmm.hideSoftInputFromWindow(url.getWindowToken(), 0);
break;

case R.id.btn_GoBack:
if (ourBrowser.canGoBack()) {
ourBrowser.goBack();
}
break;

case R.id.btn_GoForward:
if (ourBrowser.canGoForward()) {
ourBrowser.goForward();
}
break;

case R.id.btn_RefreshPage:
ourBrowser.reload();
break;

case R.id.btn_ClrHistory:
ourBrowser.clearHistory();
break;
}
}

}

Then have another Java file with name : ourViewClient.java and it should contain the below code:

public class ourViewClient extends WebViewClient {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);

return true; // as mentioned in below notes, for your case., you do 'return false'
}

}

Android Manifest file:
Ensure to have <uses-permission android:name="android.permission.INTERNET" /> declared.
browser_webview.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<EditText
android:id="@+id/eT_webbrowser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >

<requestFocus />
</EditText>

<Button
android:id="@+id/btn_Go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Go" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button
android:id="@+id/btn_GoBack"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Go back a Page"
android:textSize="@dimen/mediumsize" />

<Button
android:id="@+id/btn_GoForward"
android:layout_width="78dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Go Forward"
android:textSize="@dimen/mediumsize" />

<Button
android:id="@+id/btn_RefreshPage"
android:layout_width="66dp"
android:layout_height="wrap_content"
android:layout_weight="0.90"
android:text="Refresh Page"
android:textSize="@dimen/mediumsize" />

<Button
android:id="@+id/btn_ClrHistory"
android:layout_width="82dp"
android:layout_height="wrap_content"
android:text="Clear History"
android:textSize="@dimen/mediumsize" />
</LinearLayout>

<WebView
android:id="@+id/WVBrowser"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</WebView>

</LinearLayout>

Android Webview - How I can open external links in default browser?

You have to send an Intent to to the default web browser, in order to open the link in the browser, so maybe something like this:

Create a class that extends WebViewClient, in this way:

private class MyCustomWebViewClient extends WebViewClient {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("https://mywebsite.domain.com")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
} else {
view.loadUrl(url);
}
return true;
}
}

In your MainActivity, in the onCreate method, set the MyCustomWebViewClient before the call to loadUrl method, like this:

public class MainActivity extends AppCompatActivity {

DrawerLayout drawerLayout;
ActionBarDrawerToggle drawerToggle;
NavigationView navigation;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OneSignal.startInit(this).init();
initInstances();
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new MyCustomWebViewClient());
myWebView.loadUrl("http://mywebsite.domain.com/myprofile/");
}

// the rest of your code

How to use external browser on web view Android app

Problem SOlved guys thanks for your replays

Source (from sven)

webView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && (url.startsWith("http://") || url.startsWith("https://"))) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}

});

Open external WebView links in browser

You can override URL loading with following code. If you want to handle URL loading yourself return true. If the URL should be opend in the WebView, return false.

webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Insert logic here
}
}

If you just want to open external links you can use following code. In case the opened URL starts with your domain's base URL, false is returned and the URL is opened in the WebView. Otherwise the ACTION_VIEW intent is used to open the URL in the browser and true is returned.

webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Abort if no URL
if (url == null || !(url.startsWith("http://") ||
url.startsWith("https://"))) {
return false;
}

// Abort if internal URL
if (url.startsWith("http://www.myurl.com") ||
url.startsWith("https://www.myurl.com")) {
return false;
}

// Open external URL in browser
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
});

Notice: You have to handle "http://..." and "https://...". Because the method might get called for both.



Related Topics



Leave a reply



Submit