Skip to main content

Android Tab Layout Application

Hello This Android Tab Layout Application.............. Enjoy it..............


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 -> Class

Music.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 :- 









Comments

  1. you can download above code ... by click on Download link.... in top.......

    Enjoy it...... All iz well

    ReplyDelete

Post a Comment

Popular posts from this blog

Login Application using Database with dialog demo

Follow Below Steps for make simple login applicaiton in Android using database with Dialog demo. 1)DataBaseDemoActivity.java This file diplay login page and trasfer two edit text data into second activity........  package database.co.in; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class DataBaseDemoActivity extends Activity implements OnClickListener {     /** Called when the activity is first created. */     EditText mtxt_email, mtxt_password;     Button mLogin; @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         mtxt_email = (EditText)findViewById(R.id.editEmail_id);   ...

Audio Recording in Android

Simple State Diagram for Media Recorder... Here Sequence of Method call is as per state diagram of media Recorder .... Otherwise Invalid sate call Error will be Occur........ Steps: 1) Also her two permission add in Android manifest file <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 2) This is java file coding  audiorecording.java package com.example.recording; import java.io.File; import java.io.FileDescriptor; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import ...

Android Activity

An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with  setContentView(View) . While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows (via a theme with  windowIsFloating  set) or embedded inside of another activity (using  ActivityGroup ). There are two methods almost all subclasses of Activity will implement: onCreate(Bundle)  is where you initialize your activity. Most importantly, here you will usually call  setContentView(int)  with a layout resource defining your UI, and using  findViewById(int)  to retrieve the widgets in that UI that you need to interact with programmatically. onPause()  is where you deal with the user leaving your activity. Most importantly, any changes made by the user should at this point...