3

I made a library that i want to include in my project.

In that, i have this in my attr.xml:

 <declare-styleable name="MyCustomView">
    <attr name="MyPrivateID" format="string"/>
</declare-styleable>

I included the library in my app, and it works fine. But i cant retreive my custom attribute.

This is my activity_main.xml

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
...........>

<something.myown.MyCustomView
android:layout_width="match_parent"
android:layout_height="wrap_content"
custom:MyPrivateID="GjM4X9QfK2VT05BKlEyffkAKGLU=dhbdsljhbdDjTchvNTQwMg=="/>

And in MyCustomView.java, i have:

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView);
try {
    String string = a.getString(R.styleable.MyCustomView_MyPrivateID);
    return string;
} finally {
    a.recycle();
}

Somehow, string returns as null. Any help is appreciated.

1 Answer 1

3

Are you able to change?

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HalalAdView);

to

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView);

EDITED

Take a look in this sample. It's working https://github.com/betorcs/SampleCustomView

Sign up to request clarification or add additional context in comments.

4 Comments

Are you trying to get MyPrivateID value in constructor?
Yes? Would that be a problem?
I edited answer, i created a sample for you. Take a look and try figure out what's different.
Thank you. got it working. It was because i had moved it out of my constructor. Even though I made sure that the constructor was called first! it seems like a bug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.