Abdullah Gheith
Abdullah Gheith

Reputation: 520

Custom attribute returning null

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.

Upvotes: 3

Views: 2366

Answers (1)

betorcs
betorcs

Reputation: 2831

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

Upvotes: 3

Related Questions