Tuesday, October 12, 2010

TableLayout columns equal width

Something easy like columns of equal width in a TableLayout shouldn't be difficult. It's not, but there is a trick to getting the desired layout. layout_width="0dip" and layout_weight="1" tags must be set in your XML layout. Here's an example:

<tablelayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1">

    <TableRow>
      <!-- Column 1 -->
      <TextView
         android:id="@+id/tbl_txt1"
         android:layout_width="0dip"
         android:layout_height="wrap_content"
         android:background="@color/red"
         android:textColor="@color/white"
         android:padding="10dip"
         android:layout_margin="4dip"
         android:layout_weight="1"
         android:text="Column 1" />
         
      <!-- Column 2 -->
      <TextView
         android:id="@+id/tbl_txt2"
         android:layout_width="0dip"
         android:layout_height="wrap_content"
         android:background="@color/red"
         android:textColor="@color/white"
         android:padding="10dip"
         android:layout_margin="4dip"
         android:layout_weight="1"
         android:text="Column 2" />
         
      <!-- Column 3 -->
      <TextView
         android:id="@+id/tbl_txt3"
         android:layout_width="0dip"
         android:layout_height="wrap_content"
         android:background="@color/red"
         android:textColor="@color/white"
         android:padding="10dip"
         android:layout_margin="4dip"
         android:layout_weight="1"
         android:text="Column 3" />
    </TableRow>
</TableLayout>

Friday, October 1, 2010

Position two buttons on each side of the screen

Working with layouts in Android can sometimes be extremely simple and other times extremely difficult to accomplish seemingly simple tasks. I fought for a while with how to position two buttons on each side of the screen with a space between them. I'll spare all of my trials and tribulations, and get right to the code that makes it work...

Friday, September 24, 2010

App Inventor sample project: List Pickers

This sample project using Google's App Inventor demonstrates how to use ListPickers (data lists). Some of the items I'll highlight include:
  • Implementing ListPickers
  • Introduce ActivityStarter
  • Creating and working with list data objects
  • Hooking into Google Maps app and Browser app

Let's get started...

App Inventor sample project: Loan Calculator

Our next sample project using Google's App Inventor is the classic Loan Calculator. Some of the items I'll highlight include:
  • Layout design techniques
  • Adding images
  • Math functions
  • Button actions

Let's get started...

Friday, September 3, 2010

App Inventor sample project: Barcode Scanner

Up until now, I've created all Android apps in Eclipse with Java and XML code. While I will continue to do that, Google has released a very promising new web tool called App Inventor. This tool allows you to create simple Android apps in the browser all while requiring the developer to have exactly zero (0) knowledge of Java or XML. I've played around with the tool a bit and found that it is indeed easy and intuitive to create simple, 1-screen apps (as of now, it doesn't allow multiple screen apps so that really limits the complexity). For anything even remotely advanced, you'll still have to use the old methods (writing code).

In order to test it out, I decided to build an app--after all, that's the best way to learn, right? I wanted something simple, yet at least a little interesting so I went with a barcode scanner. I know, the app already exists, but this would be fun to see what I could accomplish in about 30 minutes. The app scans a barcode or QRcode and either searches Google with the result or opens the URL in a browser (if the QRCode is a URL, obviously).

So away we go...

Check Android network connection sample code

Chances are if you're building an Android app, you're going to be using the device's data connection. After all, the beauty of these things is having the power of the internet at your fingertips. You have to be careful though when building apps to always for the existence of an active connection. Failure to do so and then trying to fetch data could result in a negative user experience or possibly even crash.

Luckily, there's an easy way to check for data connections prior to making calls so you can either proceed or handle gracefully. The sample class below provides everything you need. Just insert into your project and call when necessary...

Thursday, September 2, 2010

Asynchronous web request

Many good Android apps pull in some kind of external data to keep everything current. When making web requests for data, it is important to use asynchronous calls on a new thread so that the UI remains responsive to the user. Asynchronous calls do require a bit more code than synchronous calls, but it is a minimal amount and pays big dividends to the polish of the app.

There are 2 functions required for the call: 1) makeHTTPRequest and 2) handleHTTPResponse. Sample code for each of these is provided below.