• Utiliser des onglets dans ses applications android

    Publié le 09 septembre 2009 par Guy (GuyTouch)


    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


    Guy

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

    Twitter Google+ 

  • 19 Commentaires pour ce billet

    1. 1 - Thierry dit:

      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. 2 - Guillaume dit:

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

    3. 3 - Guy dit:

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

    4. 4 - קידום אתרים בגוגל dit:

      Please link my web

    5. 5 - סוככים dit:

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

    6. 6 - Waylon Haugrud dit:

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

    7. 7 - low cost seo services dit:

      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. 8 - hormigon impreso dit:

      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. 9 - news dit:

      1 potato

    10. 10 - Genia Tijerina dit:

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

    11. 11 - Wedding Invitations dit:

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

    12. 12 - Condos in Pattaya dit:

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

    13. 13 - Boston Bruins dit:

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

    14. 14 - drug prohibition dit:

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

    15. 15 - invest solid trustpay dit:

      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. 16 - Madelyn dit:

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

    17. 17 - how to remove mole dit:

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

    18. 18 - JAMES dit:

      Fantastic Stuff, do you have a twitter account?

    19. 19 - Beaulah Santelli dit:

      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

  • Laissez un commentaire

  • Les Brèves