Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Saturday, 28 September 2013

Context Menu in Android

In previous topic we learnt the simple menu, So let's learn how to create context menu in android.

A context menu is a floating menu that appears when the user performs a long-click on an element. It provides actions that affect the selected content or context frame.

Now let's create it.
1. MainActivity.java

package com.sri.menus;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

final int MENU_RED = 1;
final int MENU_GREEN = 2;
final int MENU_BLUE = 3;

final int MENU_SIZE_22 = 4;
final int MENU_SIZE_26 = 5;
final int MENU_SIZE_30 = 6;

TextView tvColor, tvSize;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tvColor = (TextView) findViewById(R.id.tvColor);
tvSize = (TextView) findViewById(R.id.tvSize);

// context menu should be created for tvColor and tvSize
registerForContextMenu(tvColor);
registerForContextMenu(tvSize);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
switch (v.getId()) {
case R.id.tvColor:
menu.add(0, MENU_RED, 0, "Red");
menu.add(0, MENU_GREEN, 0, "Green");
menu.add(0, MENU_BLUE, 0, "Blue");
break;
case R.id.tvSize:
menu.add(0, MENU_SIZE_22, 0, "22");
menu.add(0, MENU_SIZE_26, 0, "26");
menu.add(0, MENU_SIZE_30, 0, "30");
break;
}
}

@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
// menu items for tvColor
case MENU_RED:
tvColor.setTextColor(Color.RED);
tvColor.setText("Text color = red");
break;
case MENU_GREEN:
tvColor.setTextColor(Color.GREEN);
tvColor.setText("Text color = green");
break;
case MENU_BLUE:
tvColor.setTextColor(Color.BLUE);
tvColor.setText("Text color = blue");
break;
// menu items for tvSize
case MENU_SIZE_22:
tvSize.setTextSize(22);
tvSize.setText("Text size = 22");
break;
case MENU_SIZE_26:
tvSize.setTextSize(26);
tvSize.setText("Text size = 26");
break;
case MENU_SIZE_30:
tvSize.setTextSize(30);
tvSize.setText("Text size = 30");
break;
}
return super.onContextItemSelected(item);
}

}

2.activity_main.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" >

    <TextView
        android:id="@+id/tvColor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="50dp"
        android:layout_marginTop="50dp"
        android:text="Text color"
        android:textSize="26sp" >
    </TextView>

    <TextView
        android:id="@+id/tvSize"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Text size"
        android:textSize="22sp" >
    </TextView>


</LinearLayout>

3.That's it.

Friday, 27 September 2013

Beginners in Android

Beginner in Android? Develop Application now


1.Required Softwares for Android Application Development
      if u don't have jre7 or java then download here.
      To set the path of java right-click on My computer-->Advance System Setting-->
      in the Advance tab click on Environment Variable.
      In system Variable edit path and give your java path.
      ex:- ;C:\Program Files\Java\jdk1.7.0_25\bin
      That's all. The Process is shown below.






Now Extract the ADT bundle downloaded.
open Eclipse-->eclipse.exe.








Now to run the Application We need a Android Device.
So lets Create one Android Virtual Device(AVD).



Now lets run our Application.


Wait till the android device launces.



That's it.

Wednesday, 25 September 2013

Animations in Android

How to apply animations for the objects in android??


This are few animation xml files which should be stored in res/anim folder.

alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha 
android:fromAlpha="0" 
android:toAlpha="1.0"
android:fillAfter="true" 
android:duration="7000"
/>
</set>

rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
    android:fromDegrees="0"
    android:toDegrees="-360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="7000"
    />
</set>

scale.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:pivotX="50%"
android:pivotY="50%"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="2.0"
android:toYScale="2.0"
android:duration="2500"
/>
</set>

spin.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <rotate  
       android:fromDegrees="0"
       android:toDegrees="360" 
       android:duration="2000"
       android:pivotX="50%" 
       android:pivotY="50%" 
       android:repeatCount="infinite"
  />  
</set>

translate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate  
android:fromXDelta="110" 
android:toXDelta="-120" 
android:duration="4000" 
android:fillAfter="true"
  android:fromYDelta="190" 
  android:toYDelta="0"
/>
</set>


All the above xml files should be stored in res/anim folder

Next now in
MainActivity.java

package com.sri.animation;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.Spinner;

public class MainActivity extends Activity implements OnItemSelectedListener {
Spinner sp;
ImageView i;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp = (Spinner) findViewById(R.id.spinner1);
i = (ImageView) findViewById(R.id.imageView1);
sp.setOnItemSelectedListener(this);
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long item) {
// TODO Auto-generated method stub
if (item == 1) {
Animation a = AnimationUtils.loadAnimation(this, R.anim.alpha);
i.startAnimation(a);
}
if (item == 2) {
Animation a = AnimationUtils.loadAnimation(this, R.anim.rotate);
i.startAnimation(a);
}
if (item == 3) {
Animation a = AnimationUtils.loadAnimation(this, R.anim.scale);
i.startAnimation(a);
}
if (item == 4) {
Animation a = AnimationUtils.loadAnimation(this, R.anim.spin);
i.startAnimation(a);
}
if (item == 5) {
Animation a = AnimationUtils.loadAnimation(this, R.anim.translate);
i.startAnimation(a);
}

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
}

activity_main.xml

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

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/Animation" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_marginTop="100dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/facebook" />

</LinearLayout>

values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Animation</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

    <string-array name="Animation">
        <item>Select Item</item>
        <item>Alpha</item>
        <item>Rotate</item>
        <item>Scale</item>
        <item>Spin</item>
        <item>Translate</item>
    </string-array>

</resources>

Now run the application.

Thursday, 19 September 2013

WebView in Android

Using a web view to load a page from Internet


WebView is nothing but the simple view created within the application so as to load the web pages within the application and not exit the application and load in the inbuilt browser.

1.MainActivity.java

package com.sri.webview;

import android.os.Bundle;
import android.webkit.WebView;
import android.app.Activity;

public class MainActivity extends Activity {
WebView wb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wb=(WebView) findViewById(R.id.webview);
wb.getSettings().setJavaScriptEnabled(true);
wb.loadUrl("http://www.google.co.in");
}
}


2.activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

3.Add permission in the android manifest file

<uses-permission android:name="android.permission.INTERNET"/>

4.That's it

Monday, 16 September 2013

Seek Bar with progress showing with two text views

Seek Bar with progress showing with two text views


In the previous Post i told you how to add the seek bar for media player. Now i'll tell you to add two text views to media player which shows the progress of the seek bar.

1.activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_margin="10dp" />

        <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/seekBar1"
        android:layout_marginBottom="40dp"
        android:layout_marginRight="26dp"
        android:layout_toLeftOf="@+id/textView2"
        android:text="Stop" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_toRightOf="@+id/textView1"
        android:text="Play" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/seekBar1"
        android:layout_alignRight="@+id/seekBar1"
        android:layout_marginRight="15dp"
        android:text="0:00" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignLeft="@+id/seekBar1"
        android:layout_marginLeft="16dp"
        android:text="0:00" />

</RelativeLayout>

2.MainActivity.java

package com.sri.seekbar;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements Runnable,
OnClickListener, OnSeekBarChangeListener {
private SeekBar seekBar;
private Button startMedia;
private Button stopMedia;
private MediaPlayer mp;
TextView tv1, tv2;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBar1);
startMedia = (Button) findViewById(R.id.button1);
stopMedia = (Button) findViewById(R.id.button2);
tv1 = (TextView) findViewById(R.id.textView1);
tv2 = (TextView) findViewById(R.id.textView2);
startMedia.setOnClickListener(this);
stopMedia.setOnClickListener(this);
seekBar.setOnSeekBarChangeListener(this);
seekBar.setEnabled(false);
}

public void run() {
int currentPosition = mp.getCurrentPosition();
int total = mp.getDuration();

while (mp != null && currentPosition < total) {
try {
Thread.sleep(1000);
currentPosition = mp.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
seekBar.setProgress(currentPosition);
}
}

public void onClick(View v) {
if (v.equals(startMedia)) {
if (mp == null) {
mp = MediaPlayer.create(getApplicationContext(), R.raw.song2);
seekBar.setEnabled(true);
}
if (mp.isPlaying()) {
mp.pause();
startMedia.setText("play");
} else {
mp.start();
startMedia.setText("pause");
seekBar.setMax(mp.getDuration());
new Thread(this).start();
}
}

if (v.equals(stopMedia) && mp != null) {
if (mp.isPlaying() || mp.getDuration() > 0) {
mp.stop();
mp = null;
startMedia.setText("play");
seekBar.setProgress(0);
tv1.setText("0:00");
tv2.setText("0:00");
}
}

}

public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
try {
if (mp.isPlaying() || mp != null) {
if (fromUser)
mp.seekTo(progress);
} else if (mp == null) {
Toast.makeText(getApplicationContext(), "Media is not running",
Toast.LENGTH_SHORT).show();
seekBar.setProgress(0);

}
tv1.setText("" + milliSecondsToTimer(progress));
tv2.setText("" + milliSecondsToTimer(mp.getDuration()));
} catch (Exception e) {
Log.e("seek bar", "" + e);
seekBar.setEnabled(false);

}
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

public String milliSecondsToTimer(long milliseconds) {
String finalTimerString = "";
String secondsString = "";

// Convert total duration into time
int hours = (int) (milliseconds / (1000 * 60 * 60));
int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60);
int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000);
// Add hours if there
if (hours > 0) {
finalTimerString = hours + ":";
}

// Prepending 0 to seconds if it is one digit
if (seconds < 10) {
secondsString = "0" + seconds;
} else {
secondsString = "" + seconds;
}

finalTimerString = finalTimerString + minutes + ":" + secondsString;

// return timer string
return finalTimerString;
}
}

3.That's it  now run your Project and see the text view changes with the progress bar.
Thank you

Thursday, 12 September 2013

Saving Activity state in Android using Shared Preferences

How to save the activity instance using Shared Preferences


As you all know that there are many situations where we need to store the instance of the activity so that when the user returns to the application,the instance should remain the same.

The best example is whenever you play the game, the Highest score is changed when you create a new highest score. And whenever you return the game the score is the new high score.

So we'll learn the new concept of Shared Preferences i.e used to store the value in the application database till the application is uninstalled.

1.activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:inputType="text">

    </EditText>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_marginRight="19dp"
        android:layout_marginTop="38dp"
        android:text="TextView" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:text="Register the Score" />

</RelativeLayout>

2. MainActivity.java

package com.sri.score;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
EditText e1;
Button b;
TextView tv;
int i;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
e1=(EditText) findViewById(R.id.editText1);
b=(Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
   i = Integer.parseInt(e1.getText().toString());
   tv.setText("the score changed to " + i);
   Toast.makeText(getApplicationContext(), "score registered", Toast.LENGTH_SHORT).show();
}
});
if (savedInstanceState == null)
i = 0;
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
// Set the values of the UI
int j = preferences.getInt("i", i);
tv.setText("the score now is " + j);
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
// Put the values from the UI
editor.putInt("i", i);
editor.commit();
}

}


3.Run the application.
First your application value is 0, next your new score is registered. exit the application and open app once again to see the value remains as you exited previously.

Tuesday, 10 September 2013

How to create a Splash Screen?????

Splash Screen in Android


Now we'll learn to create Splash Screen.
Basically the Splash screen is showed to advertise or to load some heavy data previous to starting the Application.

The Example is shown below.

1.activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/splash" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Loading..."
        android:textSize="24sp" />

</RelativeLayout>

2. MainActivity.java

package com.sri.splash;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(), Second.class);
startActivity(i);
finish();
}
}, 3000);
}

}


Note:Create one new class Second.java and xml layout file second.xml.

3. Second.java

package com.sri.splash;

import android.app.Activity;
import android.os.Bundle;

public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}


4. second.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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello world"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>


5.AndroidManifest.xml

<activity
            android:name="com.sri.splash.MainActivity"
            android:label="@string/app_name" 
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.sri.splash.Second"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
</activity>

6. Don't Forget to add the splash.png image in res/drawable

Thank you.

Friday, 6 September 2013

Android Screen Dimension

How to get the Screen Dimension in Android Application


Hi, This is the way to easily get the screen dimensions of currently running android device.

1.activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/dimen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="screen dimensions" />

    <TextView
        android:id="@+id/res"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:layout_below="@+id/dimen"/>

</RelativeLayout>

2.MainActivity.java

package com.sri.screendimension;

import android.os.Bundle;
import android.view.Display;
import android.widget.TextView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Point;

public class MainActivity extends Activity {
TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Display disp = getWindowManager().getDefaultDisplay();
Point size = new Point();
disp.getSize(size);
int width = size.x;
int height = size.y;
tv = (TextView) findViewById(R.id.res);
tv.setText(width + "<----->" + height);
}

}

3.Run the Application.


Bottom Tab Activity

Adding a Tab at the Bottom in Android


This the way to add the bottom tab to your Application. Adding tabs by writting in program.


1.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" >
        </FrameLayout>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:tabStripEnabled="false" >
        </TabWidget>
    </LinearLayout>

</TabHost>

2. MainActivity.java

package com.sri.tabactivity;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost th=getTabHost();
TabSpec spec1=th.newTabSpec("tab1");
spec1.setIndicator("First Tab");
Intent i1=new Intent(getApplicationContext(), First.class);
spec1.setContent(i1);
TabSpec spec2=th.newTabSpec("tab2");
spec2.setIndicator("Second Tab");
Intent i2=new Intent(getApplicationContext(), Second.class);
spec2.setContent(i2);
TabSpec spec3=th.newTabSpec("tab3");
spec3.setIndicator("Third Tab");
Intent i3=new Intent(getApplicationContext(), Third.class);
spec3.setContent(i3);
th.addTab(spec1);
th.addTab(spec2);
th.addTab(spec3);
}

}

3. First.class

package com.sri.tabactivity;

import android.app.Activity;
import android.os.Bundle;

public class First extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
}
}

4. first.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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First Tab"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

5. Second.java

package com.sri.tabactivity;

import android.app.Activity;
import android.os.Bundle;

public class Second extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}

}

6.second.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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second Tab"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>


7.Third.java

package com.sri.tabactivity;

import android.app.Activity;
import android.os.Bundle;

public class Third extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
}
}

8.third.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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Third Tab"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

9.AndroidManifest.xml

       <activity android:name="com.example.tabactivity.First" />
        <activity android:name="com.example.tabactivity.Second" />
        <activity android:name="com.example.tabactivity.Third" />
Add it after </activity>

10.save it and run