Reputation: 149
I'm pretty new to Android development and building a simple demo app to try out the Android custom tabs functionality. Yet somehow when I launched the app and tested it on the virtual device, I still see the URL is launched with webView instead of custom tabs. Am I missing something? Thanks so much!
*the reason I'm trying is to bypass the facebook/Google login restriction nowadays on webview
my build.gradle
:
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.0"
defaultConfig {
applicationId "com.example.helloworld"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation "androidx.browser:browser:1.3.0"
}
My MainActivity.java:
package com.example.helloworld;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.browser.customtabs.CustomTabsIntent;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button loginBtn = findViewById(R.id.login);
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String loginUrl = "https://google.com";
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
customTabsIntent.launchUrl(getApplicationContext(), Uri.parse(loginUrl));
}
});
}
}
Upvotes: 0
Views: 1072
Reputation: 1266
User needs to have a browser app installed. Most browsers these days do support CustomTabs.
If there is not browser installed or the installed browser does not support CustomTabs, CustomTabs opens the URL in WebView.
Upvotes: 1
Reputation: 325
Use PackageManager
to check the device' s browser in code.
https://developer.chrome.com/docs/android/custom-tabs/integration-guide/
Upvotes: 1