Chapter 3. View tips and tricks

 

In this chapter, you’ll read about different hacks that use views. Most of them show how to customize and/or tweak widgets to perform certain functionalities.

Hack 9 Avoiding date validations with an EditText for dates: Android v1.6+

We all know that validating data in forms is boring as well as error-prone. I worked on an Android application that used a lot of forms and had a couple of date inputs. I didn’t want to validate the date fields, so I found an elegant way to avoid it. The idea is to make users think they have an EditText when it’s in fact a button that will show a DatePicker when clicked.

To make this happen, we’ll change the default background of an Android Button to the EditText’s background. We can do this easily from the XML:

<Button android:id="@+id/details_date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:background="@android:drawable/edit_text" />

Note how we used @android:drawable instead of a drawable of our own. Using Android’s resources inside your application has its pros and cons. It makes your application fit in the device, but it’ll look different on different devices. Some developers prefer using their own resources, drawables, and themes to have their own look.

Hack 10 Formatting a TextView’s text: Android v1.6+

 
 

Hack 11 Adding text glowing effects: Android v1.6+

 
 
 

Hack 12 Rounded borders for backgrounds: Android v1.6+

 

Hack 13 Getting the view’s width and height in the onCreate() met- thod: Android v1.6+

 
 
 
 

Hack 14 VideoViews and orientation changes: Android v1.6+

 
 
 

Hack 15 Removing the background to improve your Activity startup - time: Android v1.6+

 
 

Hack 16 Toast’s position hack: Android v1.6+

 
 
 

Hack 17 Creating a wizard form using a Gallery: Android v2.1+

 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest