Skip to content

Commit

Permalink
Retrieve ActionBarItem default colors from theme attributes
Browse files Browse the repository at this point in the history
Closes #38
  • Loading branch information
Cyril Mottier committed Jun 2, 2011
1 parent 3db4794 commit b64b0b5
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected void onCreate(Bundle savedInstanceState) {

addActionBarItem(getActionBar()
.newActionBarItem(NormalActionBarItem.class)
.setDrawable(new ActionBarDrawable(getResources(), R.drawable.ic_action_bar_info)), R.id.action_bar_view_info);
.setDrawable(new ActionBarDrawable(this, R.drawable.ic_action_bar_info)), R.id.action_bar_view_info);
}

private TextItem createTextItem(int stringId, Class<?> klass) {
Expand Down
2 changes: 2 additions & 0 deletions GreenDroid/res/values/gd_attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
<attr name="gdActionBarDividerWidth" format="dimension" />
<attr name="gdActionBarApplicationDrawable" format="reference" />
<attr name="gdActionBarHomeDrawable" format="reference" />
<attr name="gdActionBarItemColorNormal" format="reference|color" />
<attr name="gdActionBarItemColorAlt" format="reference|color" />

<attr name="gdActionBarStyle" format="reference" />
<attr name="gdActionBarTitleStyle" format="reference" />
Expand Down
2 changes: 2 additions & 0 deletions GreenDroid/res/values/gd_themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
<item name="gdActionBarDividerWidth">1px</item>
<item name="gdActionBarApplicationDrawable">@null</item>
<item name="gdActionBarHomeDrawable">@null</item>
<item name="gdActionBarItemColorNormal">@android:color/white</item>
<item name="gdActionBarItemColorAlt">@android:color/black</item>

<item name="gdActionBarStyle">@style/GreenDroid.Widget.ActionBar</item>
<item name="gdActionBarTitleStyle">@style/GreenDroid.Widget.ActionBar.Title</item>
Expand Down
52 changes: 49 additions & 3 deletions GreenDroid/src/greendroid/graphics/drawable/ActionBarDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@
package greendroid.graphics.drawable;

import greendroid.widget.ActionBar;
import greendroid.widget.ActionBarItem;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.StateSet;
import android.util.TypedValue;

import com.cyrilmottier.android.greendroid.R;

/**
* A specialized {@link Drawable} that is dedicated to {@link ActionBarItem}s.
Expand All @@ -36,27 +42,67 @@
*/
public class ActionBarDrawable extends BitmapDrawable {

private static final TypedValue sTypedValue = new TypedValue();

private ColorFilter mNormalCf;
private ColorFilter mAltCf;


@Deprecated
public ActionBarDrawable(Resources res, int resId) {
this(res, res.getDrawable(resId), Color.WHITE, Color.BLACK);
this(res, res.getDrawable(resId));
}


@Deprecated
public ActionBarDrawable(Resources res, Drawable d) {
this(res, d, Color.WHITE, Color.BLACK);
}

@Deprecated
public ActionBarDrawable(Resources res, int resId, int normalColor, int altColor) {
this(res, res.getDrawable(resId), normalColor, altColor);
}

@Deprecated
public ActionBarDrawable(Resources res, Drawable d, int normalColor, int altColor) {
super(res, (d instanceof BitmapDrawable) ? ((BitmapDrawable) d).getBitmap() : null);
mNormalCf = new LightingColorFilter(Color.BLACK, normalColor);
mAltCf = new LightingColorFilter(Color.BLACK, altColor);
}

public ActionBarDrawable(Context context, int resId) {
this(context, context.getResources().getDrawable(resId));
}

public ActionBarDrawable(Context context, Drawable d) {
this(context, d, getColorFromTheme(context, R.attr.gdActionBarItemColorNormal, Color.WHITE),
getColorFromTheme(context, R.attr.gdActionBarItemColorAlt, Color.BLACK));
}

public ActionBarDrawable(Context context, int resId, int normalColor, int altColor) {
this(context, context.getResources().getDrawable(resId), normalColor, altColor);
}

public ActionBarDrawable(Context context, Drawable d, int normalColor, int altColor) {
super(context.getResources(), (d instanceof BitmapDrawable) ? ((BitmapDrawable) d).getBitmap() : null);
mNormalCf = new LightingColorFilter(Color.BLACK, normalColor);
mAltCf = new LightingColorFilter(Color.BLACK, altColor);
}

private static int getColorFromTheme(Context context, int attr, int defaultColor) {
synchronized (sTypedValue) {
final TypedValue value = sTypedValue;
final Theme theme = context.getTheme();
if (theme != null) {
theme.resolveAttribute(attr, value, true);
if (value.type >= TypedValue.TYPE_FIRST_INT && value.type <= TypedValue.TYPE_LAST_INT) {
return value.data;
}
}

return defaultColor;
}
}

@Override
public boolean isStateful() {
return true;
Expand Down
2 changes: 1 addition & 1 deletion GreenDroid/src/greendroid/widget/ActionBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public ActionBar(Context context, AttributeSet attrs, int defStyle) {
mDividerWidth = a.getDimensionPixelSize(R.styleable.ActionBar_dividerWidth, -1);
mHomeDrawable = a.getDrawable(R.styleable.ActionBar_homeDrawable);
if (mHomeDrawable == null) {
mHomeDrawable = new ActionBarDrawable(getResources(), R.drawable.gd_action_bar_home);
mHomeDrawable = new ActionBarDrawable(context, R.drawable.gd_action_bar_home);
}

int layoutID;
Expand Down
4 changes: 2 additions & 2 deletions GreenDroid/src/greendroid/widget/ActionBarItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ static ActionBarItem createWithType(ActionBar actionBar, ActionBarItem.Type type

case Refresh:
return actionBar.newActionBarItem(LoaderActionBarItem.class)
.setDrawable(new ActionBarDrawable(actionBar.getResources(), R.drawable.gd_action_bar_refresh))
.setDrawable(new ActionBarDrawable(actionBar.getContext(), R.drawable.gd_action_bar_refresh))
.setContentDescription(R.string.gd_refresh);

case TakePhoto:
Expand Down Expand Up @@ -288,7 +288,7 @@ static ActionBarItem createWithType(ActionBar actionBar, ActionBarItem.Type type
return null;
}

final Drawable d = new ActionBarDrawable(actionBar.getResources(), drawableId);
final Drawable d = new ActionBarDrawable(actionBar.getContext(), drawableId);

return actionBar.newActionBarItem(NormalActionBarItem.class).setDrawable(d)
.setContentDescription(descriptionId);
Expand Down

0 comments on commit b64b0b5

Please sign in to comment.