How to replace a Fragment in an ActionBar in mode NAVIGATION_MODE_TABS

In the post Tab Layout in Android with ActionBar and Fragment I wrote an example about tabs in an action bar where each label open a fragment that you can not change at runtime.
In this post I explain how to replace a fragment with an other using a button.

  1. follow every step of the post Tab Layout in Android with ActionBar and Fragment
  2. add the following rows to the file values/strings.xml
  3. insert a button in the linear layout in the file res/layout/tab1.xml
  4. create the file layout/tab1b.xml
  5. edit the file eu/lucazanini/

    the button adds the fragment with the tag “bis” and layout tab1b.xml and hides the fragment with tag “one” and layout tab1.xml
  6. create the file eu/lucazanini/
  7. replace the following methods of thew inner class TabListener in the file eu/lucazanini/
    • onTabSelected
    • onTabReselected

    if you select or reselect a tab and if the fragment with tag “bis” exists then this fragment is removed and it is shown that with tag “one”

  8. launch the app
    • tab “ONE” selected

    • tab “ONE” selected + button

    • tab “TWO” selected


 Add your comment
  1. How to change the uppercase letter (ONE) to lowercase letter (one) ???

  2. Very nice. 😀


Leave a Comment

Your email address will not be published.