1

I have following custom attributes:

    <attr name="myText" format="string" />
    <attr name="myRadius" format="float" />

and i want to get this attributes in class, so i have following code in view class:

 int[] attrsArray = new int[] {
            R.attr.myText,
            R.attr.myRadius
    };

    TypedArray ta = context.obtainStyledAttributes(attrs, attrsArray, 0, 0);
    try {
       String text = ta.getString(0);
       float radius = ta.getFloat(1,0);

    }finally{ta.recycle();}

myText attribute is ok, i received value of myText but radisu attribute is allways 0.0; why does some custom attributes not working?

  • Where are you specifying the value for the attribute? Presumably there's some layout file that includes `app:myRadius="[something]"`; can you add that to the question? – Ben P. Jul 06 '20 at 20:44
  • @BenP. i tried different names.. clean and rebuild project, restarted IDE, restarted PC but still not working – Giorgi Abashidze Jul 06 '20 at 21:17
  • The attribute array you pass to `obtainStyledAttributes()` needs to be in ascending order. If it's not, you can get partial results like that. Try doing `Arrays.sort(attrsArray);` before that call. – Mike M. Jul 06 '20 at 21:25
  • @MikeM. yes ! order of the attrsArray items was the problem. Thanks. how can I mark your comment as solved – Giorgi Abashidze Jul 06 '20 at 21:48
  • No problem. It's actually a rather common issue, so I'll just mark this as a duplicate, rather than repeat an answer again. Thank you, though. I appreciate the offer. Glad you got it working. Cheers! – Mike M. Jul 06 '20 at 21:58

0 Answers0