Processing external links in android WebView, for example, the Gmail application

I am a web developer. I am currently developing an Android application in Android Studio using a WebView that accesses my site as an android application. One of my web pages contains many external links. My goal is to get the Android application to use external links, such as the Gmail application (as well as facebook and Line do). The following is an example gmail application.

Email contains external link

The link is clicked, then the application opens a new activity, like a browser, without leaving the Gmail application

Any idea how to do this?

+5
source share
1 answer

It is pretty simple. You should use custom Chrome tabs as suggested by Gergely, as well as in the comments. Below is a little functional code to help you achieve this.

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

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

Second, add the function below to your code and just pass it the string URL.

 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 takes care of itself. Since custom Chrome tabs can be customized so that you can do as you can add a menu to the toolbar. For more information, you can visit the official documentation from Google itself here .

Hope this helps you get started with :)

+4
source

Source: https://habr.com/ru/post/1265421/


All Articles