user4881271
user4881271

Reputation:

Android attributes,declare-styleable,reference

I am newbie in android development, I am trying to create my custom view right now. I have faced with a lot of problems. I have solved some of them, but the most difficult to understand for is attributes. Let's take sample xml file with attributes

<declare-styleable name="ViewPagerIndicator">
        <!-- Style of the circle indicator. -->
        <attr name="vpiCirclePageIndicatorStyle" format="reference"/>
        <!-- Style of the icon indicator's views. -->
        <attr name="vpiIconPageIndicatorStyle" format="reference"/>
        <!-- Style of the line indicator. -->
        <attr name="vpiLinePageIndicatorStyle" format="reference"/>
        <!-- Style of the title indicator. -->
        <attr name="vpiTitlePageIndicatorStyle" format="reference"/>
        <!-- Style of the tab indicator's tabs. -->
        <attr name="vpiTabPageIndicatorStyle" format="reference"/>
        <!-- Style of the underline indicator. -->
        <attr name="vpiUnderlinePageIndicatorStyle" format="reference"/>
    </declare-styleable>
    <attr name="centered" format="boolean" />
    <attr name="selectedColor" format="color" />
    <attr name="strokeWidth" format="dimension" />
    <attr name="unselectedColor" format="color" />

    <declare-styleable name="CirclePageIndicator">
        <attr name="centered" />
        <attr name="fillColor" format="color" />
        <attr name="pageColor" format="color" />
        <attr name="android:orientation"/>
        <attr name="radius" format="dimension" />
        <attr name="snap" format="boolean" />
        <attr name="strokeColor" format="color" />
        <attr name="strokeWidth" />
        <attr name="android:background"/>
    </declare-styleable>
<declare-styleable name="LinePageIndicator">
    <attr name="centered" />
    <attr name="unselectedColor" />
    <attr name="selectedColor" />
    <attr name="lineWidth" format="dimension" />
    <attr name="strokeWidth" />
    <attr name="gapWidth" format="dimension" />
    <attr name="android:background"/>
</declare-styleable>
  1. What does reference format mean, I cannot still understand what does this format mean, what I've found that it can be used with drawables also I seen that can be used with themes. Please give good example where it can be useful and what is the main purpose of this format type. In preceding example it is used for style, but I don't understand how can I use this in my xml file where I am going to use custom view.
   <com.viewpagerindicator.CirclePageIndicator
        android:id="@+id/indicator"
        android:padding="10dip"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        custom:auto_cycle="true"
    custom:pageColor="@color/abc_background_cache_hint_selector_material_dark"
        />
  1. Each declare-styleable name="Name of custom view" line, as I can understand, used for separate custom view, we can see this in the example. But what is the purpose of <declare-styleable name="ViewPagerIndicator">, I haven't found any view in this lib with such name and this stylable is include my first question (reference attribute).

  2. If attribute declared out of declare-stylable it means that it is in several view and not to duplicate code in each view common attributes are placed outside. Am I right ?

Please help, I have spent a day trying to find any tutorials or documentation. Official documentation is very poor. I think a lot of newbies will be grateful for explaining this topic.

Please help to understand this, the most important question is about references (first qustion) <attr name="vpiCirclePageIndicatorStyle" format="reference"/> what is this , how to use this, where it is declared ?

Also I found another example used in another lib

<declare-styleable name="Themes">
    <attr name="SliderStyle" format="reference"/>
    <attr name="PagerIndicatorStyle" format="reference"/>
</declare-styleable>

I cannot understand this at all. Please help, you are my last hope.

Upvotes: 4

Views: 5980

Answers (1)

DariusL
DariusL

Reputation: 4087

AFAIK, reference allows you to reference other drawables and styles in that attribute.

<declare-styleable name="Theme">
    <attr name="indicatorStyle" format="reference"/>
</declare-styleable>

This allows me to declare a custom style that I can get later

<style name="AppTheme.Platform.NoActionBar" parent="Theme.AppCompat.NoActionBar">
    <item name="indicatorStyle">@style/ViewPagerIndicator</item>
</style>

Now I can use this in XML with

style="?attr/indicatorStyle"

If I try to use this attribute in a theme that does not declare it I'll get an error. That's what the ViewPagerIndicator style is for, to declare custom attributes in the app theme. I didn't get your third question, but this should cover the first two.

Upvotes: 6

Related Questions