1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.example.android.apis.app; 17 18 import com.example.android.apis.R; 19 20 import android.app.ActionBar; 21 import android.app.ActionBar.Tab; 22 import android.app.Activity; 23 import android.app.Fragment; 24 import android.app.FragmentTransaction; 25 import android.os.Bundle; 26 import android.view.LayoutInflater; 27 import android.view.View; 28 import android.view.ViewGroup; 29 import android.widget.TextView; 30 import android.widget.Toast; 31 32 /** 33 * This demonstrates the use of action bar tabs and how they interact 34 * with other action bar features. 35 */ 36 public class ActionBarTabs extends Activity { 37 @Override 38 protected void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 41 setContentView(R.layout.action_bar_tabs); 42 } 43 44 public void onAddTab(View v) { 45 final ActionBar bar = getActionBar(); 46 final int tabCount = bar.getTabCount(); 47 final String text = "Tab " + tabCount; 48 bar.addTab(bar.newTab() 49 .setText(text) 50 .setTabListener(new TabListener(new TabContentFragment(text)))); 51 } 52 53 public void onRemoveTab(View v) { 54 final ActionBar bar = getActionBar(); 55 if (bar.getTabCount() > 0) { 56 bar.removeTabAt(bar.getTabCount() - 1); 57 } 58 } 59 60 public void onToggleTabs(View v) { 61 final ActionBar bar = getActionBar(); 62 63 if (bar.getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS) { 64 bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 65 bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE); 66 } else { 67 bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 68 bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); 69 } 70 } 71 72 public void onRemoveAllTabs(View v) { 73 getActionBar().removeAllTabs(); 74 } 75 76 /** 77 * A TabListener receives event callbacks from the action bar as tabs 78 * are deselected, selected, and reselected. A FragmentTransaction 79 * is provided to each of these callbacks; if any operations are added 80 * to it, it will be committed at the end of the full tab switch operation. 81 * This lets tab switches be atomic without the app needing to track 82 * the interactions between different tabs. 83 * 84 * NOTE: This is a very simple implementation that does not retain 85 * fragment state of the non-visible tabs across activity instances. 86 * Look at the FragmentTabs example for how to do a more complete 87 * implementation. 88 */ 89 private class TabListener implements ActionBar.TabListener { 90 private TabContentFragment mFragment; 91 92 public TabListener(TabContentFragment fragment) { 93 mFragment = fragment; 94 } 95 96 public void onTabSelected(Tab tab, FragmentTransaction ft) { 97 ft.add(R.id.fragment_content, mFragment, mFragment.getText()); 98 } 99 100 public void onTabUnselected(Tab tab, FragmentTransaction ft) { 101 ft.remove(mFragment); 102 } 103 104 public void onTabReselected(Tab tab, FragmentTransaction ft) { 105 Toast.makeText(ActionBarTabs.this, "Reselected!", Toast.LENGTH_SHORT).show(); 106 } 107 108 } 109 110 private class TabContentFragment extends Fragment { 111 private String mText; 112 113 public TabContentFragment(String text) { 114 mText = text; 115 } 116 117 public String getText() { 118 return mText; 119 } 120 121 @Override 122 public View onCreateView(LayoutInflater inflater, ViewGroup container, 123 Bundle savedInstanceState) { 124 View fragView = inflater.inflate(R.layout.action_bar_tab_content, container, false); 125 126 TextView text = (TextView) fragView.findViewById(R.id.text); 127 text.setText(mText); 128 129 return fragView; 130 } 131 132 } 133 } 134