Android - custom UI with custom attributes

AndroidCustom Controls

Android Problem Overview


I know it is possible to create custom UI element (by way of View or specific UI element extension). But is it possible to define new properties or attributes to newly created UI elements (I mean not inherited, but brand new to define some specific behavior I am not able to handle with default propertis or attributes)

e.g. element my custom element:

<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?

Thx

Android Solutions


Solution 1 - Android

Yes. Short guide:

  1. Create an attribute XML

Create a new XML file inside /res/values/attrs.xml, with the attribute and it's type

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

Basically you have to set up one <declare-styleable /> for your view that contains all your custom attributes (here just one). I never found a full list of possible types, so you need to look at the source for one I guess. Types that I know are reference (to another resource), color, boolean, dimension, float, integer and string. They are pretty self-explanatory

  1. Use the attributes in your layout

That works the same way you did above, with one exception. Your custom attribute needs it's 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.

  1. Make use of the values you get passed

Modify the constructor of your custom view 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 lot's of other things to parse other types of values.

And that's it. Use the parsed value in your View to modify it, e.g. use it in onDraw() to change the look accordingly.

Solution 2 - Android

In your res/values folder create attr.xml. There you can define your attribues:

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

When you then want to use it in your layout file you have to add

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

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

Solution 3 - Android

Yes , you can.Just use <resource> tag.
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 official website

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWaypointView Question on Stackoverflow
Solution 1 - Androiduser658042View Answer on Stackoverflow
Solution 2 - AndroidMaria NeumayerView Answer on Stackoverflow
Solution 3 - AndroidFrank ChengView Answer on Stackoverflow