Fork me on GitHub

Wednesday, September 18, 2013

Unit of measurement of TextView.setTextSize()

The Soundcorset metronome and tuner, in which I wrote it with Scaloid, is keep growing and hits 70,000 downloads. Until recently, it has a strange problem that it displays very large text for some devices, so that some button texts are clipped away out of the layout. After some code investigation, I found that it is very tricky pitfall because the code does not seems to have any problem at first:
STextView("Hello").textSize(20 sp)  // not correct!
The unit specification like 20 sp is very common in Scaloid. The implicit function sp converts the number from the sp unit to the pixel unit. The code above looks fine because the most of the Android API receives a size as the pixel unit. But there was a single exception. The method TextView.setTextSize(float) does not receives a size as the pixel unit, it receives sp unit instead. It may cause a mistake because the most of other APIs handles a size as the pixel unit, even in TextView.getTextSize()!!! So I overridden STextView.textSize so that the APIs have consistency in pixel units:
@inline def textSize  (p: Int) =  textSize_=(p)
@inline def textSize_=(p: Int) = { 
  basis.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, p)
Now we can safely use the method textSize with the implicit unit conversions:
STextView("Hello").textSize(20 sp)  // correct :-D
I patched the current snapshot, and it will be available at the next release of Scaloid.


  1. The last thing you ever want to happen is to have your Android phone get severely affected with a virus. We are now living in the Information Age, where people are willing to exchange lots of bucks just to be able to take hold of powerful information.

  2. The system look varies depending on the phone but the software itself has become better looking over time. Android's openness means that almost anyone can create an application on it.Download ShowBox APK

  3. Some APIs are designed for a particular version of Android. Hence, these APIs do not work on devices powered by different versions of the mobile operating this link here now

  4. Android development requires a high level of ingenuity and imagination as well. An exposure to several verticals across a cross section of industries is also advantageous. Web Development Company

  5. When we look at measuring trust, we enter murky waters. We measure in order to bring certainty to our initiatives. used test equipment auction

  6. Very informative post! There is a lot of information here that can help any business get started with a successful social networking campaign.
    gbwhatsapp apk download

  7. You will be surprised at the number of so-called professionals that slap the title app developer into their occupation. But with further inspection, it turns out that they know little to nothing about creating an app.gerald winata gozali

  8. I am very enjoyed for this blog. Its an informative topic. It help me very much to solve some problems. Its opportunity are so fantastic and working style so speedy.
    Kroger experience

  9. Thank you very much for this great post.

  10. You’ve got some interesting points in this I would have never considered any of these if I didn’t come across this. Thanks!.
    Kroger customer survey

  11. Most of the time I don’t make comments on but I'd like to say that this article really forced me to do so. Really nice post!
    browse this site

  12. Most of the time I don’t make comments on but I'd like to say that this article really forced me to do so. Really nice post!

  13. I really thank you for the valuable info this great subject and look forward to more great posts. Thanks a lot for enjoying this beauty article with me. I am appreciating it very much! Looking forward to another great article. Good luck to the author! All the best!

  14. Please continue this great work and I forward to more of your awesome blog posts.
    seo tools

  15. Interesting post. I Have Been wondering about this issue, thanks for posting. Pretty cool post.It 's really very nice and Useful post.Thanks
    UPS employee portal


  16. Nice post. I was checking constantly this blog and I’m Extremely useful info specially the last part I care for such information a lot. I was seeking this certain info for a long time. Thank you and good luck.

    survey monkey usa

  17. This is such a great resource that you are providing and you it away for free. I love seeing blog that understand the value.

    Kids Shoes Online Shopping

  18. You have a good point here!I totally agree what you have said!!Thanks for sharing your views...hope more people will read this article!!!
    artificial jewellery in karachi