Below is source code of how to implement TabView in Android.
Note: It has been verified in Android emulator 4.0.3.
activity_tab_example.xml
<TabHost 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:id="@android:id/tabhost" >
   
<LinearLayout
android:id="@+id/tablinearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
       
<TabWidget
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TabWidget>
       
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></FrameLayout>
</LinearLayout>
   
</TabHost>
 
 
        
         
        
         
         
         
         
         
         
Note: It has been verified in Android emulator 4.0.3.
activity_tab_example.xml
<TabHost 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:id="@android:id/tabhost" >
<LinearLayout
android:id="@+id/tablinearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></FrameLayout>
</LinearLayout>
</TabHost>
strings.xml
<resources>
    <string name="app_name">TabExample</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_tab_example">Tab Example</string>
    <string name="title_activity_plus_tab">PlusTabActivity</string>
    <string name="title_activity_minus_tab">MinusTabActivity</string>
    <string name="title_activity_multiplication_tab">MultiplicationTabActivity</string>
    <string name="title_activity_division_tab">DivisionTabActivity</string>
    <string name="plus">You clicked on Plus.</string>
    <string name="minus">You clicked on Minus.</string>
    <string name="division">You clicked on Division.</string>
    <string name="multiplication">You clicked on Multiplication.</string>
</resources>
TabExample.java
package com.example.tabexample;
import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.util.Log;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
@SuppressWarnings("deprecation")
public class TabExample extends TabActivity {
 private TabHost oTabHost;
    @SuppressWarnings("null")
 @Override
    public void onCreate(Bundle savedInstanceState) {
     try
     {
      super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_tab_example);
         oTabHost = (TabHost) findViewById(android.R.id.tabhost);
         oTabHost = getTabHost();
         TabHost.TabSpec oTabSpec1 ;
         oTabSpec1 = oTabHost.newTabSpec("+");
         oTabSpec1.setIndicator("+", getResources().getDrawable(R.drawable.sq_plus));
         Intent oPlusIntent = new Intent(this, PlusTabActivity.class);
         oTabSpec1.setContent(oPlusIntent);
         oTabHost.addTab(oTabSpec1);
         TabHost.TabSpec oTabSpec2 = oTabHost.newTabSpec("-");
         oTabSpec2.setIndicator("-", getResources().getDrawable(R.drawable.sq_minus));         
         Intent oMinusIntent = new Intent(this, MinusTabActivity.class);
         oTabSpec2.setContent(oMinusIntent);
         oTabHost.addTab(oTabSpec2);
         TabHost.TabSpec oTabSpec3 = oTabHost.newTabSpec("*");
         oTabSpec3.setIndicator("*", getResources().getDrawable(R.drawable.multiply));         
         Intent oMultiplicationIntent = new Intent(this, MultiplicationTabActivity.class);
         oTabSpec3.setContent(oMultiplicationIntent);
         oTabHost.addTab(oTabSpec3);
         TabHost.TabSpec oTabSpec4 = oTabHost.newTabSpec("/");
         oTabSpec4.setIndicator("/", getResources().getDrawable(R.drawable.sign_direction));
         Intent oDivisionIntent = new Intent(this, DivisionTabActivity.class);
         oTabSpec4.setContent(oDivisionIntent);
         oTabHost.addTab(oTabSpec4);
     }
     catch(Exception e){
      Log.d("TabExample", e.getMessage());
     }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_tab_example, menu);
        return true;
    }
}
Snapshot of application structure. I have added 4 layout files and 4 images files in drawable folder.
Result:
In below example, you will get only text in each tab. if you want images then you need to keep first parameter of setIndicator method of each tab blank.


 
No comments:
Post a Comment