Last active
January 15, 2020 20:44
-
-
Save NikolaDespotoski/e587669f3d9b1519bc5e to your computer and use it in GitHub Desktop.
Toggle Toolbar background alpha and alpha of its title view.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ToolbarAlphaScrollBehavior extends CoordinatorLayout.Behavior<android.support.v7.widget.Toolbar> { | |
private ColorDrawable mStatusBarColorDrawable; | |
private int mStatusBarColor; | |
private TextView mTitleView; | |
private boolean searchedForTitleView = false; | |
public ToolbarAlphaScrollBehavior(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
mStatusBarColor = ContextCompat.getColor(context, R.color.primary_dark); | |
mStatusBarColor = getColorWithAlpha(0, mStatusBarColor); | |
mStatusBarColorDrawable = new ColorDrawable(mStatusBarColor); | |
} | |
public static int getColorWithAlpha(float alpha, int baseColor) { | |
int a = Math.min(255, Math.max(0, (int) (alpha * 255))) << 24; | |
int rgb = 0x00ffffff & baseColor; | |
return a + rgb; | |
} | |
public ToolbarAlphaScrollBehavior() { | |
} | |
@Override | |
public boolean layoutDependsOn(CoordinatorLayout parent, Toolbar child, View dependency) { | |
return dependency instanceof AppBarLayout; | |
} | |
@Override | |
public boolean onInterceptTouchEvent(CoordinatorLayout parent, Toolbar child, MotionEvent ev) { | |
return ev == null || super.onInterceptTouchEvent(parent, child, ev); | |
} | |
@Override | |
public boolean onDependentViewChanged(CoordinatorLayout parent, Toolbar child, View dependency) { | |
if (dependency instanceof AppBarLayout) { | |
float ratio = (float) getCurrentScrollValue(child, dependency) / getTotalScrollRange(child, dependency); | |
float alpha = 1f - Math.min(1f, Math.max(0f, ratio)); | |
int drawableAlpha = (int) (alpha * ViewCompatHelper.DRAWABLE_ALPHA); | |
// Log.i("ToolbarAlphaScrollBehavior", "Alpha: " + alpha); | |
if (ViewCompatHelper.isL()) { | |
child.getBackground().setAlpha(drawableAlpha); | |
} else if (ViewCompatHelper.isKitKat()) { | |
ViewGroup toolbarParent = (ViewGroup) child.getParent(); | |
if (toolbarParent.getChildCount() == 2) { | |
int count = toolbarParent.getChildCount(); | |
for (int i = count - 1; i >= 0; i--) { | |
toolbarParent.getChildAt(i).getBackground().setAlpha(drawableAlpha); | |
} | |
} | |
} else { | |
child.getBackground().setAlpha(drawableAlpha); | |
} | |
// setStatusBarColor(parent, drawableAlpha); | |
if (mTitleView != null) { | |
ViewCompat.setAlpha(mTitleView, alpha); | |
return false; | |
} | |
if (!searchedForTitleView) { | |
mTitleView = ViewHelper.getTitleView(child); | |
searchedForTitleView = true; | |
} | |
} | |
return false; | |
} | |
private void setStatusBarColor(CoordinatorLayout parent, int alpha) { | |
ColorDrawable statusBarBackground = (ColorDrawable) parent.getStatusBarBackground(); | |
statusBarBackground.setColor(getColorWithAlpha(alpha, statusBarBackground.getColor())); | |
parent.setStatusBarBackground(statusBarBackground); | |
} | |
private int getCurrentScrollValue(Toolbar child, View dependency) { | |
return dependency.getBottom() - child.getTop(); | |
} | |
private float getTotalScrollRange(Toolbar child, View dependency) { | |
return Math.max(dependency.getHeight(), ((AppBarLayout) dependency).getTotalScrollRange()) - child.getTop(); | |
} | |
} |
i tried to use it but the class is never called
<android.support.design.widget.AppBarLayout
.......
android:fitsSystemWindows="true"
android:background="@android:color/transparent"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
.........................
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_behavior=".components.ToolbarAlphaScrollBehavior"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/content_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
can you please check what is wrong in my layout
@Fshamri For behavior to work, it must be direct descendant of the CoordnatorLayout. In other words, you need to take the Toolbar out of the CollapsingToolbarLayout.
The value of ViewCompatHelper.DRAWABLE_ALPHA is 255, the maximum drawable alpha value.
what's the code of the ViewCompatHelper class
what is ViewCompatHelper class?
This guy is the worst. Such a tease lol.. Let's do better, honestly.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great but what is the value of ViewCompatHelper.DRAWABLE_ALPHA?