I'm_With_Stupid
I'm_With_Stupid

Reputation: 1112

Is it possible to programmatically change the color of the split actionbar?

How do I change the color of the split actionbar in code, and not in xml? My user can pick the color of the actionbar, and I would like it so they could also change the color of the split action bar (the actionbar that appears at the bottom of the screen).

I'm implementing splitActionBar using android:uiOptions="splitActionBarWhenNarrow" in android manifest

so far I'm trying this, but it's not working

final int splitBarId = getResources().getIdentifier("split_action_bar", "id", "android");

    final View splitActionBar = findViewById(splitBarId);

    if (splitActionBar != null) {

          splitActionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor(actionbar_colors)));

    }

        }

Upvotes: 0

Views: 395

Answers (3)

adneal
adneal

Reputation: 30814

The framework doesn't provide a way to change it programmatically; however, you can use Resources.getIdentifier to find the View and adjust the background Drawable from there.

The internal id is split_action_bar.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    final int splitBarId = getResources().getIdentifier("split_action_bar", "id", "android");
    final View splitActionBar = findViewById(splitBarId);
    if (splitActionBar != null) {
        // Adjust the background drawable
    }
}

Update

Evidently there's ActionBar.setSplitBackgroundDrawable. Definitely use that callback rather than Resources.getIdentifier.

Here's a screenshot of the results:

Example

Upvotes: 1

I'm_With_Stupid
I'm_With_Stupid

Reputation: 1112

Use this to change the split actionbar color

getActionBar().setSplitBackgroundDrawable(new ColorDrawable(Color.parseColor("#33B5E5")));

Or use this if you're using support actionbar

getSupportActionBar().setSplitBackgroundDrawable(new ColorDrawable(Color.parseColor("#33B5E5")));

Source: http://scriptedpapers.com/2014/09/25/android-implement-spilit-action-bar-change-its-background-color/

Upvotes: 1

thestrongenough
thestrongenough

Reputation: 225

This should work

ActionBar mActionBar = getActionBar(); 
mActionBar.setBackgroundDrawable(new ColorDrawable(0xff00DDED));
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayShowTitleEnabled(true);

Upvotes: 0

Related Questions