androidandroid-layoutandroid-viewandroid-custom-viewandroid-attributes

android:src attrs for custom view


Is there a way to set default android attrs to the custom view, that would be available when defining this view in xml?. For example, I need to create CustomImageView that will be FrameLayout with ImageView and ProgressBar inside it. If I add such view in xml I want to set background or src of that view, for example. FrameLayout has no android:src attribute and it doesn't offer via Android Studio autocomplete window, can I make somehow Android Studio to know that this layout can process such attributes?


Solution

  • The trick is to put android:src in custom styleables, located at values/attrs.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="MyView">
            <attr name="android:src"/>
        </declare-styleable>
    </resources>
    

    And having MyView class that is a descendant of a View, then in xml file we'd see this:

    enter image description here