Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling External Links in android WebView like Gmail App does

I'm a web developer. I'm currently developing android application on Android Studio using WebView which access my website as an android application. One of my webpage contains many external links. My goal is to make the android application can handle external links like Gmail App does (also like facebook and Line do). Below is the example of gmail app.

An email contains external link

Link clicked, then application open a new activity acts like a browser without leaving Gmail application

Any idea how to make it?

like image 945
Dika Avatar asked Mar 14 '17 07:03

Dika


People also ask

How do I open links outside of Gmail?

Tap the menu button at the upper left corner of the app. Select “Settings” from the side menu. Scroll to the bottom and turn off “Open web links in Gmail”.

What is Mobile WebView Gmail?

Android System WebView lets applications display browser windows in an app instead of transporting the user to another browser. Android developers use WebView when they want to display webpages in a Google app or other application.

What apps use Android WebView?

A lot of important digital products that are well known as app platforms are actually WebView apps. While most companies don't share their technology, we know that Facebook, Evernote, Instagram, LinkedIn, Uber, Slack, Twitter, Gmail, the Amazon Appstore, and many others are or have been WebView apps.


1 Answers

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 :)

like image 127
Geek Avatar answered Sep 24 '22 02:09

Geek