0

I am trying to make this work

<mypackage.Square
         android:id="@+id/imageButton8"
         android:contentDescription="@string/r8"
         android:layout_height="50dp"
         android:layout_width="47dp"
         stringRandom="lalala"/>

where stringRandom is a custom variable from the java class Square located at src/mypackage

this is the java class Square:

public class Square extends ImageButton {
private String stringRandom;

public Square(Context context, boolean iswhite) {
    super(context);
}

public Square(Context context, boolean iswhite, String piece) {
    super(context);
}

public Square(Context context, AttributeSet attrs)
{
    super(context, attrs);
}   
}

I found an example here where they make work exactly the same as I am trying to, but I can not figure out why in my case it is not working. However I also found out that if instead of the right variable with the sample given you type anything else that should crash, it doesn't crash but it reports a java.null exception when getting the original variable.

Any clue?

Ssr1369
  • 328
  • 3
  • 16
  • Your code doesn't do any of the `attrs.getAttributeValue(null, "name");` stuff that the example you linked to is doing. Even if it did, it's not the best way to solve the problem. What you want to do is follow the advice in this answer: http://stackoverflow.com/a/2695649/49489 – Barend Aug 26 '14 at 18:09
  • @Barend But the attrs.getAttributeValue(null, "name"); stuff is for getting the variable's value, not for setting it. Why do you say it is not the best way to solve this? It's not recommended in any way? This solution is far more complex than what I want to do, it requieres an attr.xml working as intermediate. I simply want to use a class that extends ImageButton and initialize an attribute by using the class on my xml layout file, it can not be done in one single step? – Ssr1369 Aug 26 '14 at 18:22

0 Answers0