Skip to content

Commit

Permalink
[TopAppBar] Updated to take a CSL as the AppBarLayout background color.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 527090028
  • Loading branch information
pekingme authored and leticiarossi committed Apr 26, 2023
1 parent 2e85f06 commit 6980c40
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions lib/java/com/google/android/material/appbar/AppBarLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.ColorStateListDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Build.VERSION;
Expand All @@ -51,6 +52,7 @@
import android.widget.ScrollView;
import androidx.annotation.ColorInt;
import androidx.annotation.Dimension;
import androidx.annotation.DoNotInline;
import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.IntDef;
Expand Down Expand Up @@ -234,7 +236,7 @@ public AppBarLayout(@NonNull Context context, @Nullable AttributeSet attrs, int
context = getContext();
setOrientation(VERTICAL);

if (Build.VERSION.SDK_INT >= 21) {
if (VERSION.SDK_INT >= 21) {
// Use the bounds view outline provider so that we cast a shadow, even without a
// background
if (getOutlineProvider() == ViewOutlineProvider.BACKGROUND) {
Expand All @@ -256,10 +258,10 @@ public AppBarLayout(@NonNull Context context, @Nullable AttributeSet attrs, int
MaterialResources.getColorStateList(
context, a, R.styleable.AppBarLayout_liftOnScrollColor);

if (getBackground() instanceof ColorDrawable) {
ColorDrawable background = (ColorDrawable) getBackground();
ColorStateList backgroundCSL = getBackgroundCSL();
if (backgroundCSL != null) {
MaterialShapeDrawable materialShapeDrawable = new MaterialShapeDrawable();
materialShapeDrawable.setFillColor(ColorStateList.valueOf(background.getColor()));
materialShapeDrawable.setFillColor(backgroundCSL);
// If there is a lift on scroll color specified, we do not initialize the elevation overlay
// and set the alpha to zero manually.
if (liftOnScrollColor != null) {
Expand All @@ -283,12 +285,12 @@ public AppBarLayout(@NonNull Context context, @Nullable AttributeSet attrs, int
/* force */ false);
}

if (Build.VERSION.SDK_INT >= 21 && a.hasValue(R.styleable.AppBarLayout_elevation)) {
if (VERSION.SDK_INT >= 21 && a.hasValue(R.styleable.AppBarLayout_elevation)) {
ViewUtilsLollipop.setDefaultAppBarLayoutStateListAnimator(
this, a.getDimensionPixelSize(R.styleable.AppBarLayout_elevation, 0));
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (VERSION.SDK_INT >= VERSION_CODES.O) {
// In O+, we have these values set in the style. Since there is no defStyleAttr for
// AppBarLayout at the AppCompat level, check for these attributes here.
if (a.hasValue(R.styleable.AppBarLayout_android_keyboardNavigationCluster)) {
Expand Down Expand Up @@ -321,6 +323,17 @@ public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets)
});
}

@Nullable
private ColorStateList getBackgroundCSL() {
Drawable background = getBackground();
if (background instanceof ColorDrawable) {
return ColorStateList.valueOf(((ColorDrawable) background).getColor());
} else if (VERSION.SDK_INT >= VERSION_CODES.Q) {
return DrawableHelperV29.maybeGetBackgroundCSL(background);
}
return null;
}

private void initializeLiftOnScrollWithColor(MaterialShapeDrawable background) {
background.setAlpha(lifted ? 255 : 0);
background.setFillColor(liftOnScrollColor);
Expand Down Expand Up @@ -2555,4 +2568,16 @@ public void onOffsetChanged(
}
}
}

@RequiresApi(VERSION_CODES.Q)
private static class DrawableHelperV29 {
@DoNotInline
@Nullable
private static ColorStateList maybeGetBackgroundCSL(@Nullable Drawable background) {
if (background instanceof ColorStateListDrawable) {
return ((ColorStateListDrawable) background).getColorStateList();
}
return null;
}
}
}

0 comments on commit 6980c40

Please sign in to comment.