Take screenshot in Android AVD

When publishing Android applications both Google and Amazon make screenshots mandatory. The quickest way to create a set of screenshots is to used the AVD. These instructions assume Eclipse is being used for development.

1. Launch your applications in the AVD.
2. From Eclipse navigate to Window > Show View > Other
3. In the Show View dialog expand Android
4. Double click Devices to open the Devices window
5. Select your ADV and click the camera icon on the toolbar of the Devices window
6. This will open the screenshot window where the image can be cropped and saved.

Debug Android Applications using Log

Many people use System.out when developing Java applications for debug perposes. Things are slightly different for Android applications and System.out doesn’t work.

If you are using Eclipse add the log viewer by going to Window->Show View->Other->Android->LogCat. Then in your code use Log.v, Log.d, Log.i, Log.w and Log.e to output verbose, debug, information, warning and error information respectively.

Easy way to save settings when developing Android Apps

I have been looking for an easy way to save application settings when developing for Andoid. In the past I’ve used sqlite to save settings etc in a database. However it appears Google have though about this and created a class to make everyone’s life simple, its called Shared Preferences. To save a string object use the following:

Create a SharedPreferences object using settingsfile as settings storage name.

SharedPreferences settings = getSharedPreferences(“settingsfile”, 0);

Create an editor object attached to the SharedPreferences object. This allows the settings storage to be updated.

SharedPreferences.Editor editor = settings.edit();

Add a string “Tommy” to the settings file using cats_name as the key

editor.putString(“cats_name”, “Tommy”);

Commit the above changes

editor.commit();

The following retrieves the above settings:

Open the settings store again using the same SharedPreferences code

SharedPreferences settings = getSharedPreferences(“settingsfile”, 0);

Retrieve the string object, if the settings is null “Fred” is returned as a default.

String name = settings.getString(“cats_name”,”Fred”);

Other datatypes such as int and boolean can also be used. Please consult the Android SDK documentation for further information.

Setting an Android Application Theme

To change the theme of your Andoid appllication add the android:theme attribute to the application tag of your AndroidManifest.xml file. As of Android 2.2 the following are available

android:theme=”@android:style/Theme.Black”

android:theme=”@android:style/Theme.Light”

android:theme=”@android:style/Theme.Translucent”

For example to set the light theme your application tag will look similar to the following

<application android:icon=”@drawable/icon” android:label=”@string/app_name” android:theme=”@android:style/Theme.Light”></application>

Create custom Android application icon

1.Create a 48×48 PNG and drop it into /res/drawable. The name of this file is usually “icon.png”.
2.Open your AndroidManifest.xml.
3.Right under the root “manifest” node of the XML, you should see the “application” node. Add this attribute to “application”. (The “icon” in “@drawable/icon” refers to the file name of the icon.)

android:icon=”@drawable/icon”

Example: An image icon called appicon.png is copied to resdrawable. The AndroidManifest.xml is updated to include android:icon=”@drawable/appicon”.

Android Application Wide Variable

Create a subclass of android.app.Application before specifying the class in the android manifest file.

In this case, Android will automatically create an instance of that application class and make it available to entire application.

Example of such class is:

public class CombankDroid extends Application
{
 Private boolean isLogin;
 public boolean isLogin()
 {
  return isLogin;
 }
 public void setIsLogin(boolean b)
 {
  isLogin = b;
 }
}
Of course you need to declare this CombankDroid class that extends application in Android manifet at application tag.

<application
    android:name=”.CombankDroid”
    android:icon=”@drawable/first_aid”
 android:label=”@string/app_name” >
</application>

You can access CombankDroid application class from any context using the Context.getApplicationContext() method. Also, Activity class also provides a getApplication() which has same effect.

class Anyhow extends Activity
{
  @Override
  public void onCreate(Bundle b)
  {
        CombankDroid combankDroid = ((CombankDroid)getApplicationContext());
        //CombankDroid combankDroid = (CombankDroid) getApplication();
        // Don’t start the main activity if we don’t have credentials

        if (!combankDroid.isLogin())
  {
            redirectToLoginActivity();
        }
  else
  {
            loadUi();
        }
  }
}