Android Tabs with Fragments

Evelina Vrabie

Dedicated coder, occasional blogger, hackathon goer and friendly team mate.

You may also read...

112 Responses

  1. title says:

    I read this article completely about the resemblance of
    latest and previous technologies, it’s remarkable article.

    my web-site … title

  2. I’m gone to convey my little brother, that he should also visit this web
    site on regular basis to take updated from most up-to-date news update.

  3. Sandy says:

    hello there and thank you for your information – I’ve definitely picked up anything
    new from right here. I did however expertise some technical issues
    using this website, since I experienced to reload the website
    many times previous to I could get it to load correctly.
    I had been wondering if your web host is OK?
    Not that I’m complaining, but slow loading instances times will very
    frequently affect your placement in google and can damage your high
    quality score if advertising and marketing with Adwords.

    Well I’m adding this RSS to my email and can look out for much more of your respective interesting content.

    Ensure that you update this again very soon.

  4. Bernard says:

    Good replies in return of this query with genuine arguments
    and telling all about that.

    Take a look at my homepage – vegasodj.soup.io (Bernard)

  5. Asking questions are actually good thing if you are not understanding anything entirely, but this paragraph provides good understanding even.

  6. Shikha Jha says:

    Hi,
    Thanks for the code. I need some help, i want to ask how to spread out the tabs to take the width of whole screen.Please help me.What kind of code i need to add in it. Please help me. Please reply as early as possible . Thank you.

  7. What i do not realize is actually how you are not really much more well-preferred than you may be right now.
    You’re very intelligent. You recognize therefore significantly
    in relation to this topic, made me for my part believe it from
    so many varied angles. Its like women and men aren’t interested until it’s one thing to do with Woman gaga!

    Your personal stuffs outstanding. At all times take care of
    it up!

    Feel free to visit my web-site: behind the teeth braces Elk Grove

  8. Hello, every time i used to check blog posts here in the early hours in the daylight, because i enjoy to learn more and more.

    My web-site – dental implants cost Elk Grove

  9. You really make it seem so easy with your presentation but I find this
    matter to be actually something which I think I would never understand.
    It seems too complex and extremely broad for me.
    I’m looking forward for your next post, I’ll try to get the hang of it!

    Look into my web-site Raccoon Removal Barrie

  10. Neville says:

    What a data of un-ambiguity and preserveness of precious
    knowledge concerning unpredicted feelings.

    Feel free too surf to my homepage: Android Apps PC [Neville]

  11. Sandy says:

    Excellent beat ! I would like to apprentice while you amend your web site,
    how could i subscribe for a blog web site (Sandy)?

    The account aided me a acceptable deal. I had been tiny bit acquainted of this your broadcast offered bright clear idea

  12. make money says:

    Undeniably imagine that which you stated. Your favorite reason appeared to be on the web the simplest
    thing to take note of. I say to you, I certainly get annoyed while people think about worries that they just do not recognize
    about. You controlled to hit the nail upon the top as smartly as outlined out
    the entire thing with no need side effect , folks
    could take a signal. Will probably be again to get more.
    Thanks

    Review my blog post … make money

  13. Awesome website you have here but I was wondering if you knew of any message boards that
    cover the same topics talked about in this article?
    I’d really love to be a part of community where
    I can get suggestions from other knowledgeable individuals that share the same
    interest. If you have any suggestions, please let me know.
    Many thanks!

    my web blog – 24 hour heating and cooling Dothan AL

  14. I think what you wrote was very logical.
    But, think on this, suppose you composed a catchier post title?
    I mean, I don’t wish to tell you how to run your website, but what if you added a post title that grabbed people’s
    attention? I mean Android Tabs with Fragments – Android Magic is kinda vanilla.
    You could peek at Yahoo’s home page and note how they create news headlines to get
    viewers interested. You might try adding a video or a pic or
    two to get readers interested about what you’ve written. Just my opinion, it might make your website a little bit
    more interesting.

    My blog post garage door torsion spring repair Minneapolis MN

  15. You really make it seem so easy with your presentation but I find this matter to be really something which I think I would never understand.
    It seems too complex and very broad for me. I’m looking forward for your next post,
    I’ll try to get the hang of it!

    My site – palm tree removal Birmingham AL

  16. Lelia says:

    The motor is responsible for producing quiet operation while
    driving the blades for air movement. Sometimes there are
    many hidden costs with electrical work, and it is
    easy to get taken for a ride. When picking out the fan, you have to be
    quite watchful.

  17. We stumbled over here coming from a different web page and thought I might check things out.
    I like what I see so now i am following you. Look forward to
    looking at your web page again.

  18. Victorina says:

    Snakes symbolize the eternal revolution of the ages and the succession, dissolution and regeneration of humanity.
    He had the guardsman’s crossbow to his shoulder, aiming a newly loaded bolt across the room.
    Should you’re feeling like you focused your enemy spot on in a shot but still missed, check what arms you are using.

  19. Thanks designed for sharing such a pleasant idea, post is pleasant, thats why i have read it fully

  20. Louisa says:

    I agree with you on many of your original ideas here. You’ve done a great job of
    making this very interesting and clear. Thank you.

    Feel free to visit my web-site

  21. wasim says:

    Confused with the code. you have explained well.
    And how you have used those red and green color for rows ?
    I like the style …

  22. Excellent post. I certainly love this website. Keep writing!

  23. best hack says:

    Everyone loves what you guys are usually up too. This kind of clever work and
    exposure! Keep up the awesome works guys I’ve you guys to my
    own blogroll.

  24. First off I want to say awesome blog! I had a quick question that I’d like
    to ask if you don’t mind. I was interested to find out how you center yourself and clear
    your head before writing. I’ve had trouble clearing my mind in getting my thoughts
    out there. I truly do enjoy writing but it just seems like the first 10
    to 15 minutes are generally wasted just trying to figure out how to begin.
    Any ideas or hints? Thank you!

  25. Willem Ghijsen says:

    Nevermind. I played around with it, and the strings are called from strings.xml. I originally thought it was in the java code:
    public static final String TAB_WORDS = “words”;
    public static final String TAB_NUMBERS = “numbers”;
    but that was incorrect

  26. Willem Ghijsen says:

    I was going through the code, and added an extra tab to experiment. This was successful, however, I cannot seem to find out how/why the tabs start with a capital letter, since the strings passed in are all lowercase. Does anyone know why this happens?

  27. thx for this tutorial !but i have a question !!
    how to hide the titles of the tabs !
    need your help !

  28. These are in fact enormous ideas in on the topic of blogging.

    You have touched some nice factors here. Any way keep up wrinting.

  29. k40s says:

    Thanks a lot for this

  30. John says:

    Very useful. Thanks

  31. bahaha says:

    Can you please post the entire source code? Where is r.id.bar defined, new to android here.

  32. vkhooda says:

    Well Blog post…By the developer for the developers….:)
    Thanks for sharing good tutorial about tab in fragments.

  33. Tom says:

    Brilliant! I was looking for use of custom tabs with fragments because I wanted to use the new Navigation Drawer! Only tutorial I can find that does what I want and I’ve been looking for 3 days! A+++!

  34. Zeus says:

    Can u pls suggest as to how can i add an onclick listener to one of the tab to open a new activity or fragment inside the same tab

  35. Zeus says:

    What if i wanted to start a child activity form one of the tabs then would the tabs still be maintained on that child

    activity as well

  36. Ankit Rohilla says:

    thank you for great tutorial
    Its working

  37. Babar says:

    Hi How can i have tabs at bottom??

  38. Shakir Orakzai says:

    wonderfull very nice

  39. arw357 says:

    Thx . usefull indeed !

  40. rashid says:

    How do i implement this using ListView instead of Creating a list of LinearLayouts? Pls help.

  41. mesi says:

    what is R.id.bar ? pls mention

  42. Jim_Welke says:

    Right on, Evalina! Great code sample; it clarified a lot things I (an Android newbie) was struggling with. Specifically, how to structure the .xml files so that configuration info and selectors work right. I appreciate the clarity in your comments, too. Thanks. Peace.

  43. Authman Apatira says:

    This project (FragmentTabs) crashes when you rotate the device 4x.

  44. dereky says:

    it really helps me a lot,thanks very much

  45. Dusan says:

    OMFG! Does anybody know why i cant set weight atribut on root LinearLayout in tabs.xml. I tryed a lot of thing, but it wont work… I want tabs to spread out to take whole screen…

  46. Dim says:

    Is it simple enough to add each fragment to the backstack such that hitting the back button returns to the previous tab?

  47. nate says:

    If you wanted to make one of the fragments a mapview how would you go about do that?

  48. Everton Barros says:

    Very nice post, I needed to do something similar, only in my case I needed to put tabs in a popup, after picking up a bit, I managed to reach the final solution. Thank you!

  49. rathael says:

    I think you must change “getListView().setAdapter(mAdapter);” by “setListAdapter(mAdapter);”.

  50. jin says:

    Hi, This tutorial really helped me a lot. I want to make a third tab, but unable to code it. Pl help for the same.

  51. CH4:D says:

    Line 106 is wrong. It should just simply be
    if (mPosition < mTotal).

    As it stands, the last element in the array won't show up if you add the "- 1" because it stops one item behind.

  52. Alex says:

    this is crashing on second orientation change

  53. binhbt says:

    Oh man. Thanks a lot. nice example :X:X:X:X:X:X:X:X::-*

  54. zx81 says:

    Hi, Thanx for nice example, saves lot of learning time.

    BTW, what is R.layout.tab in newTab() and how can I create a proper XML for that element?

  55. seetha says:

    you are superb dear….

  56. Filip Bulovic says:

    Very nice example, thanks.

  57. mith says:

    Hi. Thanks for the wonderful code and tutorial. I have a question though. How could I make the tabs scrollable as in if I add many tabs , how to make it scrollable horizontally ?
    Thanks

    • mith says:

      A quick google search helped me.. This is how I did

      Hope this helps somebody.
      Thanks

    • evelyne24 says:

      Hey, thanks for the comment. My code has become a bit obsolete because there are other people out there creating compatibility libraries to support all kinds of ICS features, including tabs, tabs + swipe, etc. Best look for Jake Wharton’s libraries (he’s the author of ActionBarSherlock). Check out this library: https://github.com/JakeWharton/Android-ViewPagerIndicator/ it probably has everything you want.

      • Thomas Markle says:

        You should add an update at the top of the post, I just found the ABSherlock library and it looks like it’s a great time saver.

  58. nicolas raby says:

    Hi, android.support.v4.app.FragmentActivity , does I mean the code requires Android v4 at least ?

  59. kyk says:

    Thanks for posting a working project!

  60. Gladiatornoida says:

    I found it in your code. Thanks!

  61. Gladiatornoida says:

    private TabSpec newTab(String tag, int labelId, int tabContentId) {
    Log.d(TAG, “buildTab(): tag=” + tag);

    View indicator = LayoutInflater.from(getActivity()).inflate(
    R.id.tab,
    (ViewGroup) mRoot.findViewById(android.R.id.tabs), false);
    ((TextView) indicator.findViewById(R.id.text)).setText(labelId);

    TabSpec tabSpec = mTabHost.newTabSpec(tag);
    tabSpec.setIndicator(indicator);
    tabSpec.setContent(tabContentId);
    return tabSpec;
    }

    Can you please give explanation for R.id.tab & indicator.findViewById(R.id.text). From where these ids come?

    • Kyle Beckman says:

      I have the same question…. any help?

      • Nelson says:

        I’m not sure about R.id.tab, but I think you actually mean android.R.id.tabs, but R.id.text is just the id of a TextView defined in the list_item.xml file. She’s not talking about it in the text, but if you download the tar file with the whole project there you can see that file, which is actually the layout for every single row displayed in the listview.

        • Nico Sordoni says:

          R.id.tab doesn’t appear anywhere, but instead appear R.layout.tab, that is the layout used as “background” for the indicators of the tabs. R.id.text is a TextView inside this layout, used to contain the text of the tab’s indicator.

  62. Tom Karni says:

    This is a greate tutorial, helped me a lot.

  63. samcool says:

    Is there an article only for fragments ? I searched around for lot of tutorials and tried implementing by following them but couldn’t get it working. I am looking for fragment implementation which will work on all the android versions. That will be really helpful. Thank you in advance.

  64. Zerotvt says:

    Thanks

  65. Dada Venki says:

    hi i have read your tutorial its nice but i have a doubt if in any tab if i want to move another screen (same tab) like clicking any of the rows in listview how it can be done any suggestion will be a great help

  66. ye009 says:

    Can you post some code for listFragment with which used customlistAdapter in setAdapter and listfragment loads data from asychTaskLoader, I am having problem with how to refresh the listfragment when data is loaded from asynchTaskLoader. Please help

  67. Vinesh Gaadhe says:

    Ohhh man u rocks… I was looking for this from last 24 hours…thanks a lot man

  68. Cloud Jacker says:

    Thanks! I dont understand what your activity is doing though, why do the individual tabs load the words and numbers so slowly? Is that a fragment feature?

    I need to use tabs to load full layouts. Ie tab 1 loads an activity and tab 2 loads a separate activity! But TabActivity and that entire methodology is deprecated, yet this fragment “replacement” isn’t very intuitive for froyo+

    • Evelina Vrabie says:

      Have you read the code? The words are loaded slowly because I wanted to simulate an operation that takes time, like a network call. If you actually read the code you’ll see there’s a Thread.sleep() that does the trick (there’s a comment that says that as well).

      About the methodology, if you think it’s deprecated then use something else that you see fit. If you find a better way, please share so we can all learn

  69. Abe Froman says:

    When I try running the code you provided I just get a loading icon that never goes away. The done loading string appears in my LogCat but nothing is appearing on the screen. If you have any ideas what I may have done wrong please let me know.

    • Brill Pappin says:

      Run it through the debugger, that’s that it’s for.
      I had to modify it as well for my project, but it works just fine.

      • Tom Karni says:

        I had the same problem when I used it in my project but not when I ran this package. Brill, how did you solve it?

    • U Sargin says:

      i have used android-support-v13 no v4 and i have same problem

      • Tom Karni says:

        I finally solved it with:
        @Override
        public void onLoadFinished(Loader loader, Void result) {
        //update adapter

        setListShown(true);
        }

        • CH4:D says:

          God, this really solved my issue as well! I’ve been up all night trying to customize this tutorial to suit my needs but even simple label and id adjustments screw this app up.

          I don’t know if this is an android or eclipse bug but i tried tracking the changes little by little and rerunning at each change.

          It appears that if you add one extra tab to the tabs_fragment.xml the whole application gets foobar’d. However, this may not be the exact cause but probably a consequence because the issue is intermittent.

          Sometimes the loading screen shows up even if you undo the changes and uninstall the app. Sometimes it just corrects itself after a few runs.

        • rathael says:

          Try to change “getListView().setAdapter(mAdapter);” by “setListAdapter(mAdapter);”.

  70. Brill Pappin says:

    Here is anther tip. The tab in your example are left aligned.
    if you add the android:layout_weight=”1″ attribute to the root LinierLayout of tabs.xml they will spread out to take up the whole width of the screen.

    Long labels will still cause some tabs to be longer than others, but that would be an issue no matter what you did.

    It was also easy to add an image to the tabs.xml so that you get a more “iphone” like effect.

  71. Brill Pappin says:

    Best example of Tabs with Fragments i’ve seen yet. A simple as can be and the code is clean and obvious.

  72. tabbykitten says:

    Thanks for tips
    tab_1, tab_2 is overhead for tabhost_fragment.xml is overhead, you can use android.R.id.tabcontent directly

  73. Vinh Bui says:

    Thanks a lot, it’s really useful to me.

  74. peng wang says:

    thanks,it is help me .

  75. Evelina Vrabie says:

    Glad to help

  76. Jalota_arun says:

    Thnx alot…this exact i have been lokking for last 3 days…!!gr8 post thnk u!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>