Android - user interface with custom attributes

I know that it is possible to create a user interface UI element (via View or a specific extension of the UI element). But is it possible to define new properties or attributes for newly created user interface elements (I mean not inherited, but completely new, to define some specific behavior that I cannot deal with by default propertytis or attributes)

eg. item is my custom item:

<com.tryout.myCustomElement android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Element..." android:myCustomValue=<someValue> /> 

So, is it possible to define MyCustomValue ?

thank

+85
android custom-controls
Sep 30 '11 at 9:30
source share
3 answers

Yes. Quick guide:

1. Create an XML attribute

Create a new XML file inside /res/values/attrs.xml , with the attribute and enter

 <?xml version="1.0" encoding="UTF-8"?> <resources> <declare-styleable name="MyCustomElement"> <attr name="distanceExample" format="dimension"/> </declare-styleable> </resources> 

Basically, you need to configure one <declare-styleable /> for your view, which contains all your user attributes (there is only one here). I did not find a complete list of possible types, so you need to look at the source that I assume. The types that I know are reference (to another resource), color, boolean, dimensional, float, integer and string. They are pretty clear.

2. Use attributes in the layout

This works the same as you did above, with one exception. Your own attribute needs its own XML namespace.

 <com.example.yourpackage.MyCustomElement xmlns:customNS="http://schemas.android.com/apk/res/com.example.yourpackage" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Element..." customNS:distanceExample="12dp" /> 

Pretty straight forward.

3. Use the obtained values

Modify the custom view constructor to parse the values.

 public MyCustomElement(Context context, AttributeSet attrs) { super(context, attrs); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyCustomElement, 0, 0); try { distanceExample = ta.getDimension(R.styleable.MyCustomElement_distanceExample, 100.0f); } finally { ta.recycle(); } // ... } 

distanceExample is a private member variable in this example. TypedArray got a ton of other things to analyze other types of values.

What is it. Use the syntax value in the View to change it, for example. use it in onDraw() to change the look accordingly.

+203
Sep 30 '11 at 9:57
source share

In the res / values ​​folder, create attr.xml. There you can define your attributes:

 <declare-styleable name=""> <attr name="myCustomValue" format="integer/boolean/whatever" /> </declare-styleable> 

If you want to use it in your layout file, you must add

 xmlns:customname="http://schemas.android.com/apk/res/your.package.name" 

and then you can use the value with customname:myCustomValue=""

+14
Sep 30 2018-11-11T00:
source share

Yes, you can. Just use the <resource> .
like this:

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="CodeFont" parent="@android:style/TextAppearance.Medium"> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:textColor">#00FF00</item> <item name="android:typeface">monospace</item> </style> </resources> 

link from the official site

-7
Sep 30 2018-11-11T00: 00Z
source share



All Articles