0

I am creating an android application where in one activity I was displaying tabs with the help of actionbar, So I have to change actionbar background color. But whenever I am trying to change the background color it will display either white background or black background. So how can I modify the action bar to change its background color?

Below is my activity code:-

public class SavedRecord extends FragmentActivity implements ActionBar.TabListener{

    private ActionBar actionBar;
    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;

    String[] tabs = {"Audio","Video"};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.saved_record);

        //initialization
        actionBar = getActionBar();
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setIcon(null);
        actionBar.setDisplayUseLogoEnabled(false);
        actionBar.setTitle("Saved Record");
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#ff7043"));
        actionBar.setStackedBackgroundDrawable(colorDrawable);
        for (String tab_name:tabs){
            actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
        }

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {

    }
}

And the screenshot of the activity:-

enter image description here

Gaurav Takte
  • 607
  • 5
  • 22

2 Answers2

1

I assume that you are using the "regular" ActionBar.

Try to use the SupportActionBar by importing

import android.support.v7.app.ActionBar;

and calling

actionBar = getSupportActionBar();

I think this should fix your problem.

W3hri
  • 1,563
  • 2
  • 18
  • 31
0

Try to change using style of your App

<resources>
    <!-- Base application theme. -->
     <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:actionBarStyle">@style/ActionBarStyle</item>
     </style>

     <style name="ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">/*your custom color*/</item>
     </style>
</resources>
Olena Y
  • 233
  • 2
  • 8