Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

TabHost tutorial

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-01-2010, 07:28 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default TabHost tutorial

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



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



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:
Code:
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.
Code:
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.basic4ppc.com/android/fil...ls/TabHost.zip
Reply With Quote
  #2 (permalink)  
Old 12-22-2010, 06:32 AM
Brad's Avatar
Senior Member
 
Join Date: Dec 2010
Location: Seattle
Posts: 189
Default

Is there a way to place the tabs at the bottom of the screen?
Reply With Quote
  #3 (permalink)  
Old 12-22-2010, 07:50 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

I will add it in the next update.
Reply With Quote
  #4 (permalink)  
Old 12-23-2010, 02:52 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

I guess I'm missing the obvious but I can't get to build a tab set programatically using AddTab2. I get the tab header displayed and can select one of them but no tab bodies are shown and trying to set the Panel heights throws null pointer exceptions
Code:
Sub Globals
    
Dim TabHost1 As TabHost
End Sub

Sub Activity_Create(FirstTime As Boolean)
    TabHost1.Initialize(
"TabHost1")
    Activity.AddView(TabHost1, 
00300430)
    
Dim p1, p2, p3 As Panel
    p1.Initialize(
"Panel1")
    p2.Initialize(
"Panel2")
    p3.Initialize(
"Panel3")
    TabHost1.AddTab2(
"One", p1)
    TabHost1.AddTab2(
"Two", p2) 
    TabHost1.AddTab2(
"Three", p3) 
End Sub
__________________
Sorry, but I don't answer questions by PM or email.
Please post your queries in the forum.
Reply With Quote
  #5 (permalink)  
Old 12-23-2010, 03:41 PM
Cor Cor is offline
Basic4ppc Veteran
 
Join Date: May 2007
Posts: 481
Default

maybe the invalidate?
__________________
Best regards,
Cor de Visser, Netherlands, HTC Magic 2.2.1

Amazing Guitar Chords for Android
http://android.ready4music.com
Reply With Quote
  #6 (permalink)  
Old 12-23-2010, 03:47 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

I was missing the obvious. I didn't realise that the panels are created transparent. I was expecting a default colour.
__________________
Sorry, but I don't answer questions by PM or email.
Please post your queries in the forum.
Reply With Quote
  #7 (permalink)  
Old 03-09-2011, 07:16 PM
Basic4ppc Veteran
 
Join Date: Feb 2011
Posts: 241
Default

Quote:
Originally Posted by Erel View Post
I will add it in the next update.
Was the ability to place tabs at the bottom of the screen added? I haven't figured out how to achieve that yet if it was so thought I would ask.

Thanks.

Dave
Reply With Quote
  #8 (permalink)  
Old 03-10-2011, 06:21 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

It wasn't added yet. Android support for TabHost with buttons at the buttom is not good which makes it a bit complicated.
Reply With Quote
  #9 (permalink)  
Old 05-05-2011, 03:25 PM
Junior Member
 
Join Date: Feb 2011
Posts: 22
Default

Any idea when this will be ready? I absolutely want to create an app which looks the same as an IOS app I did before, which has its buttons below.
Reply With Quote
  #10 (permalink)  
Old 05-06-2011, 11:13 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

I don't know. The native TabHost makes it complicated to change the buttons positions to the bottom.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
GPS tutorial Erel Basic4android Getting started & Tutorials 62 05-22-2012 02:04 PM
SQL Tutorial Erel Tutorials 32 02-07-2011 09:14 AM
how to create/send mail, and use tabhost ? Andre-K Basic4android Updates and Questions 7 12-17-2010 11:26 PM
tabhost example neede Cor Beta 5 12-01-2010 07:31 AM
Need SQL tutorial Cableguy Basic4android Updates and Questions 11 11-30-2010 07:03 AM


All times are GMT. The time now is 10:20 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0