Sorry to be so annoying, but my project isn’t compiling. The reason is that “package androidx.browser.customtabs does not exist”. However, it is right in the Google Developers documentation!
Just to be sure, I enabled AndroidX support.
My code:
package com.gordonlu.customtabs;
import android.app.Activity;
import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.PropertyTypeConstants;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import java.lang.Object;
import androidx.appcompat.app.AppCompatActivity;
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.core.content.ContextCompat;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.PropertyCategory;
@DesignerComponent(
version = 1,
description = "A non-visible component that opens web pages as custom tabs on Google Chrome.",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "https://docs.google.com/drawings/d/e/2PACX-1vQCI87PHLBF0jb8QWyYmIRQSjjNW3EFXf-qpsWCvBYkUQ9vEgPAB8SpxcMpblxNpbIYrjCjLrRLIU2c/pub?w=16&h=16")
@SimpleObject(external = true)
//Libraries
@UsesLibraries(libraries = "")
//Permissions
@UsesPermissions(permissionNames = "")
public class CustomTabs extends AndroidNonvisibleComponent{
//Activity and Context
private Context context;
private Activity activity;
public String url = "https://www.google.com";
public int col = -16711936;
public CustomTabs(ComponentContainer container){
super(container.$form());
this.activity = container.$context();
this.context = container.$context();
}
@SimpleProperty(category = PropertyCategory.BEHAVIOR,
description = "The URL to load in the custom tab. The URL must start with 'http://' or 'https://'.")
public String URL() {
return url;
}
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_STRING,
defaultValue = "https://appinventor.mit.edu/")
@SimpleProperty
public void URL(String url) {
this.url = url;
}
@SimpleProperty(category = PropertyCategory.BEHAVIOR,
description = "Specifies the color of the toolbar.")
public int ToolbarColor() {
return col;
}
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_INTEGER,
defaultValue = "-16711936")
@SimpleProperty
public void ToolbarColor(int argb) {
this.col = col;
}
@SimpleFunction(description = "Use this block to open the custom tab.")
public void OpenCustomTab() {
CustomTabsIntent.Builder customIntent = new CustomTabsIntent.Builder();
customIntent.setToolbarColor(ContextCompat.getColor(MainActivity.this, R.color.purple_200));
openCustomTab(MainActivity.this, customIntent.build(), Uri.parse(url));
}
public static void OpenCustomTabOpenPage(Activity activity, CustomTabsIntent customTabsIntent, Uri uri) {
String packageName = "com.android.chrome";
activity.startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
}
Error message:
Started Compiling Project CustomTabs
Buildfile: /compiler/androidX/build.xml
javac:
[mkdir] Created dir: /compiler/androidX/build/HoWSX/classes
[javac] Compiling 1 source file to /compiler/androidX/build/HoWSX/classes
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] Note: Wrote file file:/compiler/androidX/build/HoWSX/classes/simple_components.json
[javac] Note: Wrote file file:/compiler/androidX/build/HoWSX/classes/simple_components.txt
[javac] Note: Wrote file file:/compiler/androidX/build/HoWSX/classes/simple_components_build_info.json
[javac] Note: Wrote file file:/compiler/androidX/build/HoWSX/classes/AutogeneratedOdeMessages.java
[javac] Note: Wrote file file:/compiler/androidX/build/HoWSX/classes/ComponentsTranslation.java
[javac] /compiler/androidX/src/HoWSX/com/gordonlu/customtabs/CustomTabs.java:22: error: package androidx.browser.customtabs does not exist
[javac] import androidx.browser.customtabs.CustomTabsIntent;
[javac] ^
[javac] /compiler/androidX/src/HoWSX/com/gordonlu/customtabs/CustomTabs.java:89: error: cannot find symbol
[javac] public static void OpenCustomTabOpenPage(Activity activity, CustomTabsIntent customTabsIntent, Uri uri) {
[javac] ^
[javac] symbol: class CustomTabsIntent
[javac] location: class CustomTabs
[javac] 2 errors