So, I did it as follows:
First of all, add the following dependency to build.gradle(app) :
compile 'com.android.support:customtabs:23.1.1'
First create a WebviewActivity action:
WebViewActivity.java
public class WebviewActivity extends AppCompatActivity { public static final String EXTRA_URL = "extra.url"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_webview); String url = getIntent().getStringExtra(EXTRA_URL); WebView webView = (WebView)findViewById(R.id.webview); webView.setWebViewClient(new WebViewClient()); WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); setTitle(url); getSupportActionBar().setDisplayHomeAsUpEnabled(true); webView.loadUrl(url); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) {
Now, let's say, when you click the button, you want to open the chrome user tab and download www.google.com , so do it as shown below:
public void onButtonClick(View view){ CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build(); CustomTabActivityHelper.openCustomTab( this,
activity_webview.xml
<?xml version="1.0" encoding="utf-8"?> <WebView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".helpers.WebviewActivity" />
Then just skip the classes as shown below:
CustomTabActivityHelper.java import android.app.Activity;
import android.net.Uri; import android.os.Bundle; import android.support.customtabs.CustomTabsClient; import android.support.customtabs.CustomTabsIntent; import android.support.customtabs.CustomTabsServiceConnection; import android.support.customtabs.CustomTabsSession; import java.util.List; public class CustomTabActivityHelper implements ServiceConnectionCallback { private CustomTabsSession mCustomTabsSession; private CustomTabsClient mClient; private CustomTabsServiceConnection mConnection; private ConnectionCallback mConnectionCallback; public static void openCustomTab(Activity activity, CustomTabsIntent customTabsIntent, Uri uri, CustomTabFallback fallback) { String packageName = CustomTabsHelper.getPackageNameToUse(activity);
CustomTabsHelper.java import android.content.Context;
import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.net.Uri; import android.text.TextUtils; import android.util.Log; import java.util.ArrayList; import java.util.List; public class CustomTabsHelper { private static final String TAG = "CustomTabsHelper"; static final String STABLE_PACKAGE = "com.android.chrome"; static final String BETA_PACKAGE = "com.chrome.beta"; static final String DEV_PACKAGE = "com.chrome.dev"; static final String LOCAL_PACKAGE = "com.google.android.apps.chrome"; private static final String EXTRA_CUSTOM_TABS_KEEP_ALIVE = "android.support.customtabs.extra.KEEP_ALIVE"; private static final String ACTION_CUSTOM_TABS_CONNECTION = "android.support.customtabs.action.CustomTabsService"; private static String sPackageNameToUse; private CustomTabsHelper() {} public static void addKeepAliveExtra(Context context, Intent intent) { Intent keepAliveIntent = new Intent().setClassName( context.getPackageName(), KeepAliveService.class.getCanonicalName()); intent.putExtra(EXTRA_CUSTOM_TABS_KEEP_ALIVE, keepAliveIntent); } public static String getPackageNameToUse(Context context) { if (sPackageNameToUse != null) return sPackageNameToUse; PackageManager pm = context.getPackageManager();
ServiceConnection.java
import android.content.ComponentName; import android.support.customtabs.CustomTabsClient; import android.support.customtabs.CustomTabsServiceConnection; import java.lang.ref.WeakReference; public class ServiceConnection extends CustomTabsServiceConnection {
ServiceConnectionCallback.java
import android.support.customtabs.CustomTabsClient; public interface ServiceConnectionCallback { void onServiceConnected(CustomTabsClient client); void onServiceDisconnected(); }
WebviewFallback.java
import android.app.Activity; import android.content.Intent; import android.net.Uri; public class WebviewFallback implements CustomTabActivityHelper.CustomTabFallback { @Override public void openUri(Activity activity, Uri uri) { Intent intent = new Intent(activity, WebviewActivity.class); intent.putExtra(WebviewActivity.EXTRA_URL, uri.toString()); activity.startActivity(intent); } }
Now you can open the Chrome tab.
If you have any problems, just let me know.
Thanks.