Android Question [B4X] Set gradient to a panel in B4X

mberthe

Member
Licensed User
In b4A I have set a gradient with 3 colors to a panel :

B4X:
dim panel1 as panel
Dim dw As GradientDrawable
Dim colorgris As Int= Colors.RGB(96,96,96)
Dim clr(3) As Int
clr = Array  As Int(colorgris,Colors.white,colorgris)
dw.Initialize("TOP_BOTTOM",clr)
panel1.background= dw

How make the same in B4X ?
 

sorex

Expert
Licensed User
I don't think there is a XUI method for it yet. I only used the xui color & border methods so far.

you'll have to use conditional code for #B4A & #B4i
 

Erel

Administrator
Staff member
Licensed User
B4X:
Dim clr(3) As Int
clr = Array  As Int(colorgris,Colors.white,colorgris)
Better:
B4X:
Dim clr() As Int = Array  As Int(colorgris,Colors.white,colorgris)
Cross platform solution:

B4X:
Sub SetGradientBackground(pnl As B4XView, Clrs() As Int, Orientation As String)
   Dim bc As BitmapCreator
   bc.Initialize(pnl.Width / xui.Scale, pnl.Height / xui.Scale)
   bc.FillGradient(Clrs, bc.TargetRect, Orientation)
   Dim iv As ImageView
   iv.Initialize("")
   Dim xiv As B4XView = iv
   pnl.AddView(xiv, 0, 0, pnl.Width, pnl.Height)
   xiv.SendToBack
   bc.SetBitmapToImageView(bc.Bitmap, xiv)
End Sub
Example:
B4X:
SetGradientBackground(Panel1, Array As Int(xui.Color_Red, xui.Color_Green, xui.Color_Blue), "TOP_BOTTOM")
 
Top