BomberBus
BomberBus

Reputation: 1235

Change ActionBar Background Color Dynamically

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));
        }

    }
});

Upvotes: 8

Views: 4330

Answers (2)

user3465277
user3465277

Reputation: 219

Make something like this:

         actionBar.setBackgroundDrawable(Color.BLACK);

It is the easiest way

Upvotes: -1

Santhi Bharath
Santhi Bharath

Reputation: 2878

Please try like this

ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#ffFEBB31"));
actionBar.setBackgroundDrawable(colorDrawable);

Upvotes: 6

Related Questions