Implement switch tabs label ;
Activity code:
public class Actionbartabs extends Activity {
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- Super.oncreate (savedinstancestate);
- Setcontentview (R.layout.action_bar_tabs);
- }
- Public void Onaddtab (View v) {
- Final ActionBar bar = Getactionbar ();
- Final int tabcount = Bar.gettabcount ();
- Final String Text = "tab" + Tabcount;
- Bar.addtab (Bar.newtab (). SetText (text)
- . Settablistener (new Tablistener (new Tabcontentfragment (text)));
- }
- Public void Onremovetab (View v) {
- Final ActionBar bar = Getactionbar ();
- Bar.removetabat (Bar.gettabcount ()- 1);
- }
- Public void Ontoggletabs (View v) {
- Final ActionBar bar = Getactionbar ();
- if (bar.getnavigationmode () = = Actionbar.navigation_mode_tabs) {
- Bar.setnavigationmode (Actionbar.navigation_mode_standard);
- Bar.setdisplayoptions (Actionbar.display_show_title, actionbar.display_show_title);
- } Else {
- Bar.setnavigationmode (Actionbar.navigation_mode_tabs);
- Bar.setdisplayoptions (0, Actionbar.display_show_title);
- }
- }
- Public void Onremovealltabs (View v) {
- Getactionbar (). Removealltabs ();
- }
- Private class Tablistener implements Actionbar.tablistener {
- Private Tabcontentfragment mfragment;
- Public Tablistener (Tabcontentfragment fragment) {
- Mfragment = fragment;
- }
- Public void ontabselected (Tab tab, fragmenttransaction ft) {
- Ft.add (R.id.fragment_content, Mfragment, Mfragment.gettext ());
- }
- Public void ontabunselected (Tab tab, fragmenttransaction ft) {
- Ft.remove (mfragment);
- }
- Public void ontabreselected (Tab tab, fragmenttransaction ft) {
- Toast.maketext (actionbartabs. This, "reselected!", Toast.length_short). Show ();
- }
- }
- Private class Tabcontentfragment extends Fragment {
- Private String MText;
- Public tabcontentfragment (String text) {
- MText = text;
- }
- Public String GetText () {
- return mText;
- }
- @Override
- Public View Oncreateview (layoutinflater inflater, ViewGroup container,
- Bundle savedinstancestate) {
- View Fragview = inflater.inflate (r.layout.action_bar_tab_content, container, false);
- TextView Text = (TextView) Fragview.findviewbyid (R.id.text);
- Text.settext (MText);
- return fragview;
- }
- }
- }
The Action_bar_tabs.xml code for the layout file involved is:
< XML version="1.0" encoding="Utf-8"?>
- < LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android"
- Android:layout_width="Match_parent"
- android:layout_height="Match_parent"
- android:orientation="vertical" >
- < framelayout android:id="@+id/fragment_content"
- Android:layout_width="Match_parent"
- android:layout_height="0dip"
- android:layout_weight="1"/>
- < LinearLayout android:layout_width="Match_parent"
- android:layout_height="0dip"
- android:layout_weight="1"
- android:orientation="vertical" >
- < button android:id="@+id/btn_add_tab"
- Android:layout_width="Wrap_content"
- android:layout_height="Wrap_content"
- android:text="@string/btn_add_tab"
- android:onclick="Onaddtab"/>
- < button android:id="@+id/btn_remove_tab"
- Android:layout_width="Wrap_content"
- android:layout_height="Wrap_content"
- android:text="@string/btn_remove_tab"
- android:onclick="Onremovetab"/>
- < button android:id="@+id/btn_toggle_tabs"
- Android:layout_width="Wrap_content"
- android:layout_height="Wrap_content"
- android:text="@string/btn_toggle_tabs"
- android:onclick="Ontoggletabs"/>
- < button android:id="@+id/btn_remove_all_tabs"
- Android:layout_width="Wrap_content"
- android:layout_height="Wrap_content"
- android:text="@string/btn_remove_all_tabs"
- android:onclick="Onremovealltabs"/>
- </linearlayout>
- </linearlayout>
Layout file action_bar_tab_content.xml;
< XML version= "1.0" encoding= "utf-8"?>
- < TextView xmlns:android="Http://schemas.android.com/apk/res/android"
- android:id="@+id/text"
- Android:layout_width="Wrap_content"
- android:layout_height="Wrap_content"/>
Android Actionbar Details (iii): Actionbar implementation Switch Tabs label