I got the following exception when trying to change the background of pages inside a ViewPager
in the onPageScrolled
method. I have edited the question in order to make it more clear.
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1245)
at android.content.res.Resources.getColor(Resources.java:899)
at android.support.v4.content.ContextCompat.getColor(ContextCompat.java:413)
at com.noel.material_onboarding.OnboardingActivity.color(OnboardingActivity.java:113)
at com.noel.material_onboarding.OnboardingActivity.access$200(OnboardingActivity.java:29)
at com.noel.material_onboarding.OnboardingActivity$1.onPageScrolled(OnboardingActivity.java:86)
First I create the slider objects, this includes setting up the background color:
addSlide(new SlideFragmentBuilder()
.description("This is a test")
.backgroundColor(R.color.colorPrimary)
.build());
addSlide(new SlideFragmentBuilder()
.description("This is a test 2")
.backgroundColor(R.color.green)
.build());
addSlide(new SlideFragmentBuilder()
.description("This is a test 3")
.backgroundColor(R.color.orange)
.build());
addSlide(new SlideFragmentBuilder()
.description("This is a test 4")
.backgroundColor(R.color.orange)
.build());
Here's a link to the SlideFragmentBuilder on github and the Fragment class itself
Here's my onPageScrolled method:
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
int colorUpdate = (Integer) evaluator.evaluate(positionOffset, color(mOnboardingAdapter.getItem(position).backgroundColor()), color(mOnboardingAdapter.getItem(position + 1).backgroundColor()));
mViewPager.setBackgroundColor(colorUpdate);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(colorUpdate);
}
}
@Override
public void onPageSelected(int position) {
btnFinish.setVisibility(position == mOnboardingAdapter.getLastItemPosition() ? View.VISIBLE : View.GONE);
btnNext.setVisibility(position == mOnboardingAdapter.getLastItemPosition() ? View.GONE : View.VISIBLE);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
The color() method that is used
private int color(@ColorRes int color){
return ContextCompat.getColor(this, color);
}
Basically, I just need the background of one page to fade in as the user swipes to another page.