0

I have a value in my dimens.xml defined as follows:

<dimen name="input_text_width">250dp</dimen> 

I want to use it to dynamically size the width of a TextView, as follows:

tv.setWidth(activity.getResources().getInteger(R.dimen.input_text_width)); 

The resource exists (checked in R.java). I have run "clean" on my project more than once. Why do I keep getting this exception?

android.content.res.Resources$NotFoundException: Resource ID #0x7f050012 type #0x5 is not valid 
2

3 Answers 3

2

I have taken this from Android Docs

<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="textview_height">25dp</dimen> <dimen name="textview_width">150dp</dimen> <dimen name="ball_radius">30dp</dimen> <dimen name="font_size">16sp</dimen> </resources> 

And in code

//Retreive dimension Resources res = getResources(); float fontSize = res.getDimension(R.dimen.font_size); 

In your xml

<TextView android:layout_height="@dimen/textview_height" android:layout_width="@dimen/textview_width" android:textSize="@dimen/font_size"/> 

I hope this will help!

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

Comments

0

You have to get the dimension pixels...

int size = activity.getResources().getDimensionPixelSize(R.dimen.input_text_width); textView.setWidth(size); 

Comments

0

Might it be because you use :

activity.getResources().getInteger(R.dimen.input_text_width) 

Instead of :

activity.getResources().getDimension(R.dimen.input_text_width); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.