Aharon Manne
Aharon Manne

Reputation: 712

ResourcesNotFoundException

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

Upvotes: 0

Views: 1296

Answers (3)

Gopal Gopi
Gopal Gopi

Reputation: 11131

You have to get the dimension pixels...

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

Upvotes: 0

Sebastien FERRAND
Sebastien FERRAND

Reputation: 2150

Might it be because you use :

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

Instead of :

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

Upvotes: 0

Faizan Mubasher
Faizan Mubasher

Reputation: 4569

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!

Upvotes: 2

Related Questions