Wednesday, 26 June 2013

Android ActionBar Tab Example 1

Here in This example i'm going to show how to display Tab's in ActionBar.

Project Name: ActionBarWithTab
Java files       : MainActivity.java


ScreenShots:
----------------

MainActivity.java:
package com.ram.actionbarwithtab;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.Toast;

@SuppressLint("NewApi")
public class MainActivity extends Activity implements TabListener {
Tab tab1, tab2, tab3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

tab1 = actionBar.newTab();
tab1.setText("camera");
tab1.setTabListener(this);
actionBar.addTab(tab1);

tab2 = actionBar.newTab();
tab2.setText("contacts");
tab2.setTabListener(this);
actionBar.addTab(tab2);

tab3 = actionBar.newTab();
tab3.setText("gallery");
tab3.setTabListener(this);
actionBar.addTab(tab3);

}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
switch (tab.getPosition()) {
case 0:

Toast.makeText(getApplicationContext(), "tab1 selected",
Toast.LENGTH_LONG).show();
break;

case 1:
Toast.makeText(getApplicationContext(), "tab2 selected",
Toast.LENGTH_LONG).show();
break;

case 2:
Toast.makeText(getApplicationContext(), "tab3 selected",
Toast.LENGTH_LONG).show();
break;
}

}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub

}
}