0

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.

1
  • 1
    Please do use 'dimens.xml' instead of 'strings.xml'. Commented Oct 8, 2017 at 17:13

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@Funlamb: The IDE might have warnings in the layout editor when you tried applying those resources -- I haven't tried this specific scenario. Otherwise, the error message in LogCat should have said something about your resource ID not being a valid dimension resource.
The IDE didn't have any errors. The only time I saw a problem was when I was trying to start the app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.