Android Question Been jerking around with looping through tabhost tabcontent for days getting nowhere...

Steve Miller

Active Member
Licensed User
Longtime User
I've been trying to loop through all the tabs in a tabhost trying to gain access to the views (editboxes, checkboxes, spinners, radiobuttons, buttons, etc) but getting nowhere.

The way I have each tab setup in the tabhost is as follows:

Tab1
Panel
views (editboxes, buttons, spinners, etc.)
Tab2
Panel
views (editboxes, buttons, spinners, etc.)
etc...

I have tried to loop through the tabs and gain access to the views, but just cannot seem to get anywhere. I've gotten to the point where it seemed to loop through the tabs, but everytime it moved to the next higher tab, it processed all previous tabs twice!

I'm out of my mind over this. Anyone have any code that works?

Thanks.
 

Steve Miller

Active Member
Licensed User
Longtime User
I did, but it only picked up the first tab. I saw some code in there that looked like it only loops through the tabs, sets it to current tab, but does nothing else in the loop. Maybe I'm missing something...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I did, but it only picked up the first tab. I saw some code in there that looked like it only loops through the tabs, sets it to current tab, but does nothing else in the loop. Maybe I'm missing something...
Make sure that you are using the latest version. It loops over all the internal views. You can see it by changing the state of an inner view and it will be kept after the orientation changes.
 
Upvote 0

Steve Miller

Active Member
Licensed User
Longtime User
I created them in the designer and add them to the tabhost via normal means in code.

I can access the tabs and the tab content, but it isn't working correctly. It would process the views on the tab twice. Then the loop went to the next tab, and it would process the tab twice, along with each previous tab twice. And so on until all tabs were done.

I just want to loop through all the tabs, set the foreground color and background color of each control in the tab content view.

I wouldn't have thought that to be difficult, but its proving to be a real chore. In .net it would be simple, but this isn't .net...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code seems to work as expected:
B4X:
Sub Globals
   Dim th As TabHost
End Sub

Sub Activity_Create(FirstTime As Boolean)
   th.Initialize("th")
   Activity.AddView(th, 0, 0, 100%x, 50%y)
   For i = 1 To 10
     th.AddTab(i, "1")
   Next
End Sub

Sub Activity_Click
   Dim current As Int = th.CurrentTab
   For i = 0 To th.TabCount - 1
     th.CurrentTab = i
   Next
   th.CurrentTab = current
   For Each v As View In Activity.GetAllViewsRecursive
     If v Is Button Then
       v.Color = Colors.Red
     End If
   Next
End Sub
 
Upvote 0

Steve Miller

Active Member
Licensed User
Longtime User
Forgive me for my ignorance, but, what is the first loop in activity_click doing? To me, it doesn't appear to really do anything.

Is this looping through the views in just one tab, or is it looping through all tabs and setting the color of all buttons on every tab to red? To me, it looks like it will only loop in the current tab.

If there are 10 tabs, and each tab has a panel on it, and each panel contains a button and a label, I want to loop through all 10 tabs, set the background color of each panel, button and label. I need to do the same for the text color of each button and label.

Am I wrong in my understanding above? Again, I'm sorry but I am not at my computer until this evening so cannot test until then.
 
Upvote 0

Steve Miller

Active Member
Licensed User
Longtime User
This code seems to work as expected:
B4X:
Sub Globals
   Dim th As TabHost
End Sub

Sub Activity_Create(FirstTime As Boolean)
   th.Initialize("th")
   Activity.AddView(th, 0, 0, 100%x, 50%y)
   For i = 1 To 10
     th.AddTab(i, "1")
   Next
End Sub

Sub Activity_Click
   Dim current As Int = th.CurrentTab
   For i = 0 To th.TabCount - 1
     th.CurrentTab = i
   Next
   th.CurrentTab = current
   For Each v As View In Activity.GetAllViewsRecursive
     If v Is Button Then
       v.Color = Colors.Red
     End If
   Next
End Sub

I implemented this code, but it doesn't work in my project. I don't think there is anything wrong with the layouts.
Erel, with your permission, I'd like to email the project to you to take a quick peak at. Maybe you can see something obvious that I'm doing wrong.
 
Upvote 0
Top