I want to set an image resource according to custom attribute of my
custom theme.

This is my attrs.xml file

    <resources>
        <declare-styleable name="PlayPauseButton">
                <attr name="pauseImg" format="integer"/>
                <attr name="playImg" format="integer"/>
        </declare-styleable>
    </resources>

Here I set values for the attributes in my theme in themes.xml:

    <style name="MyTheme.Blue">
        <item name="playImg">@drawable/play_button</item>
        <item name="pauseImg">@drawable/pause_button</item>
    </style>

The attribute has type integer, but when I try to get it in code, it
seems to be string:

    TypedArray a =
getActivity().getTheme().obtainStyledAttributes(R.styleable.PlayPauseButton);
    String path =
a.getString(R.styleable.PlayPauseButton_pauseImg); // returns "res/
drawable/pause_button.xml"
    int id = a.getInteger(R.styleable.PlayPauseButton_pauseImg,0); //
"Cannot convert to integer" exception

Now I cannot get the drawable using this path. I tried:

    int i = getResources().getIdentifier(path, "integer",
getActivity().getPackageName());
    Drawable d = Drawable.createFromPath(path);

but both creates null objects.

My questions:
 1. Is there any way how to create a drawable from the path "res/
drawable/pause_button.xml" so I can pass it to
playButton.setImageResource() or playButton.setImageDrawable().
 2. Overall I think this is not the best way how to do it. The
attribute pauseImg has integer type. Why I get string? Is there any
way how to get the attribute value as resource id integer, ie.
identicaly to R.drawable.pause_button ?

Thank you very much for any help with this or for any ather way how to
do it. I am really stucked.

-- 
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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to