Wednesday, May 18, 2011

Steps to create a basic Android Activity with UI

This is a summary of basic things you need to cover when creating an Activity in relation to the UI
  1. Create a new class file
  2. Declare that extends Activity
  3. Register it at AndroidManifest.xml
  4. Create a new XML file of type Layout
  5. For the Layout set some of this properties: Orientation, Background
  6. Create a TextView and set some of this properties: Text, Text size, Text style, gravity, Layout width
  7. Create an EditText and set some of this properties: Id, Hint, Layout gravity, Layout width
  8. Create a Button and set some of this properties: Id, Layout gravity, Text, On click
  9. Override method onCreate and inflate the Layout
  10. Add the method to receive the Click event and do something

AndroidManifest.xml (partial)
<activity
  android:name=".uiBasics"
  android:label="@string/uiBasics">
</activity>

uibasics.java
package com.correa.android.tests.uibasics;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class uiBasics extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.uibasics);
  }
  public void onClickMyButton(View view) {
    EditText textMyEditText;
    textMyEditText =
      (EditText) findViewById(R.id.MyEditText);
    Toast.makeText(this, textMyEditText.getText(),
      Toast.LENGTH_LONG).show();
  }
}

uibasics.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:background="#AA0000FF">
  <TextView android:id="@+id/textView1"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:layout_width="fill_parent"
    android:text="@string/textTextView1"
    android:textStyle="bold"
    android:gravity="center">
  </TextView>
  <EditText android:layout_height="wrap_content"
    android:hint="Type something here"
    android:layout_gravity="center"
    android:layout_width="250sp"
    android:id="@+id/MyEditText">
  </EditText>
  <Button android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Press Me!"
    android:id="@+id/MyButton"
    android:onClick="onClickMyButton">
  </Button>
</LinearLayout>


Thursday, May 12, 2011

Capturing a screenshot on your Android

If you need a screenshot of you Android, the ddms tool of your SDK provides the means for it.
  1. Open a DOS window or Terminal
  2. CD \android-sdk\tools
  3. Run: ddms
  4. Select your device by clicking on it (left pane of ddms)
  5. On the ddms menu Click DEVICE/SCREEN CAPTURE or press CTRL-S
  6. Now you can Save the image as .png file or Copy to the Clipboard
  7. Don’t forget to Refresh every time you screen changes on your Android
 

Wednesday, May 11, 2011

Logging and Tracking on Android Applications


Tracking what is happening on your program either to inform the user, to debug a program or just to record information of what is occurring on it for later processing is a useful resource for a developer. This becomes important when you are running a Service with no visible interface with the user. Let’s review two ways to do so:

Writing to the Log. Android keeps a log of events. The OS and apps are constantly writing to it and your apps can write too.

One way to take a look at this log is using ADB (Android Debug Bridge) from the command prompt. CD to your SDK folder and look for the abd.exe application currently under the platform-tools folder.

CD  \android-sdk\platform-tools

Now type

Adb logcat

Notice that you have to have an emulator running or a phone plugged.

You should start looking at the live log. Do something on your phone or emulator and you will see that something gets logged.

To write to this log you will use the Log class as this:

Log.i(tag, msg);

Where tag is an identifier to associate the msg with a particular group of items, for instance:

Log.i(“MyAppName-MyClass”, “I’ve reached point XYZ”);

Note that you have to:

import android.util.Log;

Google android.util.Log  for more information.

Displaying a quick message on the screen.  A second way is to use the Toast Class which produces a kind of pop-up temporarily message on top of the current Activity.

To display a message write:

Toast.makeText(this, "Hi There!", Toast.LENGTH_LONG).show();

Note that you have to:

import android.widget.Toast;

For messages that will be part of a deployed solution, use resource strings instead of hardcoded messages:

private static final String TAG = YourClass.class.getSimpleName();
Log.i(TAG, R.string.msgXYZ);

Toast.makeText(this, R.string.msgXYZ, Toast.LENGTH_LONG).show();

Tuesday, May 10, 2011

Android Phones Memory

When I jumped into the Android world, I was not aware of how the memory is managed. Initially I was so happy for having a SD card slot but then I was disappointed because not all applications can be stored on the SD card, so the phone memory became an issue. It was difficult to find information about it or to interpret it.

So an Android phone has two types of memory: INTERNAL and EXTERNAL.

The OS reserves for itself part of the internal memory and the remaining is for you to install apps or to save your data, being music, pictures, contacts, etc. The internal memory is fixed ( a chip on your phone ) and can not be expanded.

On the External memory you can store your data, music, pictures, etc. as well as those applications that allow to be placed on external memory which is set by the developer and cannot be changed (as far as I know).

So I was happy with my new phone that has SD card but after installing a few applications I started to get the memory low warning and then discovered that I cannot move all apps to external memory. Then I had to return my phone and get a different one that had 1 GB of Internal memory.

The original phone had 512 MB of Internal memory. The OS took 316 living 196 for me.

Now, physically speaking, the External Memory can be an SD card or more fix chips inside the phone. The Nexus S from Samsung has a total of 16GB that is used as follows: 1) The OS takes whatever it needs, 2) It gives you a full 1 GB for Internal Memory (your apps) and the remaining behaves as External Memory.

Compared to my initial phone with 196MB, this one gives you 1GB, (1007 MB to be precise) which is great. I can still move those apps that allow it to the external memory, that is to the remaining 14GB or so on my phone. It is true that I can not insert an SD card of 32 but for my use, 16GB total memory is OK as long as is divided as the Nexus S does.

Specs on the phone, will read INTERNAL or ROM, and EXTERNAL or FLASH or SDcard or Total Memory as in the Nexus S, in this case, you have to know how much is reserved for your apps, typically will say reserved for ROM.

A tool to know how much memory you have available for your apps, is DiskUsage and you can find it on the market.

Don't buy a phone without enough memory for your apps, it would be like buying a car with a gas tank of just 5 litters!.

By the way, there is a third type of memory, RAM, that is just like the RAM on your computer, it is used to store data for the running programs but that is maintained by the OS and as far as I know, you won't have trouble with it. The contents of the RAM get erased when your apps stop running or when new apps need the space.

Monday, May 9, 2011

Use YouTube!

It is amazing what you can learn on YouTube. People post instructions and tutorials about just everything. Sharing is common and welcomed.

Search for Android Tutorials, Android Development, etc. and you will find a lot of information, most of them are 10 minutes videos that you can watch in your way to work

Learning from YouTube results in gigantic steps that save you tons of time. (Don't forget to leave a thank you note to the author)

About this blog

I try to keep track of what I learn since most probably I will forget the details a week later. I realized that a blog will be a great tool to keep track of my learnings and at the same time will be a reference material for others.

Among all the areas on IT, developing has always be my preferred one. Although currently working as a PM I've just found joy on learning how to develop applications under the Android platform.

Since the first mobile devices (Palm Pilots, etc.) I've always believe on mobile applications and finally I have found an niche to start. Developing for Android does not require a huge bank of knowledge. Even if you have not developed lately due to your new responsibilities on your job, you will see that with a basic knowledge of java, your background as a developer in any language, understanding of Object Oriented programming and Event Driven applications enriched with your passion for developing, you and  I can create some nice programming, all this with the help of the information available on the web (that was not there some 25 years ago).