Guest Post: Android TextView setTextSize Gotcha

The quoted post below is from a colleague of mine, Arpit Mathur.  You can find his personal blog by clicking here.  He describes a design decision in the Android framework that might lead a few people to scratch their heads.  So without further adue, here is post:

Most developers know about the different ways to specify dimensions of visual elements in Android. While setting dimensions for visual elements is usually done in “device independent pixels”, it is recommended to use “scale independent pixels” (sp) for type. The Android OS provides a setting (under the Display category on stock Android) where users can set the default minimun font size of all applications to small, normal, large or huge, an important accessibility feature for users with poor eyesight. Any text fontSize set in sp’s will scale up or down to respect this setting.

That said, you may want your textView to not scale up or down with the settings. Setting the TextView value to some sp value in the layout.xml file will do that. However if you are setting the text size in code, remember that even if you do read the dimensions from a dimen.xml file with values set in dp, the value returned cannot be used in textView.setTextSize (value) since it will interpret those values as sp’s regardless. To set dp values, use the other setTextSize method that takes an additional parameter: how to interpret the numbers being read.

TL;DR: Use setTextSize(TypedValue.COMPLEX_UNIT_DIP, xx) to set dp values to textviews

Advertisements

The Pitfalls of AsyncTask

As a professional Android developer, I spend a lot of time talking with, and helping, other developers.  One class that seems to cause a decent number of problems is the AsyncTask.  For those that don’t know, an AsyncTask is a nice and clean wrapper for threads, which is designed to make background processing easier on Android.

Why is background processing important you ask?  It allows the application do things that are slow and take a long time without stopping the user from interacting with the app.  As an example, imagine an app that downloads 10 very large images that take 6 seconds each.  If you have to download them all without any background processing, you would need all 10 images to fully download before you could interact with any of them, forcing you to wait a full minute.  On the other hand, if the images were downloaded in the background, you could start interacting with the images immediately after each one finishes (6 seconds for the first).

Continue reading

Access another apps database

Shockingly, there is very little information online that I could find on how to get access to another apps database.  I know that Android prevents this for security reasons.  This means I’ll have to work around Android’s security, or use root.  Below you will find a simple tutorial on one method that worked for me.

***DISCLAMER***

Rooting your phone, and accessing the underlying files can be dangerous for your phone.  While the following code worked for me, you use it, and any modification of it at your own risk!  By reading further, you agree that I cannot be held responsible for any damage to your phone or apps on your phone.

Firstly, you must have a rooted Android phone, Eclipse installed, the Android SDK downloaded and installed, and you must know where the app and database is located you want to edit (try looking in /data/data).  There are plenty of guides on the internet that explain how to do this.  In the Activity of your choosing, you would have the following:

Continue reading

Surviving Google Reader’s shutdown…

Before I get into the main topic of this article, let me explain what Google Reader is.  If you are already familiar with it, please feel free to skip this paragraph, otherwise, continue reading!  Google Reader is (soon to be was) a method of aggregating content from various sources.  If you read articles from various websites, you could have had Google Reader automatically collect that information for you, presenting it in an easy to read manner.

Recently I was turned on to Google Reader.  Unfortunately, Google Reader is going to be shutdown in July.  I was upset at first; there goes a great tool for bringing all the content I wanted to read together.  As it turns, it is probably a good thing because of the alternative I came across!  Jump past the break to see what I now use, how I set it up, and some of the great benefits!

Continue reading

Keeping Google Voice’s Voicemail on PIAF/Asterisk/FreePBX

After a lot of trial and error, and some great help from the author of Enjoy Technology, I was able to get an Asterisk server up and running on an old computer.  The process itself appears daunting at first, but is not all that bad.

For those of you that don’t know, the server I set up allows me to make phone calls from my Android phone using wifi or a data plan (a.k.a. Voip).  While there may be easier solutions/apps that do this for me, the call quality was always a bit lacking.  Hence, I decided to roll my own server.

There was one thing that was not covered; how do you keep google voice as the provider of your voicemail?  I did some searching online.  Everything I found talked about editing one file; extensions_custom.conf.  No matter what I tried, nothing worked.  So I decided to do some digging.  Keep reading to see what finally worked, with step by step instructions…

Continue reading