android eclipse using string resource for text size in layouts

I want to be able to store a font size (18sp) in a resource so that a lot of my layouts will use so that I can easily change the size in the future if I need to. I've a string in a string resource file with "18sp" as the value, and I can call it in the layout editor by setting the text size to "@strings/string_name". At that point everything is fine, and the text is changed to the correct font. The problem is once I try to test that on an actual device I get :

04-13 12:01:01.210: E/AndroidRuntime(17114): Caused by: android.view.InflateException:    Binary XML file line #50: Error inflating class <unknown>


04-13 12:01:01.210: E/AndroidRuntime(17114):    at 

android.view.LayoutInflater.createView(LayoutInflater.java:606)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.app.Activity.setContentView(Activity.java:1835)
04-13 12:01:01.210: E/AndroidRuntime(17114):    
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.app.Activity.performCreate(Activity.java:4465)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-13 12:01:01.210: E/AndroidRuntime(17114):    ... 11 more
04-13 12:01:01.210: E/AndroidRuntime(17114): Caused by: java.lang.reflect.InvocationTargetException
04-13 12:01:01.210: E/AndroidRuntime(17114):    at java.lang.reflect.Constructor.constructNative(Native Method)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.view.LayoutInflater.createView(LayoutInflater.java:586)
04-13 12:01:01.210: E/AndroidRuntime(17114):    ... 29 more
04-13 12:01:01.210: E/AndroidRuntime(17114): Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x3
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.widget.TextView.<init>(TextView.java:786)
04-13 12:01:01.210: E/AndroidRuntime(17114):    at android.widget.TextView.<init>(TextView.java:442)

Is this because I am using a string resource as a text size, or would it be some other underlying problem? If it is because i am using a string resource, is there a different / more proper way to have a global text size that I can call on so that I can change everything by modifying 1 value?

Also to note: My app worked just fine before I tried using a string resource as a text size, so I'm relatively certain it can't be something unrelated that is causing the error.

THanks!

Answers


Create a dimens.xml file in your res directory with contents similar to this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="some_text_size">18dp</dimen>
</resources>

Then instead of referencing it like @string/blah, use @dimen/blah.


Use this as your XML resource and call it from where ever you want...

<?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>

Then call this in code:

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

Need Your Help

PHP GD cropping PNG doesn't work

php image png jpeg crop

I'm creating a function to crop images using PHP GD.

Can I run Ubuntu Enterprise Cloud on Sun sparc servers?

cloud ubuntu-9.10 sparc eucalyptus

I would like to deploy a private cloud using Ubuntu Server 9.10. There is a -sparc iso available here.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.