Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically load R.styleable resource?

I'm trying to export the Facebook Android SDK as a JAR for use in my project.

This requires loading all the resources dynamically.

For example, I have to make changes similar to this:

//findViewById(R.id.com_facebook_login_activity_progress_bar).setVisibility(View.VISIBLE);
int viewID = getResources().getIdentifier("com_facebook_login_activity_progress_bar", "id", getPackageName());
findViewById(viewID).setVisibility(View.VISIBLE);

The commented line shows the original, and the 2 lines below show the change I made to load the same resource dynamically.

The Facebook SDK declares a R.styleable resource, and I can't figure out how to load it dynamically. Here's the original code:

private void parseAttributes(AttributeSet attrs) {
    TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.com_facebook_profile_picture_view);
    setPresetSize(a.getInt(R.styleable.com_facebook_profile_picture_view_preset_size, CUSTOM));
    isCropped = a.getBoolean(R.styleable.com_facebook_profile_picture_view_is_cropped, IS_CROPPED_DEFAULT_VALUE);
    a.recycle();
}

Then in attrs.xml, the following is declared:

    <declare-styleable name="com_facebook_profile_picture_view">
        <attr name="preset_size">
            <!-- Keep in sync with constants in ProfilePictureView -->
            <enum name="small" value="-2" />
            <enum name="normal" value="-3" />
            <enum name="large" value="-4" />
        </attr>
        <attr name="is_cropped" format="boolean" />
    </declare-styleable>

How can I load this resource dynamically, (e.g. replace the R.styleable reference) ?

like image 723
ch3rryc0ke Avatar asked Oct 22 '22 16:10

ch3rryc0ke


1 Answers

I'm answering the question here incase anyone is specifically trying to also export the Facebook SDK as a jar.

I used the function described in the answer to this question: Accessing <declare-styleable> resources programatically

private void parseAttributes(AttributeSet attrs) {
    int attrArray[] = StyleableHelper.getResourceDeclareStyleableIntArray(getContext(), "com_facebook_profile_picture_view");
    //TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.com_facebook_profile_picture_view);
    TypedArray a = getContext().obtainStyledAttributes(attrs, attrArray);

    setPresetSize(a.getInt(0, CUSTOM));
    isCropped = a.getBoolean(1, IS_CROPPED_DEFAULT_VALUE);
    //setPresetSize(a.getInt(R.styleable.com_facebook_profile_picture_view_preset_size, CUSTOM));
    //isCropped = a.getBoolean(R.styleable.com_facebook_profile_picture_view_is_cropped, IS_CROPPED_DEFAULT_VALUE);
    a.recycle();
}
like image 64
ch3rryc0ke Avatar answered Oct 24 '22 11:10

ch3rryc0ke