I have a custom class, extending android.support.v7.widget.AppCompatImageView
where I would like to catch a custom string value from XML during its construction.
I followed this top answer, everything compile like a charms however when I try to acces my custom value, I get Null.
Here is my custom class
public class CustomImageView extends android.support.v7.widget.AppCompatImageView {
private Context context = null;
private AttributeSet attrs = null;
public CustomImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.context = context;
this.attrs = attrs;
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomImageView);
String e = ta.getString(R.styleable.CustomImageView_customUrl);
System.out.print(e) // e is null
ta.recycle();
}
}
here is my activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.custom.customView.customImageView
android:id="@+id/image"
android:src="@mipmap/ic_launcher_round"
android:alpha="1"
app:customUrl="http://google.fr"/>
...
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomImageView">
<attr name="customUrl" format="string" />
</declare-styleable>
</resources>
I understand there might be plenty of answer about similar or pseudo similar cases however, I couldn't manage to find any answer regarding getting null from getString
Edit : fix wrong link