Search code examples
androidxmldoublehardcode

How can I save a double in values folder on android


I don't want to hardcode a double, so I wanted to put it in the values folder, either in a new XML or in the strings.xml

I know it exists:

<string name="a_string">my string</string>
<bool name="is_a_bool">true </bool>
<string-array name="string_array">
        <item>ítem 1</item>
        <item>ítem 2</item>
</string-array>

etcetera.

but how can I do to add a double?, like this:

<double name="a_double">-20.50</double>

cause I want to use it here:

<EditText
            android:layout_width="170dp"
            android:layout_height="wrap_content"
            android:inputType="number|numberDecimal|numberSigned"
            android:ems="10"
            android:id="@+id/txtLatitude"
            android:text="@.../a_double" />

Solution

  • You have to put your double in a string:

    <string name="a_double">-20.50</string>
    

    And you it in layout as this:

    <EditText
                android:layout_width="170dp"
                android:layout_height="wrap_content"
                android:inputType="number|numberDecimal|numberSigned"
                android:ems="10"
                android:id="@+id/txtLatitude"
                android:text="@string/a_double" />
    

    Please visit Android developer website, there's no double resources:

    http://developer.android.com/guide/topics/resources/available-resources.html http://developer.android.com/guide/topics/resources/more-resources.html