Tuesday, July 5, 2011

Android Sample Code

Add Two Numbers



<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/btnAdd"
android:layout_width="88px"
android:layout_height="wrap_content"
android:text="Add"
android:layout_x="109px"
android:layout_y="204px"
>
</Button>
<TextView
android:id="@+id/widget33"
android:layout_width="wrap_content"
android:layout_height="17px"
android:text="First Number:"
android:layout_x="43px"
android:layout_y="34px"
>
</TextView>
<TextView
android:id="@+id/widget34"
android:layout_width="wrap_content"
android:layout_height="22px"
android:text="Second Number:"
android:layout_x="27px"
android:layout_y="76px"
>
</TextView>
<TextView
android:id="@+id/widget35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sum:"
android:layout_x="90px"
android:layout_y="120px"
>

Displaying the List of music files Stored in SD card and playing music in the background

The foolowing example used to list all the music files stored in SDcard and you can play the music
in the backgroung by selecting the file from list.

main.xml
--------
<?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" android:layout_width="fill_parent"
      android:layout_height="fill_parent">
      < ListView
            android:id="@+id/PhoneMusicList"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
</LinearLayout>

MusicActivity.java
-------------------
package sample.music;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MusicActivity extends Activity {
      ListView musiclist;
      Cursor musiccursor;
      int music_column_index;
      int count;
      MediaPlayer mMediaPlayer;

How to Display Thumbnails of Images Stored in the SD Card

Using the following example we can display the thumbnails of images stored in the sd card. Here im displaying the thumbnails in the Grid view.
You should to add the folowing permission in the manifest xml file.
< uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
Sd card should be available with the emulator or real device.

ImageThumbnailsActivity is the main Activity.

AndroidManifest.xml
--------------------
<?xml version="1.0" encoding="utf-8"?>
< manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="image.Thumbnails" android:versionCode="1" android:versionName="1.0.0">
      < uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
      < application android:icon="@drawable/icon" android:label="@string/app_name">
            < activity android:name=".ImageThumbnailsActivity"
                  android:label="@string/app_name">
                  < intent-filter>
                        < action android:name="android.intent.action.MAIN" />
                        < category android:name="android.intent.category.LAUNCHER"/>
                  </intent-filter>
            </activity>
            < activity android:name=".ViewImage">
                  < intent-filter>
                        < action android:name="android.intent.action.VIEW" />
                        < category android:name="android.intent.category.DEFAULT" />
                  </intent-filter>
            </activity>
      </application>
</manifest>

package image.Thumbnails;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;

How to use Http connection , SaxParser and SharedPreferences in Android

The following code helps you to create a login screen and connect with the server using the Http connection.
The login url will give the following xml file as a output.
-----------------------------
< login>
      < status> SUCCESS </status>
      < message> Login Successful </message>
</login>
------------------------------

< login>
      < status> ERROR </status>
      < message> Login Failed </message>
</login>
------------------------------
Here im using the saxparser to retreive the xml output.
Application will navigate the user to the welcome page once he logged in successfully.
Otherwise user will get login failed message.
The username and password will be stored in the SharedPreferences after the successful login.
Application will navigate the user to the main page directly once the user completed the login successfully.
User need not to login each time of application launching.

AndroidManifest.xml
--------------------
<?xml version="1.0" encoding="utf-8"?>
< manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="login.sample"
      android:versionCode="1"
      android:versionName="1.0.0">
      < uses-permission android:name="android.permission.INTERNET" />
      < application android:icon="@drawable/icon"
            android:label="@string/app_name">
            < activity android:name=".LoginActivity"
            android:label="@string/app_name">
                  < intent-filter>
                        < action android:name="android.intent.action.MAIN" />
                        < category
            android:name="android.intent.category.LAUNCHER"/>
                  </intent-filter>
            </activity>
            < activity android:name=".LoginError"
            android:label="@string/app_name"
            android:theme="@style/Theme.CustomDialog">
                  < intent-filter>
                        < action android:name="android.intent.action.VIEW" />
                        < category android:name="android.intent.category.DEFAULT" />
                  </intent-filter>
            </activity>
            < activity android:name=".Welcome"
            android:label="@string/app_name">
                  < intent-filter>
                        < action android:name="android.intent.action.VIEW" />
                        < category android:name="android.intent.category.DEFAULT" />
                  </intent-filter>
            </activity>
      </application>
</manifest>