How to create tab in android

Put your problem here if it does not fit any of the other categories.

How to create tab in android

Postby smalltalk » Thu Feb 28, 2008 8:16 am

Hi +-.
I want to ask you how to create a simple tab. I saw contact app in android but don't know how to create tab. Please reply me fast (I'm hurry up.)
smalltalk
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Feb 12, 2008 3:34 pm

Top

Postby plusminus » Thu Feb 28, 2008 9:44 am

Hello smalltalk,

I have some working code, but did not post it, because TabHost is currently still pretty buggy.
i.e.
- applying a theme (not the default one) makes the TabHost invisible :roll:
- AdapterViews (or was it ViewGroups ...?) cannot be used, because the cause an Exception.

But anyway (Looks shitty, because I planned to put it into GridView, what is not yet possible :( ):
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.         android:id="@+id/th_set_menu_tabhost"
  6.  
  7.         android:layout_width="fill_parent"
  8.  
  9.         android:layout_height="fill_parent">
  10.  
  11.         <TabWidget
  12.  
  13.                 android:id="@android:id/tabs"
  14.  
  15.                 android:layout_width="fill_parent"
  16.  
  17.                 android:layout_height="fill_parent"/>
  18.  
  19.         <FrameLayout
  20.  
  21.                 android:id="@android:id/tabcontent"
  22.  
  23.                 android:layout_width="fill_parent"
  24.  
  25.                 android:layout_height="fill_parent"
  26.  
  27.                 android:paddingTop="65px">
  28.  
  29.                 <LinearLayout
  30.  
  31.                         android:id="@+id/grid_set_menu_page1"
  32.  
  33.                         android:orientation="horizontal"
  34.  
  35.                     android:layout_width="fill_parent"
  36.  
  37.                     android:layout_height="fill_parent">
  38.  
  39.                                 <ImageButton
  40.  
  41.                                         android:id="@+id/ibtn_settings_audio"
  42.  
  43.                                         android:layout_width="wrap_content"
  44.  
  45.                                         android:layout_height="wrap_content"
  46.  
  47.                                         android:src="@drawable/audio_48"/>
  48.  
  49.                                 <ImageButton
  50.  
  51.                                         android:id="@+id/ibtn_settings_close_page1"
  52.  
  53.                                         android:layout_width="wrap_content"
  54.  
  55.                                         android:layout_height="wrap_content"
  56.  
  57.                                         android:src="@drawable/close_48"/>
  58.  
  59.                 </LinearLayout>
  60.  
  61.                 <LinearLayout
  62.  
  63.                         android:id="@+id/grid_set_menu_page2"
  64.  
  65.                         android:orientation="horizontal"
  66.  
  67.                     android:layout_width="fill_parent"
  68.  
  69.                     android:layout_height="fill_parent">
  70.  
  71.                                 <ImageButton
  72.  
  73.                                         android:id="@+id/ibtn_settings_favorites"
  74.  
  75.                                         android:layout_width="wrap_content"
  76.  
  77.                                         android:layout_height="wrap_content"
  78.  
  79.                                         android:src="@drawable/favorites_48"/>
  80.  
  81.                                 <ImageButton
  82.  
  83.                                         android:id="@+id/ibtn_settings_close_page2"
  84.  
  85.                                         android:layout_width="wrap_content"
  86.  
  87.                                         android:layout_height="wrap_content"
  88.  
  89.                                         android:gravity="right"
  90.  
  91.                                         android:src="@drawable/close_48"/>
  92.  
  93.                 </LinearLayout>
  94.  
  95.         </FrameLayout>
  96.  
  97. </TabHost>
Parsed in 0.006 seconds, using GeSHi 1.0.8.4


Then call the following in Java, after setting the Content View:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.             this.myTabHost = (TabHost)this.findViewById(R.id.th_set_menu_tabhost);
  2.  
  3.             this.myTabHost.setup();
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Let me know if it worked with you. :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby smalltalk » Thu Feb 28, 2008 4:53 pm

Hi +-,
I want to ask. When I press Run button in eclipse is 'ok', but when i click on symbol on 'desktop' of virtual machine is 'not ok'. android state 'nullpointerexception'. I don't understand. Please reply fast(i'm hurry).
ps: I posted some your code (with ArrayAdapter). and thanks for your xml file
Image
Image
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.     android:id="@+id/th_set_menu_tabhost"
  7.  
  8.     android:layout_width="fill_parent"
  9.  
  10.     android:layout_height="fill_parent">
  11.  
  12.      <TabWidget
  13.  
  14.          android:id="@android:id/tabs"
  15.  
  16.          android:layout_width="fill_parent"
  17.  
  18.          android:layout_height="fill_parent"/>
  19.  
  20.      <FrameLayout
  21.  
  22.          android:id="@android:id/tabcontent"
  23.  
  24.          android:layout_width="fill_parent"
  25.  
  26.          android:layout_height="fill_parent"
  27.  
  28.          android:paddingTop="65px">  
  29.  
  30.           <ListView
  31.  
  32.          android:id = "@+id/danhsach"
  33.  
  34.          android:layout_width="fill_parent"
  35.  
  36.          android:layout_height="fill_parent"
  37.  
  38.          />
  39.  
  40.      </FrameLayout>
  41.  
  42. </TabHost>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package khtn.com;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.os.Bundle;
  9.  
  10. import android.view.View;
  11.  
  12. import android.widget.ArrayAdapter;
  13.  
  14. import android.widget.LinearLayout;
  15.  
  16. import android.widget.ListView;
  17.  
  18. import android.widget.TabHost;
  19.  
  20. import android.widget.LinearLayout.LayoutParams;
  21.  
  22. import android.widget.TabHost.TabSpec;
  23.  
  24.  
  25.  
  26. public class Main_screen extends Activity{
  27.  
  28.        
  29.  
  30.         private ListView ls1;
  31.  
  32.         private ListView ls2;  
  33.  
  34.         private TabHost myTabHost;
  35.  
  36.        
  37.  
  38.         @Override
  39.  
  40.         public void onCreate(Bundle icicle)
  41.  
  42.         {
  43.  
  44.                 super.onCreate(icicle);
  45.  
  46.                
  47.  
  48.                 ls1 = new ListView(Main_screen.this);                  
  49.  
  50.                 ls2 = new ListView(Main_screen.this);
  51.  
  52.                
  53.  
  54.                 setContentView(R.layout.main_screen);
  55.  
  56.                 this.myTabHost = (TabHost)this.findViewById(R.id.th_set_menu_tabhost);
  57.  
  58.         this.myTabHost.setup();        
  59.  
  60.                
  61.  
  62.                 TabSpec ts = myTabHost.newTabSpec("TAB_TAG_1");
  63.  
  64.                
  65.  
  66.                 ts.setIndicator("Tab1");                               
  67.  
  68.                                
  69.  
  70.                 ts.setContent(new TabHost.TabContentFactory(){
  71.  
  72.                         public View createTabContent(String tag)
  73.  
  74.                         {                                                                      
  75.  
  76.                                 ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main_screen.this,android.R.layout.simple_list_item_1,new String[]{"item1","item2","item3"});
  77.  
  78.                                 ls1.setAdapter(adapter);       
  79.  
  80.                                 return ls1;
  81.  
  82.                         }              
  83.  
  84.                 });    
  85.  
  86.                
  87.  
  88.                 myTabHost.addTab(ts);
  89.  
  90.                                
  91.  
  92.                 TabSpec ts1 = myTabHost.newTabSpec("TAB_TAG_2");
  93.  
  94.                
  95.  
  96.                 ts1.setContent(new TabHost.TabContentFactory(){
  97.  
  98.                         public View createTabContent(String tag)
  99.  
  100.                         {                                                                      
  101.  
  102.                                 ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main_screen.this,android.R.layout.simple_list_item_1,new String[]{"item4","item5","item6"});
  103.  
  104.                                 ls2.setAdapter(adapter);       
  105.  
  106.                                 return ls2;
  107.  
  108.                         }              
  109.  
  110.                 });            
  111.  
  112.                
  113.  
  114.                 ts1.setIndicator("Tab2");
  115.  
  116.                                
  117.  
  118.                 myTabHost.addTab(ts1);
  119.  
  120.         }      
  121.  
  122. }
  123.  
  124.  
Parsed in 0.042 seconds, using GeSHi 1.0.8.4
Attachments
tab2.png
tab2.png (21.64 KiB) Viewed 93213 times
tab1.png
tab1.png (21.76 KiB) Viewed 93213 times
smalltalk
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Feb 12, 2008 3:34 pm

Postby plusminus » Thu Feb 28, 2008 7:08 pm

Hello smalltalk,

I don't know why it works only directly form Eclipse.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby smalltalk » Sat Mar 01, 2008 8:01 am

Hello +-,
Please answer for me how to create tab below only use file xml and not use any code in java.Please reply me fast(i'm hurry)

Image
Attachments
cal.PNG
cal.PNG (19.39 KiB) Viewed 93124 times
smalltalk
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Feb 12, 2008 3:34 pm

Postby surya.n.k » Sat Mar 01, 2008 8:21 am

hi all,

can i have more than two tabs in the screen as i am trying it is not allowing to add .....
surya.n.k
Developer
Developer
 
Posts: 26
Joined: Fri Dec 07, 2007 7:33 am

Top

Postby plusminus » Sat Mar 01, 2008 7:17 pm

Hello smalltalk,

to add icons to the tab-headers and setting them up in java, do the following:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.             TabSpec ts1 = myTabHost.newTabSpec("TAB1");
  2.  
  3.             ts1.setIndicator(getString(R.string.settings_tab_caption_1), getResources().getDrawable(R.drawable.settings_48));
  4.  
  5.             ts1.setContent(R.id.grid_set_menu_page1);
  6.  
  7.             this.myTabHost.addTab(ts1);
  8.  
  9.            
  10.  
  11.             TabSpec ts2 = myTabHost.newTabSpec("TAB2");
  12.  
  13.             ts2.setIndicator(getString(R.string.settings_tab_caption_2), getResources().getDrawable(R.drawable.home_48));
  14.  
  15.             ts2.setContent(R.id.grid_set_menu_page2);
  16.  
  17.             this.myTabHost.addTab(ts2);
  18.  
  19.            
  20.  
  21.             this.myTabHost.setCurrentTab(0);
Parsed in 0.044 seconds, using GeSHi 1.0.8.4


@ surya.n.k

I haven't tried it, but there is no reason, why three tab-headers should not work.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby E.Jyothsna » Thu Mar 13, 2008 7:41 am

I saw this code and i tried this.its working.Instead of listview i tried by using intent.But instead of displaying in same page it displaying in next page.Actually i want to display in same page.for that what i have to do.this is my code.

ts1.setContent(new TabHost.TabContentFactory() {

public View createTabContent(String arg0) {

Intent intent = new Intent(Main_screen.this, welcome.class);
startActivity(intent);
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main_screen.this,android.R.layout.simple_list_item_1,new String[]{"item4","item5","item6"});
// ls2.setAdapter(adapter);
return ls2;
}
});
reply soon
E.Jyothsna
Junior Developer
Junior Developer
 
Posts: 22
Joined: Thu Feb 28, 2008 12:25 pm
Location: chennai

Postby rock_win » Sat Mar 15, 2008 11:14 am

I tried it in rc37a and it shows only one tab and its the last TabWidgets that is added in the xml file.

More details are at:version rc37a showing only one tab.

Please help!
rock_win
Junior Developer
Junior Developer
 
Posts: 11
Joined: Mon Jan 21, 2008 4:24 pm

Postby chitgoks » Mon Mar 17, 2008 8:41 am

im doing this as well. but somehow the tab labels do not appear. they're just blank. anyone got ideas what could possibly be wrong?
Website/Java Games: http://www.chitgoks.com
Blogs:
http://tech.chitgoks.com
http://wuhtevah.chitgoks.com
http://disneyusa.blogspot.com
chitgoks
Developer
Developer
 
Posts: 36
Joined: Tue Mar 11, 2008 3:06 am

Postby williamzanelli » Mon Jan 05, 2009 3:06 am

I've copied this exmple and get a NullPointer Exception at this poiint

Code: Select all
ts3.setContent(new TabHost.TabContentFactory(){
            public View createTabContent(String tag) 
            {   
           
                 ArrayAdapter<String> adapter = new ArrayAdapter<String>(MytoDoList.this,android.R.layout.simple_list_item_1 ,new String[]{"item1","item2","item3"});
                 ls1.setAdapter(adapter);
                 
               
               return ls1;
           
            }         
       }); 


The Error reads, Uncaught handler: Thread main exiting due to uncaught exception
java.lang.NullPointerException


Any ideas on what I;ve done wrong?

Thanks in advance

William
Will
williamzanelli
Experienced Developer
Experienced Developer
 
Posts: 63
Joined: Mon Jan 05, 2009 2:37 am
Location: UK

Postby r0b » Fri Mar 06, 2009 8:06 pm

williamzanelli wrote:I've copied this exmple and get a NullPointer Exception at this poiint

Code: Select all
ts3.setContent(new TabHost.TabContentFactory(){
            public View createTabContent(String tag) 
            {   
           
                 ArrayAdapter<String> adapter = new ArrayAdapter<String>(MytoDoList.this,android.R.layout.simple_list_item_1 ,new String[]{"item1","item2","item3"});
                 ls1.setAdapter(adapter);
                 
               
               return ls1;
           
            }         
       }); 


The Error reads, Uncaught handler: Thread main exiting due to uncaught exception
java.lang.NullPointerException


Any ideas on what I;ve done wrong?

Thanks in advance

William


I get the same thing but don't have an exact line within my source - it's somewhere in the tabview source I believe.

I'm also trying to use a SimpleAdapter but it should work still - did you get yours fixed?
r0b
Freshman
Freshman
 
Posts: 3
Joined: Fri Mar 06, 2009 8:04 pm

Postby steven_d » Mon Aug 17, 2009 3:27 pm

Syntax: [ Download ] [ Hide ]
  1. .... 
  2.  
  3. public class Main_screen extends Activity{ 
  4.      
  5.     private ListView ls1; 
  6.     private ListView ls2;     
  7.     private TabHost myTabHost; 
  8.      
  9.     @Override 
  10.     public void onCreate(Bundle icicle) 
  11.     { 
  12.         super.onCreate(icicle); 
  13.          
  14.           ls1 = new ListView(Main_screen.this);           
  15.           ls2 = new ListView(Main_screen.this); 
  16.            
  17. .....         
  18.                  
  19.         ts.setContent(new TabHost.TabContentFactory(){ 
  20.             public View createTabContent(String tag) 
  21.               {                                     
  22.                   ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main_screen.this,android.R.layout.simple_list_item_1,new String[]{"item1","item2","item3"}); 
  23.                   ls1.setAdapter(adapter);     
  24.                   return ls1; 
  25.               }         
  26.         });     
  27.          
  28.         myTabHost.addTab(ts); 
  29.                  
  30. ...... 



Thanks for this code. Thats works for me, too. But I have one question. I've read on another page, that the coder create a second java file for createTabContent - method and make an instance of this class ( http://gedankzittern.wordpress.com/2009/05/08/erstellen-eines-tabhost-in-android/ ):

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. MyTabContentFactory tabContentfactory = new MyTabContentFactory(this, tabNames);  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

and in the other class:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MyTabContentFactory implements TabContentFactory { .....
  2.  
  3.  public View createTabContent(String tag) {
  4.  
  5. ...
  6.  
  7. }
  8.  
  9. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Whats the best way?? Have one solution a better performance??


Regards,
Stefan
steven_d
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon Jul 20, 2009 1:15 pm
Location: Germany

Force close problem

Postby Rashi » Sun Feb 14, 2010 12:52 pm

After making tabs and activityies for each tab i am getting a force close problem. Please help us.

Thanks,
Rashi
Rashi
Developer
Developer
 
Posts: 32
Joined: Thu Sep 10, 2009 11:28 am

Postby Rashi » Wed Mar 03, 2010 5:58 pm

Hello,

I have a problem, if i want to add actionListener on listview created in this tutorial den the whole layout of tab is gone. Do you have some method to keep the tabs in the flow.

Thanks,
Rashi
Rashi
Developer
Developer
 
Posts: 32
Joined: Thu Sep 10, 2009 11:28 am

Top
Next

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 25 guests