I would like to know is it possible to change ActionBar
(in Support Library) Background Color dynamically according to selected page in viewpager or selected actionbar tabs. I have tried with the following code. But, it doesn't work. Please help.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
if(position == 0){
actionBar.setBackgroundDrawable(new ColorDrawable(Color.GREEN));
}else if(position == 1){
actionBar.setBackgroundDrawable(new ColorDrawable(Color.BLUE));
}else{
actionBar.setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
}
}
});
To change background color of Action Bar in Kotlin Android, set the colorPrimary in themes. xml, with a required color. We can also dynamically change the background color of Action Bar programmatically by setting background drawable for support action bar with the required color drawable.
Please try like this
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#ffFEBB31"));
actionBar.setBackgroundDrawable(colorDrawable);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With