Handling Links in a Webview

handling links in a webview

I assume you are already overriding shouldOverrideUrlLoading, you just need to handle this special case.

mWebClient = new WebViewClient(){

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("mailto:")){
MailTo mt = MailTo.parse(url);
Intent i = newEmailIntent(MyActivity.this, mt.getTo(), mt.getSubject(), mt.getBody(), mt.getCc());
startActivity(i);
view.reload();
return true;
}

else{
view.loadUrl(url);
}
return true;
}
};
mWebView.setWebViewClient(mWebClient);

public static Intent newEmailIntent(Context context, String address, String subject, String body, String cc) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { address });
intent.putExtra(Intent.EXTRA_TEXT, body);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_CC, cc);
intent.setType("message/rfc822");
return intent;
}

Handling external links in android webview

Use this line in Manifest file of your Activity which is Handling that intent to avoid such problem.

android:launchMode="singleTask"

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 launch a web link from within my WebView in a registered app on Android?

The fix simply involved handling shouldOverrideUrlLoading to true, and if the url isn't within my own app, returning true. When true is returned, it launches registered app or app chooser dialog or browser, depending on the configured context.

mWebview.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean isLocalUrl = false;
try {
URL givenUrl = new URL(url);
String host = givenUrl.getHost();
if(host.contains("myapp.com"))
isLocalUrl = true;

} catch (MalformedURLException e) {

}

if (isLocalUrl)
return super.shouldOverrideUrlLoading(view, url);
else
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
});

I also realized that the current status on Android (I tested on MIUI 7 on Xiaomi Mi 4, based on Android Kitkat, but forums indicate that this is broadly true for most pre-Marshmallow androids at least) where a link to a facebook page in the WebView does not open in the Facebook app. Nor does it give user the option to open in Facebook app. This used to work earlier, but does not work now. It always launches in the browser if we use the above code. A lot of the misunderstanding happened because of this. It works as expected with other sites (twitter, quora, etc.).

How to get URL from long click links in a webview?

public class MainActivity extends AppCompatActivity {

WebView webView;
String URL1 = "https://stackoverflow.com/questions/60577403/how-to-get-url-from-long-click-links-in-a-webview/60577736?noredirect=1#comment107173847_60577736";

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

webView = findViewById(R.id.webview);

webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setWebViewClient(new WebViewClient() {

public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

}

public void onPageFinished(WebView view, String url) {

}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
URL1 = url;
return super.shouldOverrideUrlLoading(view, url);
}
});
webView.loadUrl(URL1);
// Register the context menu for web view
registerForContextMenu(webView);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);

// Get the web view hit test result
final WebView.HitTestResult result = webView.getHitTestResult();

// If user long press on url
if (result.getType() == WebView.HitTestResult.ANCHOR_TYPE ||
result.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE) {

// Set the title for context menu
menu.setHeaderTitle("\t\t\t\t\t\t\t\t\t\t ◦ ◉ ⦿ Select ⦿ ◉ ◦ \t");

// Add an item to the menu
menu.add(0, 1, 0, " \t \t➤\t Show URL")
.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
String Pressed_url = result.getExtra();
Toast.makeText(MainActivity.this, "URL is:-" + Pressed_url,
Toast.LENGTH_SHORT).show();
return false;
}
});
}
}
}

Download Full Code from github.

Output

Menu URL

Handling External Links in android WebView like Gmail App does

It is pretty simple. You have to use Chrome Custom Tabs as suggested by Gergely as well in comment. Below is the small functional code that will help you to achieve this.

First add this dependency to your build.gradle(Module:app)

compile 'com.android.support:customtabs:23.4.0'

Second add below function to your code and simply pass string URL to it.

private void redirectUsingCustomTab(String url)
{
Uri uri = Uri.parse(url);

CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();

// set desired toolbar colors
intentBuilder.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary));
intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));

// add start and exit animations if you want(optional)
/*intentBuilder.setStartAnimations(this, android.R.anim.slide_in_left, android.R.anim.slide_out_right);
intentBuilder.setExitAnimations(this, android.R.anim.slide_in_left,
android.R.anim.slide_out_right);*/

CustomTabsIntent customTabsIntent = intentBuilder.build();

customTabsIntent.launchUrl(activity, uri);
}

Rest it will take care itself. Since Chrome Custom Tabs can customised so lot can be done like you can add menu to toolbar. For detailed information you can visit official documentation from Google itself here.

Hope it will help you to start with :)



Related Topics



Leave a reply



Submit