Skip to content

Instantly share code, notes, and snippets.

@CarlosMChica
Last active August 29, 2015 14:06
Show Gist options
  • Save CarlosMChica/9076175fae4f01b1dab5 to your computer and use it in GitHub Desktop.
Save CarlosMChica/9076175fae4f01b1dab5 to your computer and use it in GitHub Desktop.
Workaround to change action bar background dynamically
//Looks like drawable is still not ready to be set when applying it to the action bar
//in versions bellow 17 with default method. The action bar needs to be redrawed and it can be forced by using
//the following workarround
public void setActionBarbackground(ColorDrawable colorDrawable) {
ActionBar actionBar = getActionBar();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
actionBar.setBackgroundDrawable(colorDrawable);
//switch true/false values if title is not needed
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowTitleEnabled(true);
} else {
getActionBar().setBackgroundDrawable(colorDrawable);
}
}
@antoniolg
Copy link

You won't need to call getActionBar() again in else condition, just use your variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment