Defining custom attrs

android custom view attributes programmatically
android attrs
android custom attributes drawable
android create custom view with xml layout
declare-styleable enum
android custom view example
android view example

I need to implement my own attributes like in

Found nothing in official documentation so I need information about how to define these attrs and how to use them from my code.

Creating a View Class, 1) Define custom to your view in a <declarable-styleable> resource element. 2) Specify values for the attributes in your XML layout. 3) Retrieve attribute values at​  To make a custom view, we’ll usually want to add attributes so we could customize the view properties from the XML. A good reference about it surprisingly found below Defining custom attrs

Qberticus's answer is good, but one useful detail is missing. If you are implementing these in a library replace:




Otherwise the application that uses the library will have runtime errors.

How To Make Add Custom Attributes To View In Android, Now, to begin adding custom attributes to your custom views, you have to first add a new file your “values” directory and name it “attrs.xml”. Define Custom Attributes To add a built-in View to your user interface, you specify it in an XML element and control its appearance and behavior with element attributes. Well-written custom views can also be added and styled via XML. To enable this behavior in your custom view, you must:

Android Custom Views Tutorial. Part-2: Custom Attributes, How do you define you Custom View attributes? From my search, most people declared them in such that could potential cause conflict as  Visual C# .NET What are Attributes? Here is a quote about what an attribute is all about: "Attributes provide a powerful method of associating declarative information with C# code (types, methods, properties, and so forth).

The traditional approach is full of boilerplate code and clumsy resource handling. That's why I made the Spyglass framework. To demonstrate how it works, here's an example showing how to make a custom view that displays a String title.

Step 1: Create a custom view class.

public class CustomView extends FrameLayout {
    private TextView titleView;

    public CustomView(Context context) {
        init(null, 0, 0);

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0, 0);

    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs, defStyleAttr, 0);

    public CustomView(
            Context context, 
            AttributeSet attrs,
            int defStyleAttr,
            int defStyleRes) {

        super(context, attrs, defStyleAttr, defStyleRes);
        init(attrs, defStyleAttr, defStyleRes);

    public void setTitle(String title) {

    private void init(AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        inflate(getContext(), R.layout.custom_view, this);

        titleView = findViewById(;

Step 2: Define a string attribute in the values/attrs.xml resource file:

    <declare-styleable name="CustomView">
        <attr name="title" format="string"/>

Step 3: Apply the @StringHandler annotation to the setTitle method to tell the Spyglass framework to route the attribute value to this method when the view is inflated.

@HandlesString(attributeId = R.styleable.CustomView_title)
public void setTitle(String title) {

Now that your class has a Spyglass annotation, the Spyglass framework will detect it at compile-time and automatically generate the CustomView_SpyglassCompanion class.

Step 4: Use the generated class in the custom view's init method:

private void init(AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    inflate(getContext(), R.layout.custom_view, this);

    titleView = findViewById(;


That's it. Now when you instantiate the class from XML, the Spyglass companion interprets the attributes and makes the required method call. For example, if we inflate the following layout then setTitle will be called with "Hello, World!" as the argument.


        app:title="Hello, World!"/>

The framework isn't limited to string resources has lots of different annotations for handling other resource types. It also has annotations for defining default values and for passing in placeholder values if your methods have multiple parameters.

Have a look at the Github repo for more information and examples.

Better way of declaring Custom View Attributes - Elye, You can create your own custom attributes by defining an attribute class, a class that You might define a custom Author attribute class: C# Creating custom views is centered around five primary aspects that we may need to control or modify: Drawing - Control the rendering of the view on screen visually by overriding the onDraw method. Interaction - Control the ways the user can interact with the view with the onTouchEvent and gestures.

Creating Custom Attributes (C#), Create an XML res/values/attrs.xml file to define new attributes alongwith their data type. 3, Create src/ file and add the code to define your custom  How to Create Custom View Component on Android Step I: Create View on XML First, we’re going to need a layout XML file setup for our search view. Let’s create a new Step II: Define Custom Attributes To add a built-in View to your user interface, you specify it in an XML element and Step III:

Android Custom Component with Custom Attributes, define custom attributes. handle custom attributes in a custom component. use custom attributes in XML layouts. We've done lots of UI building using  Defining custom layout managers You can implement your custom layout manager by extending the ViewGroup class. This allows you to implement more efficient layout managers or to implement effects which are currently missing in the Android platform.

Custom View Components, CustomView android:layout_width="wrap_content" android:layout_height="​wrap_content" /> If you want to pass custom attributes, first define the XML  When custom view is declared in attrs.xml, special constants for attribute indexes are generated. And we can extract values this way: a.getString(R.styleable.MyCustomView_android_text) . But for manual int[] there are no constants.

Android: Custom views and attributes, Defining custom attributes in Attrs.xml for the FlowLayout Listing 3-2 shows the attrs.xml for our custom FlowLayout. This file needs to be in the “/res/values”  The attributes for the custom layout are defined in the attrs.xml file. Each attribute is assigned a name and a format which can be int, float, double, string etc. Finally call the methods of the custom layout from the desired activity/fragment

  • These docs may be newer that your post, but in order to keep this current, you can find good, official documentation for attributes here :…
  • I recommend nice article with an example about the custom attributes:
  • a small working example may be helpful:
  • Here is a sample project demonstrating custom attributes for use with a custom View:…
  • If you are using custom attrs from a library project: see this question:… - It seems to work if you use xmlns:my="" - no copying attrs.xml. Note the namespace URI path must be /apk/*lib* not /apk/res.
  • @ThomNichols the apk/lib trick didn't work for me on custom attributes with reference format from a library project. What did work was to use apk/res-auto, as suggested in just below and also in
  • Quoting @Qberticus: "flag attributes are similar except the values need to be defined so they can be bit ored together". In my opinion this is kind of understating the main difference between enum and flag: the former lets us pick one and only one value, the latter lets us combine several. I wrote up a longer answer in a similar question here, and having now found this question I figured I'd link to that.
  • a.recycle() is very important here to free up memory
  • This was only recently added... I think within a few weeks ago. Certainly it was added long after Qberticus wrote his answer.
  • I think it's older than that, but it certainly was added long after Qberticus wrote his answer. Not faulting him at all, just adding a useful detail.
  • I've updated Qbericus's answer to use apk/res-auto to save confusion.
  • You can achieve the same with Google Data Binding - if there is no attribute binding for specific attribute, GDB tries to find set* method and uses it instead. In this case you'd have to wrote, say android:title="@{&quot;Hello, world!&quot;}".