Android: How to send a message from a Thread to a Handler

As a quick example of how to use a Thread with a basic Handler in an Android application, the following code creates a view where the text in the TextView is updated to show the current date and time when the Button is tapped.

Java source code

First, here’s the Java source code for a file class named ThreadHandlerActivity:

Source code for an Android AsyncTask (REST client) example

I just got back into using an Android AsyncTask, and it took me a little while to re-load the concepts in my head. I used AsyncTask’s a few years ago, but haven’t used them since.

To help remember how they work, I created a little AsyncTask example project, and I’ve included all of the source code for that project here. I’ll show all of the source code for my classes and configuration files, and then explain the code at the end.

How to show an HTML string in an Android TextView

Filed under “What I learned about Android today,” you can display an HTML string in an Android TextView. However, this approach has severe limitations, and you’ll probably want to display your HTML in a WebView instead.

To display an HTML string in a TextView, you need to use the Android Html.fromHtml() method, as shown in this code:

How to put a background image behind a translucent/opaque TextView in Android

I just had an unusual Android need: I wanted to put an image behind a TextView, where the TextView was occupying the fullscreen. But, I didn’t want the image to be completely visible, I wanted the TextView to be mostly opaque so that you would only get a hint of the image. You can think of this as wanting a watermark image behind a large text editing area.

Jumping right to the solution, this Android XML layout code gave me the solution:

How to programmatically set the font size and style of an Android TextView

Android FAQ: How do I programmatically set the font size (and/or font style) for an Android TextView?

Solution: Setting the Android TextView font size programmatically is a simple two-step process. First, define everything about the font that you want to use in a resources file. For example, I put this code in a file named res/values/styles.xml: