Change alpha action bar during scroll event
I am trying to fade alpha in my action bar while scrolling my main ScrollView:
I've tried this:
//Active Overlay for ActionBar before set my content in the activity
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
//create a new Drawable for the AB background
final ColorDrawable newColor = new ColorDrawable(getResources().getColor(R.color.primaryColor));
newColor.setAlpha(0);
getActionBar().setBackgroundDrawable(newColor);
//create a OnScrollListener to get ScrollY position
final ScrollView mainScrollView = (ScrollView) findViewById(R.id.place_detail_scrollView);
mainScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
final int scrollY = mainScrollView.getScrollY();
final int boundY = 300;
final float ratio = (float) (scrollY/boundY) ;
newColor.setAlpha((int)(ratio*255));
}
});
In theory, my action screen will fade its alpha between position 0 and 300 of the ScrollView. Unfortunately my AB is still transparent every time ... Any idea?
+3
source to share
1 answer
I found the problem, I am using a pre Jelly Bean device, so there is a workaround for this problem.
In fact, with the pre-JELLY_BEAN_MR1 device, we have to attach Callback
to the onCreate(Bundle)
method onCreate(Bundle)
for Drawable
:
private Drawable.Callback mDrawableCallback = new Drawable.Callback() {
@Override
public void invalidateDrawable(Drawable who) {
getActionBar().setBackgroundDrawable(who);
}
@Override
public void scheduleDrawable(Drawable who, Runnable what, long when) {
}
@Override
public void unscheduleDrawable(Drawable who, Runnable what) {
}
};
then set the callback
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
mActionBarBackgroundDrawable.setCallback(mDrawableCallback);
}
And that's all, thanks to Cyril Mottier for this!
+4
source to share