Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 744
Nick Avatar asked Apr 13 '12 18:04

Nick


People also ask

How do I resize text on android?

To make your font size smaller or larger: On your device, open the Settings app. Search and select Font size. To change your preferred font size, move the slider left or right.

Why should you use string resources instead of hard coded strings?

As a general rule, you should never use hardcoded strings in your layout but always use string resources (which means the all strings are stored in one separate file where they are easily changeable for different languages and so on).

How do I change font size in android programmatically?

To set Android Button font/text size, we can set android:textSize attribute for Button in layout XML file. To programmatically set or change Android Button font/text size, we can pass specified size to the method Button. setTextSize(specific_size).

Can we change the text size programmatically?

Thus, various attributes of a TextView such as the text, text color, text size, TextView background, and its size can be changed programmatically.


2 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.

like image 89
Shellum Avatar answered Oct 12 '22 21:10

Shellum


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);
like image 4
Arif Nadeem Avatar answered Oct 12 '22 21:10

Arif Nadeem