context.obtainStyledAttributes

492 views
Skip to first unread message

Macarse

unread,
Dec 12, 2010, 9:32:16 AM12/12/10
to robol...@googlegroups.com
Hi,

Does anybody plan to do this Shadow?

As explained here: https://github.com/pivotal/robolectric/issues/closed#issue/15
I have added https://github.com/cyrilmottier/GreenDroid 's ActionBar
to all my Activities but as Robolectric doesn't have a shadow for
obtainStyledAttributes all my test that use an Activity are failing :(

It's the first time I read about obtainStyledAttributes and I am still
learning how the shadow should behave.

Any thoughts or ideas of how this should be implemented?

Thanks.

Macarse

unread,
Dec 13, 2010, 8:50:49 PM12/13/10
to robol...@googlegroups.com
My app uses https://github.com/cyrilmottier/GreenDroid 's ActionBar so
I am using that as example.

Robolectric fails because a == null after:

TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.ActionBar, defStyle, 0);

Here's a cool link with a good explanation about custom widgets:
http://androidtutorials.org/android-tutorial-4-2-passing-custom-attributes-via-xml-resource-files


What I have understood so far:

This are the attr for the action bar:

<!-- ActionBar related attributes -->
<declare-styleable name="ActionBar">
<attr name="title" format="string" />
<attr name="type">
<enum name="normal" value="0" />
<enum name="dashboard" value="1" />
</attr>
<attr name="dividerDrawable" />
<attr name="dividerWidth" />
<attr name="homeDrawable" format="reference" />
</declare-styleable>


I just debugged the code.

attrs is a HashMap with:

key: android:layout_height
value: @dimen/gd_action_bar_height

key: android:id
value: @id/gd_action_bar

key: android:background
value: ?attr/gdActionBarBackground

key: android:layout_width
value: fill_parent

The attrs are the attrs defined in the xml of the layout.
<greendroid.widget.ActionBar
android:id="@id/gd_action_bar"
android:layout_height="@dimen/gd_action_bar_height"
android:layout_width="fill_parent"
android:background="?attr/gdActionBarBackground" />


In the code cyrilmottier does this:

TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.ActionBar, defStyle, 0);

mTitle = a.getString(R.styleable.ActionBar_title);
mDividerDrawable = a.getDrawable(R.styleable.ActionBar_dividerDrawable);
mDividerWidth =
a.getDimensionPixelSize(R.styleable.ActionBar_dividerWidth, -1);
mHomeDrawable = a.getDrawable(R.styleable.ActionBar_homeDrawable);
int type = a.getInteger(R.styleable.ActionBar_type, -1);


As far as I understand if this values are not found in the attrs
HashMap they will be gotten from the styles:

<!-- ActionBar related styles -->
<style name="GreenDroid.Widget.ActionBar">
<item name="android:background">?attr/gdActionBarBackground</item>
<item name="android:layout_height">@dimen/gd_action_bar_height</item>
<item name="android:layout_width">fill_parent</item>
<item name="dividerDrawable">?attr/gdActionBarDividerDrawable</item>
<item name="dividerWidth">?attr/gdActionBarDividerWidth</item>
<item name="homeDrawable">?attr/gdActionBarHomeDrawable</item>
</style>

every value with a ? is gotten from a theme:

<item name="gdActionBarTitleColor">@android:color/white</item>
<item name="gdActionBarBackground">@drawable/header_blank</item>
<item name="gdActionBarItemBackground">@drawable/gd_action_bar_item</item>
<item name="gdActionBarDividerDrawable">@color/gd_action_bar_divider_tint</item>
<item name="gdActionBarDividerWidth">1px</item>
<item name="gdActionBarApplicationDrawable">@null</item>
<item name="gdActionBarHomeDrawable">@drawable/gd_action_bar_home</item>


The first thing that went throughout my mind:
If I understand this correctly. Why is he using:
android:background="?attr/gdActionBarBackground"
in the layout?
That should get him the default. Wouldn't be the same to remove it?

No. Because it's not a custom attr. so he uses the ?attr/ thing.

Reply all
Reply to author
Forward
0 new messages