Friday 6 September 2013

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

No comments:

Post a Comment