Is it possible to fill tabhost tab with gradient color?

warwound

Expert
Licensed User
Longtime User
Hi there.

My TabHostExtras library won't help you - at the moment.

But if we can work out what Java needs to be written to update the default TabWidget then i'll add it to my library.

Presumably it's the TabWidget children that you want to fill with a gradient color?

Take a look at this page: GradientDrawable | Android Developers.

Looks like we want to implement a GradientDrawable as the background of each TabWidget child?

Another related link here: android - how to fill a view with a gradient background color - Stack Overflow.

Martin.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Here we have a solution then - it doesn't use my TabHostExtras library, instead it uses the Reflection library.

Here's the code:

B4X:
Sub Process_Globals
End Sub

Sub Globals
   Dim TabHost1 As TabHost
   Dim Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("LayoutMain")
   
   '   add some tabs to the TabHost
   Dim i As Int
   For i=0 To 3
      TabHost1.AddTab("Tab#"&i, "LayoutTab")
      Label1.Text="This is tab#"&i
   Next
   
   '   create a GradientDrawable to use as the TabIndicator background
   Dim MyGradient As GradientDrawable
   Dim Clrs(2) As Int
   Clrs(0)=Colors.Red
   Clrs(1)=Colors.Yellow
   MyGradient.Initialize("TOP_BOTTOM", Clrs)
   
   '   apply the GradientDrawable to each TabIndicator
   Dim Reflector1 As Reflector
   Dim TabIndex, TabCount As Int
   Dim TabWidget As Object
   Reflector1.Target=TabHost1
   TabWidget=Reflector1.RunMethod("getTabWidget")
   Reflector1.Target=TabWidget
   TabCount=Reflector1.RunMethod("getTabCount")
   Dim TabIndicator As View
   For TabIndex=0 To TabCount-1
      TabIndicator=Reflector1.RunMethod2("getChildAt", TabIndex, "java.lang.int")
      TabIndicator.Background=MyGradient
   Next
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

It works but the red/yellow gradient looks rather awful to me LOL!

I'll look at adding this to my TabHostExtras library - probably do that later or tomorow morning.

Martin.
 

Attachments

  • TabHostExtrasGradientDrawable.zip
    6.4 KB · Views: 391
Upvote 0
Top