Home | History | Annotate | Download | only in app
      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         bar.removeTabAt(bar.getTabCount() - 1);
     56     }
     57 
     58     public void onToggleTabs(View v) {
     59         final ActionBar bar = getActionBar();
     60 
     61         if (bar.getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS) {
     62             bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
     63             bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE);
     64         } else {
     65             bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
     66             bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
     67         }
     68     }
     69 
     70     public void onRemoveAllTabs(View v) {
     71         getActionBar().removeAllTabs();
     72     }
     73 
     74     /**
     75      * A TabListener receives event callbacks from the action bar as tabs
     76      * are deselected, selected, and reselected. A FragmentTransaction
     77      * is provided to each of these callbacks; if any operations are added
     78      * to it, it will be committed at the end of the full tab switch operation.
     79      * This lets tab switches be atomic without the app needing to track
     80      * the interactions between different tabs.
     81      *
     82      * NOTE: This is a very simple implementation that does not retain
     83      * fragment state of the non-visible tabs across activity instances.
     84      * Look at the FragmentTabs example for how to do a more complete
     85      * implementation.
     86      */
     87     private class TabListener implements ActionBar.TabListener {
     88         private TabContentFragment mFragment;
     89 
     90         public TabListener(TabContentFragment fragment) {
     91             mFragment = fragment;
     92         }
     93 
     94         public void onTabSelected(Tab tab, FragmentTransaction ft) {
     95             ft.add(R.id.fragment_content, mFragment, mFragment.getText());
     96         }
     97 
     98         public void onTabUnselected(Tab tab, FragmentTransaction ft) {
     99             ft.remove(mFragment);
    100         }
    101 
    102         public void onTabReselected(Tab tab, FragmentTransaction ft) {
    103             Toast.makeText(ActionBarTabs.this, "Reselected!", Toast.LENGTH_SHORT).show();
    104         }
    105 
    106     }
    107 
    108     private class TabContentFragment extends Fragment {
    109         private String mText;
    110 
    111         public TabContentFragment(String text) {
    112             mText = text;
    113         }
    114 
    115         public String getText() {
    116             return mText;
    117         }
    118 
    119         @Override
    120         public View onCreateView(LayoutInflater inflater, ViewGroup container,
    121                 Bundle savedInstanceState) {
    122             View fragView = inflater.inflate(R.layout.action_bar_tab_content, container, false);
    123 
    124             TextView text = (TextView) fragView.findViewById(R.id.text);
    125             text.setText(mText);
    126 
    127             return fragView;
    128         }
    129 
    130     }
    131 }
    132