Monday, January 2, 2012

Beginning Android Programming

Hello,

Here is my second blog on Android and this is some what different blog. Here I am going to share some useful tips for beginners of android. These are the issues which I faced as a beginner in Android.

1) How to setup your phone for testing an Application.

Normally android phones does not allow to test application but you can configure it. Go to Settings->Applications

There will be a check box here Unknown Sources (Allow installation of Non market applications). It must be checked in . If not it will only application installation from android market.

After that Go to Settings->Applications->Development. Here there will be another check box USB debugging (Debug mode when USB is connected) It must be checked in. If not, it will not catch an exception in LogCat of eclipse IDE.

2) How to uninstall application from your virtual device

There might be the case that while you are testing with virtual device, you need to re install application. For that you have to use Android ADB. Keep your virtual device on  while working through adb. That you can find in Android android-sdk\platform-tools Go to command prompt and navigate to above location. After that type following command. 
adb shell. 

It will open an adb shell. Go to data/app folder here you will find application apk file. Remove apk file using following command.
rm application_name.apk

This will remove application from virtual device. If you want to remove application from phone you can do it via Applications->Manage Applications.

3) How to prevent activity from restarting on orientation change

This might be one of the common issue every beginners faces. You have declared an activity in in your manifest file. While testing in phone when you change orientation, application restart. To prevent this you need to add following attribute to your activity declaration in your manifest file.

android:configChanges="orientation"

This will save application data and state on change of orientation and will restore it back.

4) How to start new activity

We normally call it navigation. You have a one screen in one activity. On some user actions you want to load another screen. For that first you need to declare all the activities in your manifest file. Following is the code to start new activity

Intent i = new Intent(CurrentActivity.this, NewActivity.class);
startActivity(i);

5) How to pass data between two activities 

It might be the case that you need to pass some parameters from one activity to another. Following is the code you need to add in intent of new activity.

Intent i = new Intent(CurrentActivity.this, NewActivity.class);
Bundle bundle = new Bundle();
bundle.putString("param", "value");
i.putExtras(bundle);
startActivity(i);

In new activity you can access this parameter in onCreate method as follow.

Bundle bundle = this.getIntent().getExtras();
param = bundle.getString("param");


I hope this helps you.

1 comment:

  1. Thanks for sharing valuable Android Programming tips for beginner. I think you need to add compiling process and Debugging process of application.

    ReplyDelete