ViewTitleIndicator playing nice with Themes

Build 70 incorporates ViewTitleIndicator by default now. The problem with the title indicators is that they don’t natively play nice with themes. Here’s how I fixed it:

TypedValue tvBarTabStyle = new TypedValue();
 int actionBarStyleResId;
 int backgroundResId;
 int actionBarTabTextStyleResId;
 int actionBarTabStyleResId;

if (VERSION.SDK_INT >= 14) {
 backgroundResId = V14.getBackgroundStackedResId();
 actionBarStyleResId = V11.getActionBarStyleResId();
 actionBarTabTextStyleResId = V11
 .getActionBarTabTextStyleResId();
 actionBarTabStyleResId = V11.getActionBarTabStyleResId();
 } else if (VERSION.SDK_INT >= 11) {
 actionBarStyleResId = V11.getActionBarStyleResId();
 backgroundResId = android.R.attr.background;
 actionBarTabTextStyleResId = V11
 .getActionBarTabTextStyleResId();
 actionBarTabStyleResId = V11.getActionBarTabStyleResId();
 } else {
 backgroundResId = com.actionbarsherlock.R.attr.backgroundStacked;
 actionBarStyleResId = com.actionbarsherlock.R.attr.actionBarStyle;
 actionBarTabTextStyleResId = com.actionbarsherlock.R.attr.actionBarTabTextStyle;
 actionBarTabStyleResId = com.actionbarsherlock.R.attr.actionBarTabStyle;
 }

getTheme().resolveAttribute(actionBarStyleResId, tvBarTabStyle,
 true);
 TypedArray taBackground = this.obtainStyledAttributes(
 tvBarTabStyle.resourceId, new int[] { backgroundResId });
 mIndicator.setBackgroundResource(taBackground.getResourceId(0, 0));
 taBackground.recycle();

TypedValue tvBarTabTextStyle = new TypedValue();
 getTheme().resolveAttribute(actionBarTabTextStyleResId,
 tvBarTabTextStyle, true);

TypedArray taTitleTextColor = this.obtainStyledAttributes(
 tvBarTabTextStyle.resourceId,
 new int[] { android.R.attr.textColor });
 mIndicator.setSelectedColor(taTitleTextColor.getColor(0, 0));
 mIndicator.setTextColor(taTitleTextColor.getColor(0, 0));
 taTitleTextColor.recycle();

TypedValue tvBarTabBarStyle = new TypedValue();
 getTheme().resolveAttribute(actionBarTabStyleResId,
 tvBarTabBarStyle, true);
 TypedArray taIndicator = this.obtainStyledAttributes(
 tvBarTabBarStyle.resourceId,
 new int[] { android.R.attr.background });
 TypedValue tvDivider = new TypedValue();
 taIndicator.getValue(0, tvDivider);
 int color = -1;
 StateListDrawable sldIndicator = (StateListDrawable) getResources()
 .getDrawable(tvDivider.resourceId);
 sldIndicator.setState(new int[] { android.R.attr.state_selected });
 Drawable drawable = sldIndicator.getCurrent();
 Bitmap bmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
 drawable.getIntrinsicHeight(), Config.ARGB_8888);
 Canvas canvas = new Canvas(bmp);
 drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
 drawable.draw(canvas);
 int height = bmp.getHeight();
 int width = bmp.getWidth();
 color = bmp.getPixel(width / 2, height / 2);
 mIndicator.setFooterColor(color);
 taIndicator.recycle();
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: