4

Android's AttributeSet.getAttributeIntValue takes String namespace as input. Hard coding the namespace seems like a bad idea, because if the namespace changes, then a runtime bug is introduced. More concretely, it would be nice to avoid

attrs.getAttributeIntValue("http://schemas.android.com/apk/res-auto", 
                                                             "myAttribute", 0)

since it hard codes string "http://schemas.android.com/apk/res-auto", which seems unnecessary. (Admittedly, it hard codes "myAttribute", but that seems unavoidable.) Is it possible to read the integer attribute "myAttribute" without hard coding the name space? (Possibly by naming the xml file that contains "myAttribute".) Such that compilation errors are generated if anything goes wrong. (E.g., the file no longer exists.) Alternatively, is it possible to produce a compiler error if "myAttribute" cannot be found (this would eliminate the need for a default value too, since it would never be used).

Context. I have extended a LinearLayout to override constructors LinearLayout(Context context, AttributeSet attrs) and LinearLayout(Context context, AttributeSet attrs, int defStyleAttr). These constructors use AttributeSet.getAttributeIntValue to read attributes from attrs. Thus, code and GUI are completely separate. However, if the namespace is changed in the GUI, then problems arise when the namespace is hardcoded in the code. (This actually happened. So, I'm trying to figure out how to avoid it happening again!)

5
  • what exactly are you trying to do ? seems like you are using AttributeSet where you should use TypedValue Commented Nov 24, 2015 at 13:53
  • I've added some context to the question. I don't see how TypedValue is relevant to me... Commented Nov 24, 2015 at 14:14
  • if you need to read something from the attributes, TypedArray is the way to go, you shouldn't have to use AttributeSet directly Commented Nov 24, 2015 at 15:56
  • Given the AttributeSet attrs taken as input by the constructor, how can I recover a particular integer attribute using TypedValue or TypedArray? At present, I simply use attrs.getAttributeIntValue("http://schemas.android.com/apk/res-auto", "myAttribute", 0), where the first parameter is the name space, the second is the name of the attribute, and the third is the default. (This is undesirable, as per my question, because I hard code the namespace.) Commented Nov 24, 2015 at 16:37
  • Just to be sure before I do a complete answer, you have a custom attribute and you want to use it in your layout, that's it ? Commented Nov 24, 2015 at 16:45

1 Answer 1

1

If you need a custom attribute for your view and want to use it in your code, you can follow the documentation about custom views. Basically, you declare your attributes in styleable, and then use obtainResources to get them.

Sign up to request clarification or add additional context in comments.

1 Comment

I just want the namespace. E.g., I want to avoid attrs.getAttributeIntValue("http://schemas.android.com/apk/res-auto", "myAttribute", 0), since I hardcode "http://schemas.android.com/apk/res-auto". In an alternate direction, I'd be happy with a compiler error if "myAttribute" cannot be found.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.