Search code examples
androiddoubledeclare-styleable

How to get double value from "declare-styleable"


How to get double value from declare-styleable?

attributes.xml

<declare-styleable name="MeterView">
    <attr name="volume" format="double"/> // Can't resolved format
</declare-styleable>

Assign double value to meterView

<com.test.example.MeterView
    android:id="@+id/meter1"
    android:layout_width="295dp"
    android:layout_height="150dp"
    app:volume="123456789.01"/>

Calling attributes.xml

Double volume = a.getDouble(R.styleable.MeterView_volume); // Can't resolved getDouble

Solution

  • use float instead of double in your attributes.xml file. double is not allowed.

    <declare-styleable name="MeterView">
            <attr name="volume" format="float"/> 
    </declare-styleable>
    
    a.getFloat(R.styleable.MeterView_volume);
    

    or use string and convert String to double in runtime.

    <declare-styleable name="MeterView">
                <attr name="volume" format="string"/> 
     </declare-styleable>
    
    double d= Double.parseDouble(a.getString(R.styleable.MeterView_volume));