0

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

Keuha
  • 275
  • 3
  • 18
  • Remove your `custom` namespace definition in the layout, and just use the `app` prefix on your `customUrl` attribute. – Mike M. Mar 20 '18 at 16:05
  • Thanks mike, I just did it, it does not change anything. – Keuha Mar 20 '18 at 16:07
  • You'll likely need to clean/rebuild your project. – Mike M. Mar 20 '18 at 16:08
  • so I just did it, and it seems there is still no changes so far. – Keuha Mar 20 '18 at 16:14
  • What did you change in the layout, exactly? You kept the namespace that ends with `res-auto`, yes? – Mike M. Mar 20 '18 at 16:17
  • I just edit/update the xml according to your suggestions. please watch the edited question – Keuha Mar 20 '18 at 16:19
  • Yeah, that's right. I'd imagine you're just missing the `System.out.print()` in your logs. Try something more noticeable, or put a breakpoint there, and inspect the values. – Mike M. Mar 20 '18 at 16:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167199/discussion-between-keuha-and-mike-m). – Keuha Mar 20 '18 at 16:24
  • Just saw your message in chat. No, I don't use `ConstraintLayout`, but it really shouldn't do that. If it does, and you can reliably reproduce it, it's a bug. I'll run some tests later on, when I get a chance, and let you know if I find out anything of interest. – Mike M. Mar 20 '18 at 18:08

0 Answers0