• 09sept

    Utiliser des onglets dans ses applications android



    Dans ce tuto je vais utiliser des onglets dans mon application. Pour utiliser des onglets dans une application android, la classe principale doit être une extension de la classe TabActivity

    1
    
    public class AppTab extends TabActivity

    Dans le fichier xml de l’interface principale la première balise et la balise TabHost

     

    Je crée un objet de type TabHost et avec ma méthode getTabHost(), j’affecte sur cet objet mon interface xml.

    1
    2
    3
    4
    5
    6
    7
    8
    
    public class AppTab extends TabActivity  {
        /** Called when the activity is first created. */
        private TabHost mTabHost;
    	@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
            mTabHost = getTabHost();

    la méthode addTab de l’objet mTabHost , permet d’ajouter des onglets, avec comme paramètre lle nom de l’onglet,la vue sur laquelle cet onglet pointe et son identifiant

    1
    2
    3
    
    mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Onglet 1").setContent(R.id.textview1));
            mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("Onglet 2").setContent(R.id.textview2));
            mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("Onglet 3").setContent(R.id.textview3));

    la méthode setCurrentTab permet de définir l’onglet par défaut à l’ouverture de l’application

       mTabHost.setCurrentTab(0);

    code complet

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    package org.com.apptab;
     
    import android.app.TabActivity;
    import android.os.Bundle;
    import android.view.Window;
    import android.widget.TabHost;
     
    public class AppTab extends TabActivity  {
        /** Called when the activity is first created. */
        private TabHost mTabHost;
    	@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
            mTabHost = getTabHost();
     
            mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Onglet 1").setContent(R.id.textview1));
            mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("Onglet 2").setContent(R.id.textview2));
            mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("Onglet 3").setContent(R.id.textview3));
     
            mTabHost.setCurrentTab(0);
        }
    }

    onglettuto1 208x300 Utiliser des onglets dans ses applications android Android France onglettuto2 199x300 Utiliser des onglets dans ses applications android Android France


    Formation Android en vidéo

    Guy

    Co-fondateur du site Android france, senior lead developper, passionné de bière et de cigare cubain

    Twitter Google+ 

31 commentaires

  1. 15 février 2010 à 21 h 21 min

    Bonjour,

    J’essaye de reproduire ce tuto mais je n’y arrive pas (Erreur lors du lancement de l’appli) Dans le flux xml, la première balise n’est pas le LinearLayout ” main ” ?

    Serait il possible de voir le fichier xml svp ?

    Merci d’avance

  2. 17 février 2010 à 18 h 04 min

    Je pense aussi que c’est le fichier xml qui manque…

  3. 17 février 2010 à 18 h 13 min

    @Thierry je fouille dans mes archives pour voir si je le retrouve ce fichier et le met en ligne

  4. 18 mai 2011 à 12 h 04 min

    Please link my web

  5. 16 novembre 2011 à 21 h 02 min

    שלום, רציתי להמליץ לכם על חברה העוסקת בהתקנה ובניה של פרגולות. באתר, פרופיל החברה וכן קולקציות סוככים נבחרות.

  6. 30 novembre 2011 à 17 h 19 min

    Only wanna comment that you have a very nice website , I enjoy the layout it actually stands out.

  7. 13 décembre 2011 à 12 h 21 min

    Hello, i think that i noticed you visited my site thus i got here to go back the desire?.I am trying to find things to improve my site!I assume its adequate to make use of a few of your concepts!!

  8. 24 décembre 2011 à 13 h 38 min

    Hello my friend! I want to say that this article is amazing, nice written and include almost all important infos. I’d like to peer extra posts like this .

  9. 5 janvier 2012 à 5 h 47 min

    1 potato

  10. 10 janvier 2012 à 4 h 25 min

    What a nice YouTube video it is! Amazing, I loved it, and I am sharing this YouTube record with all my friends.

  11. 11 janvier 2012 à 16 h 30 min

    Cool post ! Cheers for, visiting this blog dude! I will message you again. I did not know that!

  12. 16 janvier 2012 à 23 h 27 min

    Cool post ! Thanks for, posting on my blog page dude! Ill email you some time! I didnt realise that!

  13. 20 janvier 2012 à 7 h 24 min

    Strange , your site turns up with a dark color to it, what color is the primary color on your web site?

  14. 26 janvier 2012 à 9 h 08 min

    Virtually all of the comments on this blog site dont make sense.

  15. 28 janvier 2012 à 6 h 43 min

    Great goods from you, man. Utiliser des onglets dans ses applicatons android | Android-France I have understand your stuff previous to and you are just too fantastic. I really like what you’ve acquired here, really like what you are saying and the way in which you say it. You make it entertaining and you still care for to keep it sensible. I can not wait to read much more from you. This is really a great Utiliser des onglets dans ses applicatons android | Android-France informations.

  16. 31 janvier 2012 à 6 h 32 min

    How come you dont have your web site viewable in wap format? cant view anything in my netbook.

  17. 7 février 2012 à 8 h 56 min

    Weird , your posting turns up with a black hue to it, what color is the primary color on your web site?

  18. 7 février 2012 à 18 h 54 min

    Fantastic Stuff, do you have a twitter account?

  19. 7 février 2012 à 19 h 52 min

    hello there and thank you in your info – I have certainly picked up anything new from proper here. I did then again expertise some technical points the use of this site, as I experienced to reload the site many times prior to I could get it to load correctly. I have been considering in case your web hosting is OK? No longer that I am complaining, but slow loading instances times will sometimes have an effect on your placement in google and could injury your high quality score if ads and ***********|advertising|advertising|advertising and *********** with Adwords. Anyway I’m adding this RSS to my email and can look out for much more of your respective fascinating content. Ensure that you update this again soon.. London Escorts Agency, 16 Lindsay Square, London, SW1V 3SB, 028 2522 0183

  20. 12 février 2012 à 9 h 38 min

    Many of the remarks on this web site dont make sense.

  21. 15 février 2012 à 4 h 16 min

    When I start your Feed it seems to be a lot of garbage, is the issue on my side?

  22. 16 février 2012 à 20 h 39 min

    Rencontre homme femme

  23. 1 mars 2012 à 14 h 23 min

    I simply could not leave your web site prior to suggesting that I really loved the standard information a person provide on your guests? Is gonna be again often in order to investigate cross-check new posts

  24. 2 mars 2012 à 4 h 25 min

    Someone essentially lend a hand to make significantly posts I might state. This is the first time I frequented your web page and up to now? I surprised with the analysis you made to make this actual submit incredible. Magnificent task!

  25. 4 mars 2012 à 7 h 33 min

    Uhhm.. Hello there, simply changed into alert to your blog through Google, and found that it is really informative. Thank you and best of luck.

Laissez un commentaire

     Vous devez être identifié pour publier un commentaire.
     Connexion - Inscription