Why Your TextView or Button Text Is Not Updating

Understand Why Changing View Text Does Not Work or is Delayed

When changing the text for a TextView using the setText method new Android developers sometimes fail to understand why it does not appear to work. The text not updating also applies to other Views as well, such as the EditView and Button, why this happens is explained. This article is also useful in helping to understand why the User Interface (UI) in your Android App might not be as responsive as expected. The underlying problem is the same. So if you are trying to find out why your setText() on EditText, TextView, Button, etc. is not working as intended, or you UI is sluggish read on, it includes a work around to help you out.

Not All Android Code Executes Immediately

When changing the TextView text this code is second nature to all Android developers:

For those new to programming the above is equivalent to these two lines but saves having to declare the TextView object:

When setText(“New Text”) is run the text is not updated immediately. Android is an event based system. Something happens on the device (the screen is touched, a key is pressed, a call comes in, etc.) and Android raises an event. An App is notified of an event and when one occurs that it needs to respond to it does so, often running the code that you have written. Your App runs its code in a loop under the control of the Android Operating Systems (OS). This code loop is referred to as the App’s thread of execution. There is only one thread and it is responsible for both running the App code and updating the display. The setText call posts a message to update the display, so the update does not happen immediately. Once remaining App code has run the UI messages are processed, then the text changes. A running App’s execution thread can be viewed as shown in this simplified diagram. Continue reading

Changing the Font for Android TextViews and EditTexts

Easy Font Modifications to Get Different Text Styles in Android

Setting a new font when developing an Android App puts your own style onto the user interface (UI) and can help it stand out from the crowd. For many Activity screens the interface is usually constructed using Android layouts and interface widgets. The interface widgets, such as CheckBoxes and TextViews, have various properties that can be easily modified to change their look. Changing properties is done via the Properties list when viewing an Activity in the Graphical Layout screen in Eclipse, by editing the layout XML directly, or finally by using code to set property values.

This article mainly covers changing the look of a widget’s font using the Properties list. You can follow this article as a tutorial by starting a new Android Application Project in Eclipse and playing with widget properties as we go. (See our article Your First Android Java Program if you do not now how to create a basic App project. Here we have called the new App and project Android Fonts and given the package name biz.tekeye.fonts but you will be using you own package name.)

Edit Text Properties with the Android Development Tools Graphical Layout

Drag and drop a medium Android TextViewWith the activity_main.xml layout open and the Graphical Layout shown (click the tab at the bottom of the edit area if required) drag and drop some Medium TextViews onto the first activity’s screen. Click on the Form Widgets tab if needed to find the TextViews. With a TextView selected its properties can be changed in the Outline pane. If the Outline pane is not visible use the Eclipse Window and Show View menu option to select Outline to display it. Continue reading