How to read custom attributes in Android
My question: can I read the value of those custom attributes without creating a class that extends EditText?
Yes, you can get those attributes without extending the classes. For this you could use a special Factory
set on the LayoutInflater
that the Activity
will use to parse the layout files. Something like this:
super.onCreate(savedInstanceState);
getLayoutInflater().setFactory(new CustomAttrFactory());
setContentView(R.layout.the_layout);
where the CustomAttrFactory
is like this:
public static class CustomAttrFactory implements Factory {
@Override
public View onCreateView(String name, Context context,
AttributeSet attrs) {
String attributeValue = attrs
.getAttributeValue(
"http://schemas.android.com/apk/res/com.luksprog.droidproj1",
"attrnew");
Log.e("ZXX", "" + attributeValue);
// if attributeValue is non null then you know the attribute is
// present on this view(you can use the name to identify the view,
// or its id attribute)
return null;
}
}
The idea comes from a blog post, you may want to read it for additional information.
Also, depending on that custom attribute(or attributes if you have other) you could just use android:tag="whatever"
to pass the additional data(and later retrieve it in the Activity
with view.getTag()
).
I would advise you to not use those custom attributes and rethink your current approach.