Skip to content

Instantly share code, notes, and snippets.

@jaisonfdo
Created May 30, 2017 17:47
Show Gist options
  • Save jaisonfdo/f21e756b6fd5c3d2675643e42542da91 to your computer and use it in GitHub Desktop.
Save jaisonfdo/f21e756b6fd5c3d2675643e42542da91 to your computer and use it in GitHub Desktop.
This gist is used to implement Chrome Custom Tab in your Android app. For further details :
package com.droidmentor.chromecustomtab;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.customtabs.CustomTabsIntent;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
String TAG = "MainActivity";
Button btnLaunch;
String url;
public static MainActivity instance = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
instance = this;
btnLaunch = (Button) findViewById(R.id.btnLaunch);
btnLaunch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Loading url
url = "http://droidmentor.com/";
// CustomTabsIntent.Builder used to configure CustomTabsIntent.
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder().setShowTitle(true);
// Change the background color of the Toolbar.
builder.setToolbarColor(ResourcesCompat.getColor(getResources(), R.color.colorPrimary, null));
// Configure custom enter and exit animations
builder.setStartAnimations(MainActivity.this, R.anim.slide_in_right, R.anim.slide_out_left);
builder.setExitAnimations(MainActivity.this, R.anim.slide_in_left, R.anim.slide_out_right);
// Setting a custom back button
builder.setCloseButtonIcon(BitmapFactory.decodeResource(
getResources(), R.drawable.ic_arrow_back));
// Hide status bar once the user scrolls down the content
builder.enableUrlBarHiding();
// For adding menu item
builder.addMenuItem("Share", getItem());
// For adding Action button
builder.setActionButton(BitmapFactory.decodeResource(getResources(),
R.drawable.ic_share), "Share", getItem(), true);
// CustomTabsIntent used to launch the URL
CustomTabsIntent customTabsIntent = builder.build();
//Open the Custom Tab
customTabsIntent.launchUrl(MainActivity.this, Uri.parse(url));
}
});
}
private PendingIntent getItem() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Droidmentor is a site, which contains android tutorials");
Log.d(TAG, "setMenuItem: " + url);
shareIntent.putExtra(Intent.EXTRA_TEXT, url);
return PendingIntent.getActivity(this, 0, shareIntent, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment