Saturday, 15 February 2014

android - Programmatically change Actionbar title color in Gingerbread -



android - Programmatically change Actionbar title color in Gingerbread -

i have requirement programmatically alter actionbar title text color.

i utilize next code set color in different ways, depending on sdk version

int textcolor = getresources().getcolor(android.r.color.white); if (build.version.sdk_int >= 14) { // version 4+ int titleid = getresources().getidentifier("action_bar_title", "id", "android"); textview abtitle = (textview) findviewbyid(titleid); if (abtitle != null) { abtitle.settextcolor(textcolor); } } else { // other versions textview abtitle = (textview) getwindow().findviewbyid(android.r.id.title); if (abtitle != null) { abtitle.settextcolor(textcolor); } }

it works fine on devices sdk value 14 or greater. but, gingerbread devices, abtitle null color doesn't set.

does anyony have suggestions?

your if/else wrong, should like

int titleid; if (build.version.sdk_int >= build.version_codes.honeycomb) { titleid = getresources().getidentifier("action_bar_title", "id", "android"); } else { titleid = r.id.action_bar_title; }

and should able straight through findviewbyid

android android-actionbar

No comments:

Post a Comment