Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

    }
});
like image 328
BomberBus Avatar asked May 17 '14 07:05

BomberBus


People also ask

How can change ActionBar background color in android programmatically?

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.


1 Answers

Please try like this

ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#ffFEBB31"));
actionBar.setBackgroundDrawable(colorDrawable);
like image 118
Santhi Bharath Avatar answered Sep 28 '22 15:09

Santhi Bharath