Hello This Android Tab Layout Application.............. Enjoy it..............
1) In this post we need three separate activities for three tab screens. So let’s get started by creating a simple project by opening eclipse IDE. Create a new project File -> New -> Android Project and give activity name as MainActivity.
2) Now open your activity_main.xml under res -> layout folder and type the following code.
<?xml version="1.0" encoding="utf-8"?>
Download This Code Click here
1) In this post we need three separate activities for three tab screens. So let’s get started by creating a simple project by opening eclipse IDE. Create a new project File -> New -> Android Project and give activity name as MainActivity.
2) Now open your activity_main.xml under res -> layout folder and type the following code.
<?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"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
3) Now we need 3 activities and 3 xml layouts for three tabs. So create three activities by right click on your package folder and create classes and name them asMusic.java,Photo.javaand video.java. Type the following code respectively.
Right Click on package folder -> New -> ClassMusic.java
import android.app.Activity;
import android.os.Bundle;
//O@K Tech
public class Music extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.music_layout);
}
}
Photo.java
import android.app.Activity;
import android.os.Bundle;
//O@K Tech
public class Photo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.photo_layout);
}
}
video.java
import android.app.Activity;
import android.os.Bundle;
public class video extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.video);
}
}
4) Now create 3 xml layouts by right clicking on res/layout -> New -> Android XML File and name them as photo_layout.xml, music_layout.xml and video.xml and type the following code in respective files.
music_layout.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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play Music.... "
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
photo_layout.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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello Photo view"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
video.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Video................"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
5) Now open MainActivity.java and type the following code. In the following code we are creating three TabSepcs and adding them to TabHost.
MainActivity.java
package com.example.tagdemo;
import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
// O@K Tech......
public class MainActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("helloo");
@SuppressWarnings("deprecation")
TabHost tbh = getTabHost();
TabSpec tabspecmusic = tbh.newTabSpec("Music");
tabspecmusic.setIndicator("Music",getResources().getDrawable(R.drawable.ic_launcher));
Intent imusic = new Intent(this, Music.class);
tabspecmusic.setContent(imusic);
TabSpec tabspecphoto = tbh.newTabSpec("Photo");
tabspecphoto.setIndicator("Photo",getResources().getDrawable(R.drawable.ic_action_search));
Intent iphoto = new Intent(this,Photo.class);
tabspecphoto.setContent(iphoto);
TabSpec tabspecv = tbh.newTabSpec("video");
tabspecv.setIndicator("video",getResources().getDrawable(R.drawable.ic_action_search));
Intent iv = new Intent(this,video.class);
tabspecv.setContent(iv);
tbh.addTab(tabspecmusic);
tbh.addTab(tabspecphoto);
tbh.addTab(tabspecv);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
6) Now everything is ready and before running your project make sure that you an entry of new activity name in AndroidManifest.xml file. Open you AndroidManifest.xml file and modify the code as below
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tagdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Music"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Photo"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".video"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Output of Above code in Emulator :-
you can download above code ... by click on Download link.... in top.......
ReplyDeleteEnjoy it...... All iz well