Android Read Custom View properties
Attrs.xml:
<?XML version= "1.0" encoding= "Utf-8"?><Resources> <declare-styleablename= "MyView"> <attrname= "Myviewcolor"format= "Color"/> </declare-styleable> </Resources>
Activity_main.xml:
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:fab= "Http://schemas.android.com/apk/res-auto"Android:background= "@color/background"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"> <Com.my.MyViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Fab:myviewcolor= "@color/pink"Android:layout_alignparentbottom= "true"Android:layout_centerhorizontal= "true"Android:layout_marginbottom= "16DP"/></Relativelayout>
Myview.java:
PublicMyView (Context context, AttributeSet attrs) {Super(context, attrs, Defstyle); if(attrs!=NULL) {TypedArray attr= Context.obtainstyledattributes (attrs, R.styleable.myview, 0, 0); if(attr! =NULL) { Try{Mmyviewcolor=Attr.getcolor (R.styleable.myviewcolor, GetColor (Android). R.color.white)); } finally{attr.recycle (); } } }
Android Read Custom View properties