I try to create a fade alpha on my actionbar during a scroll event of my main ScrollView :
I 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 actionbar will fade its alpha between position 0 and 300 of the ScrollView. Unfortunately, my AB still transparent everytime... Any idea ?
I found the problem, I use a pre Jelly Bean device so there's a workaround for this issue.
In fact, with pre-JELLY_BEAN_MR1 device, we have to attach the following Callback
in the onCreate(Bundle)
method for the 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, thank you to Cyril Mottier for this !