Adithya
Adithya

Reputation: 2975

usage of resources in layout

I don't want to specify hard coded test size values in my layout.xml, hence i am using the following specification :

android:layout_height="@integer/intervalViewHt"

and @integer/intervalViewHt value is as follows:

<integer name="medium">15</integer>

Now, the while inflating android is creating a problem saying that it cannot inflate the view. I want to actually specify value in dp so the actual value should be like

android:layout_height="15dp"

Can anyone help me here ?

Upvotes: 1

Views: 76

Answers (3)

MH.
MH.

Reputation: 45493

What you're looking for are "dimensions" rather than plain integers.

Declaration:

<dimen name="intervalViewHt">15dp</dimen>

Usage:

android:layout_height="@dimen/intervalViewHt"

Have a look at the given link for more examples in the Android docs.

Upvotes: 2

MinceMan
MinceMan

Reputation: 7592

I use something like this for text size across different devices. It's java based, not xml.

tvOutput.setTextSize(TypedValue.COMPLEX_UNIT_DP, getResource().getInteger(R.integer.medium));

I haven't tested the code so you might have to play with it. I'm sure you could do something like.

button1.setHeight(TypedValue.COMPLEX_UNIT_DP, getResource().getInteger(R.integer.medium));

Though again I haven't tried it and you may need something different then TypedValue

Upvotes: 0

Harry
Harry

Reputation: 1472

What if you give it a String rather than an int. i.e in strings resource you have a string "15dp" that you reference

Upvotes: -1

Related Questions