Android Tutorial TabHost tutorial

Status
Not open for further replies.
It is recommended to use TabStripViewPager for new projects: https://www.b4x.com/android/forum/threads/63975/#content


The TabHost view is a very important view. It allows you to add several layouts into the same activity.

tabhost_3.png


The designer currently doesn't support adding views directly to the TabHost.
You can only add the TabHost and set its layout:

tabhost1.png


There are several ways to add tab pages. Usually it is recommended to create a layout file in the designer for each page and then load it.
The designer treats every layout file separately. It is your responsibility to set the views names to distinct names (this is only required for views that you plan to access programmatically).

This is done with AddTab or AddTabWithIcon.
Example:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
    Dim bmp1, bmp2 As Bitmap
    bmp1 = LoadBitmap(File.DirAssets, "ic.png")
    bmp2 = LoadBitmap(File.DirAssets, "ic_selected.png")
  
    TabHost1.AddTabWithIcon ("Name", bmp1, bmp2, "page1") 'load the layout file of each page
    TabHost1.AddTab("Color", "page2")
    TabHost1.AddTab("Animal", "page3")
End Sub
AddTabWithIcon receives two bitmaps. There are actually two icons. One when the tab is selected and one when the tab is not selected. The guidelines recommend creating a dark version for the selected icon and a light version for the not selected icon.

You can manually change the selected tab by setting the CurrentTab property.

The example is attached.
B4X:
Sub Process_Globals
  
End Sub

Sub Globals
    Dim TabHost1 As TabHost
    Dim txtName, txtAnimal, txtColor As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
    Dim bmp1, bmp2 As Bitmap
    bmp1 = LoadBitmap(File.DirAssets, "ic.png")
    bmp2 = LoadBitmap(File.DirAssets, "ic_selected.png")
  
    TabHost1.AddTabWithIcon ("Name", bmp1, bmp2, "page1") 'load the layout file of each page
    TabHost1.AddTab("Color", "page2")
    TabHost1.AddTab("Animal", "page3")
End Sub
Sub Activity_Pause (Finishing As Boolean)
  
End Sub
Sub Activity_Resume

End Sub
Sub btnNext1_Click
    TabHost1.CurrentTab = 1 'move to next tab
End Sub

Sub btnNext2_Click
    TabHost1.CurrentTab = 2 'move to next tab
End Sub

Sub btnDone_Click
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append("You have entered:").Append(CRLF)
    sb.Append("Name: ").Append(txtName.Text).Append(CRLF)
    sb.Append("Color: ").Append(txtColor.Text).Append(CRLF)
    sb.Append("Animal: ").Append(txtAnimal.Text)
    Msgbox(sb.ToString, "")
End Sub

Sub TabHost1_TabChanged
    Activity.Title = "Current Tab = " & TabHost1.CurrentTab
End Sub

Project is available here: http://www.b4x.com/android/files/tutorials/TabHost.zip
 
Last edited:

Steve Miller

Active Member
Licensed User
Longtime User
Is it possible to change the icon on the tab after it's been loaded? For instance, when a user finishes entering information in Tab1 and they go on to Tab2, I'd like to load a checkmark icon on Tab1 showing they finished that tab.

Possible?
 

arjian

Member
Licensed User
Longtime User
When i add a Layout in tabhost It doesn’t display width of views completely as shown in the attached file, I have to shift them 11dip to the left or there is a simple way, how can I fix it?
 

Attachments

  • TabHost.png
    TabHost.png
    28.5 KB · Views: 808

arjian

Member
Licensed User
Longtime User
How are you building the layout?

You should build it with the designer and call lbl.SetLeftAndRight(0, 100%x) in the designer script.

Load the layout file with TabHost.AddTab.


I build it with the designer and load it with TabHost.AddTab method

But I didn’t set there’s position in the designer script because I set them by code as shown in the attached files
 

Attachments

  • 1.PNG
    1.PNG
    12.7 KB · Views: 621
  • 2.PNG
    2.PNG
    38.6 KB · Views: 615
Last edited:

arjian

Member
Licensed User
Longtime User
You will need to implement it with the designer.

that is not the way i was expected, sometimes the view's position is dependent to the variables in code ,so i have to shift views 11dip to the left
,Scrollview has this problem too
 

Steve Miller

Active Member
Licensed User
Longtime User
Is it possible to get the previous tab? So, if I am on tab1 and I click on tab3, is there a way to find out I was on tab1 prior to the current tab (which is now tab3)?
 

Sytek

Active Member
Licensed User
Longtime User
Google design guide recommended it. Note that in Android 4+ TabHost doesn't show icons if there is also text.

I think that there is a solution for showing Tabs at the bottom. Try to search the forum.
This should be sticky...At the moment.
Best Regards!
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
You can use a global variable to store the current selected tab and update it in TabChanged event. This will allow you to know the previous selected tab.

I just use the TabHost's .Tag property to store the .CurrentTab - which of course becomes the previous tab when the TabChanged event is triggered:

B4X:
Sub TabHost_TabChanged
    Log("Previous Tab: " & TabHost.Tag)
    TabHost.Tag = TabHost.CurrentTab
End Sub
 

Dadeda

Member
Licensed User
Longtime User
This lib is great, but, you have the selected/unselected tab using a bitmap ok...but i'm not using bitmaps, only text with gradient from tabhostextra wich is great, how could i do the same selected/unselect tab without bitmaps just colors of tab background and text color?
 

merlin2049er

Well-Known Member
Licensed User
Longtime User
hmm, I've got one app that's asking for me to initalize tabhost (and crashing, but it's loaded as a layout) , and the other app is fine.
Not sure what gives.
 

merlin2049er

Well-Known Member
Licensed User
Longtime User
It's ok now. In the globals...

B4X:
dim tb as tabhost
changed it to
B4X:
dim tabhost1 as tabhost
 

Rob Rendle

Member
Licensed User
Longtime User
Hi,

How do you remove the blue line that is under the tab (when selected) and also the blue line that is the same width as the TabHost?
 
Status
Not open for further replies.
Top