B4A Library TabHostExtras

Here is a new library i have created that allows you to customise a TabHost.

TabHostExtras
Version: 2.2
  • TabHostExtras
    Methods:
    • GetTagWidget (TabHost1 As TabHost) As TabWidget
      Returns TabHost1's TabWidget.
      Allowing you to customize various settings.
    • getTabContentViewPadding (tabHost1 As TabHost) As RectWrapper
      Get the layout padding of tabHost1 TabContentView
      Returns a Rect object containing pixel values
    • getTabEnabled (tabHost1 As TabHost, index As Int) As Boolean
      Get the Enabled state of TabIndicator #index in tabHost1
    • getTabHeight (tabHost1 As TabHost) As Int
      Get the height (in pixels) of the TabIndicators in tabHost1
    • getTabHostPadding (tabHost1 As TabHost) As RectWrapper
      Get the layout padding of tabHost1 container View
      Returns a Rect object containing pixel values
    • getTabIcon (tabHost1 As TabHost, TabIndex As Int) As ImageViewWrapper
      Get the (icon) ImageView of TabIndicator #TabIndex in tabHost1
    • getTabTextSize (tabHost1 As TabHost) As Float
      Get the text size (in pixels) of all TabIndicators
    • getTabVisibility (tabHost1 As TabHost, index As Int) As Boolean
      Get the visibility of TabIndicators #index in tabHost1
    • setTabContentViewPadding (tabHost1 As TabHost, left As Int, top As Int, right As Int, bottom As Int)
      Set the layout padding (in dip) of tabHost1 TabContentView
    • setTabEnabled (tabHost1 As TabHost, enabled As Boolean)
      Enable or disable all TabIndicators in tabHost1
    • setTabEnabled2 (tabHost1 As TabHost, enabled As Boolean, index As Int)
      Enable or disable TabIndicator #index in tabHost1
    • setTabGradientDrawable (tabHost1 As TabHost, orientation As String, color1 As Int, color2 As Int, cornerRadius As Float)
      Set a GradientDrawable as the background on all TabIndicators in tabHost1
      All fours corner radii of the GradientDrawable are set to the value of cornerRadius (in pixels)
    • setTabGradientDrawable2 (tabHost1 As TabHost, orientation As String, color1 As Int, color2 As Int, cornerRadius() As Float)
      Set a GradientDrawable as the background on all TabIndicators in tabHost1
      Corner radii of the GradientDrawable are set individually (in pixels) based upon the number of elements in the array cornerRadius:
      1 element defines all corner radii
      2 elements define corner radii in order top left and right, bottom left and right
      4 elements define corner radii in order top-left, top-right, bottom-right, bottom-left
    • setTabHeight (tabHost1 As TabHost, tabHeight As Int)
      Set the height (in pixels) of all TabIndicators in tabHost1
    • setTabHostPadding (tabHost1 As TabHost, left As Int, top As Int, right As Int, bottom As Int)
      Set the layout padding (in dip) of tabHost1 container View
    • setTabTextColor (tabHost1 As TabHost, Color As Int)
      Set the color to be used for all tab indicators text.
      This color will be used for all tab indicators regardless of their selected state.
    • setTabTextColorStateList (tabHost1 As TabHost, ColorStateListName As String)
      Set a ColorStateList to be used for the text color of all tab indicators.
      The ColorStateList must be defined in XML in your application Objects/res/drawable folder.
      Color for selected and not selected tab state can be defined.
    • setTabTextSize (tabHost1 As TabHost, TextSize As Float)
      Set the text size of all TabIndicators
      TextSize is assumed to be in units of dip.
    • setTabTitle (tabHost1 As TabHost, Title As String, TabIndex As Int)
      Set the Title text of TabIndicator #TabIndex in tabHost1
    • setTabVisibility (tabHost1 As TabHost, visible As Boolean)
      Set the visibility of all TabIndicators in tabHost1
    • setTabVisibility2 (tabHost1 As TabHost, visible As Boolean, index As Int)
      Set the visibility of TabIndicator #index in tabHost1
  • TabWidget
    Methods:
    • BringToFront
    • GetChildTabViewAt (TabIndex As Int) As View
      Returns the tab indicator view at the given index.
      The returned View will be a ViewGroup with 2 child Views:
      An ImageView at index 0 and a TextView at Index 1.
    • GetTabIcon (TabIndex As Int) As ImageViewWrapper
      Get the (icon) ImageView of the tab indicator view at the given index.
    • GetTabLabel (TabIndex As Int) As LabelWrapper
      Get the (TextView) Label of the tab indicator view at the given index.
    • Initialize (arg1 As String)
    • Invalidate
    • Invalidate2 (arg0 As Rect)
    • Invalidate3 (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
    • IsInitialized As Boolean
    • RemoveView
    • RequestFocus As Boolean
    • SendToBack
    • SetBackgroundImage (arg0 As Bitmap)
    • SetLayout (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
    Properties:
    • Background As Drawable
    • Color As Int [write only]
    • DividerDrawable As Drawable [write only]
      Sets the drawable to use as a divider between the tab indicators.
    • Enabled As Boolean
      Get or Set the enabled state of the TabWidget.
    • Height As Int
    • Left As Int
    • LeftStripDrawable As Drawable [write only]
      Sets the drawable to use as the left part of the strip below the tab indicators.
    • RightStripDrawable As Drawable [write only]
      Sets the drawable to use as the right part of the strip below the tab indicators.
    • StripEnabled As Boolean
      Get or Set whether the bottom strips on the tab indicators are drawn or not.
    • TabCount As Int [read only]
      Get the number of tab indicator views.
    • Tag As Object
    • Top As Int
    • Visible As Boolean
    • Width As Int

Attached are the library files and a little demo.
If you run the demo try your device's 'Menu' key and toggle the tabs from visible to hidden.

I originally created this functionality in a code module and once it was working created the library.
So i have attached the code module as a separate attachment in case anyone wants to use it - the Reflection library is required if you use the code module but is not required if you use the library of course.

The code module has a Sub (not included in the library):

GetTabIndicators(TabHost1 As TabHost) As View()

This will return an array of Views, these Views are the TabHost's TabIndicators.
Anyone hoping to further customise the look of a TabHost may find this Sub useful - see the code module demo for an example of changing the TabIndicators' Color property.

Martin.
 

Attachments

  • TabHostExtrasCodeModule.zip
    8.9 KB · Views: 2,090
  • TabHostExtras_v2_20.zip
    22 KB · Views: 2,926
Last edited:

PakoT52

Active Member
Licensed User
Longtime User
Hi , how i can chamge Text size into a tob host , i write this code but he dosen't change ..
B4X:
Activity.LoadLayout("Layout1")
TabManager.setTabTextSize(TabHost1,100)
TabHost1.AddTab("A" ,"A")
TabHost1.AddTab("B" ,"B")
TabHost1.AddTab("C" ,"C")
 

warwound

Expert
Licensed User
Longtime User
What version of android are you running this on?
I just tested on Jelly Bean and you're right - it doesn't work!

I'll try it on a Gingerbread device shortly - gotta wait for that device to charge it's battery first though.

Martin.
 

warwound

Expert
Licensed User
Longtime User
Hmmm....

Try setting the text size after adding the tab:

B4X:
Activity.LoadLayout("Layout1")
TabHost1.AddTab("A" ,"A")
TabHost1.AddTab("B" ,"B")
TabHost1.AddTab("C" ,"C")
TabManager.setTabTextSize(TabHost1,100)

Does that work?

Martin.
 

PakoT52

Active Member
Licensed User
Longtime User
Hmmm....

Try setting the text size after adding the tab:

B4X:
Activity.LoadLayout("Layout1")
TabHost1.AddTab("A" ,"A")
TabHost1.AddTab("B" ,"B")
TabHost1.AddTab("C" ,"C")
TabManager.setTabTextSize(TabHost1,100)

Does that work?

Martin.
Yes , it work . Thank you and congratulazione for the library is so beautiful
 

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.

Is that possible?
 

warwound

Expert
Licensed User
Longtime User
Can you test the attached update?

I've added a new method:

getTabIcon (tabHost1 As TabHost, TabIndex As Int) As ImageView
Get the (icon) ImageView of TabIndicator #TabIndex in tabHost1

Pass a TabHost and the index of the tab whose icon you want to change, it should return the ImageView that displays that icon.
You can now set whatever icon you wish.

Martin.
 
Last edited:

warwound

Expert
Licensed User
Longtime User
TabHostExtras updated to version 2.10

This update adds a new method:

getTabIcon (tabHost1 As TabHost, TabIndex As Int) As ImageView
Get the (icon) ImageView of TabIndicator #TabIndex in tabHost1

Returns the ImageView used to display the icon on the specified tab.
(You can update the icon image if desired).

Version 2.10 is attached to the first post in this thread.

Martin.
 

merlin2049er

Well-Known Member
Licensed User
Longtime User
Sweet, I just installed it last night. I'd like to shrink the tabs / fonts to size 10.
 

Steve Miller

Active Member
Licensed User
Longtime User
My project got corrupted and I restored the previous version from TFS. I'm trying to run it and am getting the below error. The line in the code that is highlighted is this:

TabHostE.setTabTextColorStateList(TabHost1,"tabcolor")

I looked and the tabcolor.xml file was missing in the Objects\res\drawable folder, so I brought it down from TFS and put it in there. I ran my program again and still getting the same error. I looked in the Objects\res\drawable folder and the file is gone! I think that's the cause of the below error, but what would be causing the file to be there before running the and then being deleted when running it? Am I missing something here?

This is the error:

** Activity (main) Create, isFirst = true **


android.content.res.Resources$NotFoundException: Resource ID #0x0


at android.content.res.Resources.getValue(Resources.java:1883)
at android.content.res.Resources.getColorStateList(Resources.java:1644)
at uk.co.martinpearman.b4a.tabhostextras.TabHostExtras.setTabTextColorStateList(TabHostExtras.java:221)
at b4a.example.main._activity_create(main.java:465)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:173)
at b4a.example.main.afterFirstLayout(main.java:98)
at b4a.example.main.access$100(main.java:16)
at b4a.example.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Object context is paused. Ignoring CallSubDelayed: AddToActivity


Object context is paused. Ignoring CallSubDelayed: AddToActivity
Object context is paused. Ignoring CallSubDelayed: AddToActivity
Object context is paused. Ignoring CallSubDelayed: AddToActivity
Object context is paused. Ignoring CallSubDelayed: AddToActivity
Object context is paused. Ignoring CallSubDelayed: AddToActivity
Object context is paused. Ignoring CallSubDelayed: AddToActivity
Object context is paused. Ignoring CallSubDelayed: AddToActivity
Object context is paused. Ignoring CallSubDelayed: AddToActivity
 

warwound

Expert
Licensed User
Longtime User
Any resources not marked as read-only will be deleted when you clean and/or compile your project.

So copy the tabcolor.xml file to Objects\res\drawable again.
Right click it and choose Properties.
Select read-only.

That should ensure the file is no longer deleted when you compile.

Martin.
 

Steve Miller

Active Member
Licensed User
Longtime User
Thanks Martin. I checked the file and it was read-only, yet it was deleted. It may have been a ghost flag :eek:. I unchecked it and clicked apply, then checked it again and clicked apply. Then I clicked Clean Project in the tools menu. Then ran the program and it worked.

Great product, by the way!
 

aviario

Active Member
Licensed User
Longtime User
Hi I downloaded and TabHostExtras_v2_10.zip version is 2.00 not 2.10

thanks
 

iCAB

Well-Known Member
Licensed User
Longtime User
Anyone tried this library with B4A V3.5?
I am having issues even having the demo sample show anything on the screen
 

warwound

Expert
Licensed User
Longtime User
Anyone tried this library with B4A V3.5?
I am having issues even having the demo sample show anything on the screen

Just tried the demo project with b4a version 3.50 and you're right - something is wrong.
Nothing is displayed in the Activity.

I'll try to find time to look at what's wrong over the weekend.

Martin.
 
Top