android tutorial - Adding attributes to views in android | Developer android - android app development - android studio - android app developement



Adding attributes to views in android

Custom views can also take custom attributes which can be used in Android layout resource files. To add attributes to your custom view you need to do the following:

1. Define the name and type of your attributes:

This is done inside res/values/attrs.xml (create it if necessary). The following file defines a color attribute for our smiley's face color and an enum attribute for the smiley's expression:

<resources>
    <declare-styleable name="SmileyView">
        <attr name="smileyColor" format="color" />
        <attr name="smileyExpression" format="enum">
            <enum name="happy" value="0"/>
            <enum name="sad" value="1"/>
        </attr>
    </declare-styleable>
    <!-- attributes for other views -->
</resources>
click below button to copy code from our android learning website - android tutorial - team

Use your attributes inside your layout:

This can be done inside any layout files that use your custom view. The following layout file creates a screen with a happy yellow smiley:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
    
    <com.example.app.SmileyView
        android:layout_height="56dp"
        android:layout_width="56dp"
        app:smileyColor="#ffff00"
        app:smileyExpression="happy" />
</FrameLayout>
click below button to copy code from our android learning website - android tutorial - team

Tip: Custom attributes do not work with the tools: prefix in Android Studio 2.1 and older (and possibly in future versions). In this example, replacing app:smileyColor with tools:smileyColor would result in smileyColor neither being set during runtime nor at design time.

3. Read your attributes:

This is done inside your custom view source code. The following snippet of SmileyView demonstrates how the attributes can be extracted:

public class SmileyView extends View {
    // ...

    public SmileyView(Context context) {
        this(context, null);
    }

    public SmileyView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SmileyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SmileyView, defStyleAttr, 0);
        mFaceColor = a.getColor(R.styleable.SmileyView_smileyColor, Color.TRANSPARENT);
        mFaceExpression = a.getInteger(R.styleable.SmileyView_smileyExpression, Expression.HAPPY);
        // Important: always recycle the TypedArray
        a.recycle();

        // initPaints(); ...
    }
}
click below button to copy code from our android learning website - android tutorial - team

4. (Optional) Add default style:

This is done by adding a style with the default values and loading it inside your custom view. The following default smiley style represents a happy yellow one:

<!-- styles.xml -->
<style name="DefaultSmileyStyle">
    <item name="smileyColor">#ffff00</item>
    <item name="smileyExpression">happy</item>
</style>
click below button to copy code from our android learning website - android tutorial - team

Which gets applied in our SmileyView by adding it as the last parameter of the call to obtainStyledAttributes (see code in step 3):

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SmileyView, defStyleAttr, R.style.DefaultSmileyViewStyle);
click below button to copy code from our android learning website - android tutorial - team

Note that any attribute values set in the inflated layout file (see code in step 2) will override the corresponding values of the default style.

5. (Optional) Provide styles inside themes:

Tthis is done by adding a new style reference attribute which can be used inside your themes and providing a style for that attribute. Here we simply name our reference attribute smileyStyle:

<!-- attrs.xml -->
<attr name="smileyStyle" format="reference" />
click below button to copy code from our android learning website - android tutorial - team

Which we then provide a style for in our app theme (here we just reuse the default style from step 4):

<!-- themes.xml -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="smileyStyle">@style/DefaultSmileyStyle</item>
</style>
click below button to copy code from our android learning website - android tutorial - team

Related Searches to Adding attributes to views in android