118

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

3

3 Answers 3

264

Yes. Short guide:

  1. Create an attribute XML

Create a new XML file inside /res/values/attrs.xml, with the attribute and its 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 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.

  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 has lots 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.

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

10 Comments

Just note about TypedArray. Be sure to call recycle() when done with them.
Does the IDE (eclipse for example) auto-complete the keys/values of custom attributes?
for gradle builds you should use http://schemas.android.com/apk/res-auto when declaring the custom namespace
In step 3, you can simply use String initialText = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "initialText"); where attr is the AttributeSet that is passed in the constructor and 'initialText' is your custom attribute name
|
22

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=""

1 Comment

this is not the answer, the question is how to programatically change from java
-14

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

1 Comment

Thanks for answer. But in resource, i see using default "android:" values. My point is, can I have e.g. android:phoneNameSelected="true" as a parameter of my custom UI element?

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.