Monday, May 10, 2010

[android-developers] Namespaces and custom widgets

As a continuation of the thread in
http://groups.google.com/group/android-developers/browse_thread/thread/390e8188cedd66a6,
I'm still trying to figure out how a custom widget can use
getIdentifier() to read attributes.

As an example, I have a widget class called CustomWidget. It has
attributes "inner_margin" and "outer_margin". Normally, to read these
attributes, I would do the following in source code:

import com.example.customactivity.R; // I don't want to do this.

...

TypedArray a =
ctx.obtainStyledAttributes(attrs, R.styleable.CustomWidget);

innerMargin = a.getInt(R.styleable.CustomWidget_inner_margin, 0);


Because my CustomWidget is intended to be shared among a number of
projects, or even get published, it's a real problem having to import
R from whatever activity it's going to be linked with.

What I'd rather do is:

int resid = ctx.getResources().getIdentifier("inner_margin", "int",
"com.example.customwidget");
innerMargin = a.getInt(resid, 0);

but getidentifier always returns zero.

Can anybody tell me the magic I need to perform to do what I want?

For completeness, my CustomWidget.xml file that defines the resources
looks like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomWidget">
<attr name="outer_margin" format="integer" />
<attr name="inner_margin" format="integer" />
</declare-styleable>
</resources>

and for even more completeness, the entire source code can be found at
http://www.efalk.org/tmp/CustomWidget.tar.gz

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment