I am trying to implement Tab Navigation, but I want to make sure people that have older versions of Android can still use my application.
The app in mind ATM is fai
The key is to use
import android.support.v7.app.ActionBar;
... rather than ...
import android.app.ActionBar;
That avoids the clever workaround Nelson Ramirez posted.
The following full example, based on the official documentation, was tested to work from Android 3.0, API 11
package com.example.myapp;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.*;
import android.widget.TextView;
public class NavigationTabsBasicDemoActivity extends ActionBarActivity {
static public class TabListener implements ActionBar
.TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class mClass;
/**
* Constructor used each time a new tab is created.
*
* @param activity The host Activity, used to instantiate the
* fragment
* @param tag The identifier tag for the fragment
* @param pClass The fragment's Class, used to instantiate the
* fragment
* @see
* Developers Guide > Action Bar > Adding Navigation Tabs
*/
public TabListener(Activity activity, String tag, Class pClass) {
mActivity = activity;
mTag = tag;
mClass = pClass;
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
if (mFragment == null) {
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, attach it in order to show it
ft.attach(mFragment);
}
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is about to be
// attached.
ft.detach(mFragment);
}
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// Do nothing.
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// No need for setContentView() to be used, Instead we use the root
// android.R.id.content as the container for each fragment,
// which is set in the TabListener
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(true);
ActionBar.Tab tab = actionBar.newTab().setText("Artist").setTabListener(
new TabListener(this,
"artist",
PlaceholderFragment
.class));
actionBar.addTab(tab);
tab = actionBar.newTab().setText("Album").setTabListener(
new TabListener(
this,
"album",
PlaceholderFragment.class));
actionBar.addTab(tab);
}
/**
* In this example use one Fragment but display different data based on
* which
* tab is shown. In production you'd probably use a separate fragment.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment_navigation_tabs_basic_demo,
container,
false);
TextView outputTextView = (TextView) rootView.findViewById(
R.id.output_textView);
outputTextView.setText("Hello " + getTag());
return rootView;
}
}
}