Android Code Snippet simple way to hide/show a TabStrip tab

If you don't need a tab temporarily, Instead of disabling it or removing it, you can simply hide it. When it's needed again, unhide it.

Show/hide a tabstrip tab:
'visible->hidden, or hidden->visible
Public Sub toggleTab(TabStrip1 As TabStrip, tabName As String) As Boolean
    For Each lbl As Label In GetAllTabLabels(TabStrip1)
        If lbl.Text=tabName Then
            lbl.Visible= lbl.Visible=False
            Return True
        End If
    Next
    Return False 'incorrect tab name provided
End Sub

Public Sub GetAllTabLabels (tabstrip As TabStrip) As List
    Dim jo As JavaObject = tabstrip
    Dim r As Reflector
    r.Target = jo.GetField("tabStrip")
    Dim tc As Panel = r.GetField("tabsContainer")
    Dim res As List
    res.Initialize
    For Each v As View In tc
        If v Is Label Then res.Add(v)
    Next
    Return res
End Sub
 
Last edited:

Mahares

Expert
Licensed User
Longtime User
you can simply hide it.
1. The code you have hides the label, but you can still scroll the tab. If that is not the case, please provide a simple sample.
2. If you have images in the tabstrip labels, you cannot even hide the label.
 

toby

Well-Known Member
Licensed User
Longtime User
The attached example allows you to show/hide "THE PAGE 3" by selecting menu option "Toggle tab 3".
Screen recording
 

Attachments

  • toggleTabStripTab.zip
    15.3 KB · Views: 135

Mahares

Expert
Licensed User
Longtime User
The attached example allows you to show/hide "THE PAGE 3" by selecting menu option "Toggle tab 3".
You can still scroll page3 when you toggle it to hide. Change the color of the listview in page 3 and you will see it when you left or right.
 

toby

Well-Known Member
Licensed User
Longtime User
You can still scroll page3 when you toggle it to hide. Change the color of the listview in page 3 and you will see it when you left or right.
Your code should check if the tab to scroll to is visible before trying to scroll with following code
B4X:
Public Sub visibleTabByIndex(tabIndex As Int) As Boolean
    Dim lbl As Label=GetAllTabLabels(mytabStrip1).Get(tabIndex)
    Return lbl.Visible
End Sub

If the tab to hide is the current page, you need to scroll to another tab first.
B4X:
Public Sub toggleTabByIndex(tabIndex As Int)
    If mytabStrip1.CurrentPage=tabIndex Then
        Dim nextTabIndex As Int=(tabIndex +1) Mod lstTab.Size
        mytabStrip1.ScrollTo(nextTabIndex, True)
    End If
    Dim lbl As Label= lstTab.Get(tabIndex)
    lbl.Visible= lbl.Visible=False

End Sub
 
Top