Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix buttons not taking a translucent background color #3027

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

package com.google.appinventor.components.runtime;

import android.animation.StateListAnimator;
import android.graphics.drawable.RippleDrawable;
import android.os.Build;
import android.view.ViewOutlineProvider;
import com.google.appinventor.components.annotations.Asset;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.IsColor;
Expand Down Expand Up @@ -38,7 +40,6 @@
import android.graphics.drawable.shapes.OvalShape;
import android.graphics.drawable.shapes.RectShape;
import android.graphics.drawable.shapes.RoundRectShape;
import android.graphics.PorterDuff;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
Expand Down Expand Up @@ -116,6 +117,12 @@ public abstract class ButtonBase extends AndroidViewComponent
// obtained using BitmapDrawable's getBitmap() method.
private Bitmap backgroundBitmap;

// This is the original outline provider created for the button.
private Object defaultOutlineProvider;

// This is the original state animator created for the button.
private Object defaultStateAnimator;

//Whether or not the button is in high contrast mode
private boolean isHighContrast = false;

Expand Down Expand Up @@ -150,6 +157,10 @@ public ButtonBase(ComponentContainer container) {
defaultColorStateList = view.getTextColors();
defaultButtonMinWidth = KitkatUtil.getMinWidth(view);
defaultButtonMinHeight = KitkatUtil.getMinHeight(view);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
defaultOutlineProvider = view.getOutlineProvider();
defaultStateAnimator = view.getStateListAnimator();
}

// Adds the component to its designated container
container.$add(this);
Expand Down Expand Up @@ -540,24 +551,37 @@ private void setShape() {
throw new IllegalArgumentException();
}

if (backgroundColor != Component.COLOR_DEFAULT && !container.$form().HighContrast()) {
drawable.getPaint().setColor(backgroundColor);
}

// Set drawable to the background of the button.
if (!AppInventorCompatActivity.isClassicMode() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ViewUtil.setBackgroundDrawable(view, new RippleDrawable(createRippleState(), drawable, drawable));
} else {
ViewUtil.setBackgroundDrawable(view, drawable);
}

// Hides the button shadow on Material UI if the background is NONE
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (backgroundColor == Component.COLOR_NONE) {
view.setOutlineProvider(null);
view.setStateListAnimator(null);
} else {
view.setOutlineProvider((ViewOutlineProvider) defaultOutlineProvider);
view.setStateListAnimator((StateListAnimator) defaultStateAnimator);
}
}

if (backgroundColor == Component.COLOR_NONE) {
view.getBackground().setColorFilter(backgroundColor, PorterDuff.Mode.CLEAR);
}
else if (backgroundColor == Component.COLOR_DEFAULT) {
} else if (backgroundColor == Component.COLOR_DEFAULT) {
if (isHighContrast || container.$form().HighContrast()) {
view.getBackground().setColorFilter(Component.COLOR_BLACK, PorterDuff.Mode.SRC_ATOP);
} else {
view.getBackground().setColorFilter(SHAPED_DEFAULT_BACKGROUND_COLOR, PorterDuff.Mode.SRC_ATOP);
}
}
else {
} else {
view.getBackground().setColorFilter(backgroundColor, PorterDuff.Mode.SRC_ATOP);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2059,11 +2059,6 @@ public int AccentColor() {
@SimpleProperty(userVisible = false, description = "Sets the theme used by the application.",
category = PropertyCategory.APPLICATION)
public void Theme(String theme) {
if (SdkLevel.getLevel() < SdkLevel.LEVEL_HONEYCOMB) {
backgroundColor = Component.COLOR_WHITE;
setBackground(frameLayout);
return; // Only "Classic" is supported below SDK 11 due to minSDK in AppCompat
}
if (usesDefaultBackground) {
if (theme.equalsIgnoreCase("AppTheme") && !isClassicMode()) {
backgroundColor = Component.COLOR_BLACK;
Expand Down