Funlamb
Funlamb

Reputation: 625

Android: integer from xml resource into a layout file

I have created a layout for my main page. In that page I have used a few lines to separate out some sections. Since this lines are all the same thickness I decided to make a res file with integers that I can use and change in an easier way. The problem is if I use that res my program wont load.

Here is code that works:

 <View
        android:layout_width="fill_parent"
        android:layout_height="3dp"
        android:background="#C0C0C0" />

Here is the code using the res file:(doesn't load)

<View
        android:layout_width="fill_parent"
        android:layout_height="@string/line_thickness"
        android:background="#C0C0C0" />

Here is the res file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="layout_line">3</integer> //Integer used for xml
    <string name="line_thickness">3dp</string> // string used for xml
</resources>

I tried using the res file in a couple different ways. First I tried using it with and integer. That didn't work at all and wouldn't even let me load the app. Using the string allowed me the load the app but it crashes on startup.

What am I doing wrong here? Android shows how to use these res files but they don't seem to work in this case.

Upvotes: 0

Views: 97

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006664

android:layout_height does not take an integer. It takes a dimension value or dimension resource ID.

Change:

<string name="line_thickness">3dp</string>

to:

<dimen name="line_thickness">3dp</dimen>

and refer to it as @dimen/line_thickness in the layout.

Upvotes: 1

Related Questions