0

I know that I can do this in xml, but I want to do it programatically.

I'm using bar.setBackgroundDrawable(new ColorDrawable(0x20000000 + currentlySelectedLine.color)); and bar.setStackedBackgroundDrawable(new ColorDrawable(0x20000000 + currentlySelectedLine.color)); to change the background color of the actionBar.

In the values, values-v11 and values-v14 I have the same style - pointing to a parent of Theme.AppCompat.Light, without anything in it.

In the following picture, you can see what happens when I run the program on the emulator for API lvl 10. Whether I use the setStackedBackgroundDrawable method or not, doesn't seem to make a difference.

api v10-

In the following picture you can see what happens when I run the program on the emulator for API lvl 11. Whether I use the setStackedBackgroundDrawable method or not, doesn't seem to make a difference.

api v11+

So. My question is: Can you please tell me what should I do in order to make the background pinkish color appear on behind the tabs on API lvl 11+ devices?

SlumpA
  • 882
  • 12
  • 24

1 Answers1

1

Looking at the source code for android.support.v7, I found out that android.support.v7.app.ActionBar.setStackedBackgroundDrawable() does literally nothing.

As I've been using android.support.v7.app.ActionBarActivity, and doing the most of the test on Honeycomb, I thought that I wouldn't be able to use android.app.Activity.getActionBar(), as that method would return null.

But that's not the case in 4.1.2. In 4.1.2, using that method, I do get android.app.ActionBar.

Looking at this webpage I realized that there are more or less 0.1% of android users who use API lvl 11-13.

So, I modified my code to look like this:

  // activity is an instance of ActionBarActivity.
  // ActionBar = android.support.v7.app.ActionBar.
  ActionBar bar = activity.getSupportActionBar();
  bar.setBackgroundDrawable(new ColorDrawable(0x20000000 +
      currentlySelectedLine.color));
    if (android.os.Build.VERSION.SDK_INT > 13)
      setBackgroundColorV14(activity);

  // ...

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private void setBackgroundColorV14(final ActionBarActivity activity) {
  android.app.ActionBar bar = activity.getActionBar();
  bar.setStackedBackgroundDrawable(new ColorDrawable(0x20000000 +
      currentlySelectedLine.color));
}

Basically, the result is that I do have the background color behind the tabs on API v7-10 and API v14+.

SlumpA
  • 882
  • 12
  • 24
  • You really save me on that one, I know it's old but do you have an idea how to also custom the color of the underline tab selected ? – Damien Locque Mar 02 '15 at 15:00