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)
  basis 
}
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.

4 comments:

  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. https://folder-lock-android.en.softonic.com

    ReplyDelete
  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

    ReplyDelete
  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 system.click this link here now

    ReplyDelete
  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

    ReplyDelete