iOS Question [B4XPages] BarButton Image in Navbar goes in center

Mike1970

Well-Known Member
Licensed User
Longtime User
Hi everyone,
I'm trying to put a BarButton in right of the navbar with an image...
i used the function found here and here.

But the image goes always in the center
(actually is like the image is large as all the navbar)

the code is
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    
    B4XPages.GetNativeParent(Me).TopRightButtons = Array(CreateFABarImg("profile.png", "profile"))
End Sub

Private Sub CreateFABarImg(sFile As String, tag As Object) As BarButton
    Dim bb As BarButton
    Dim b As ImageView
    b.Initialize("img")
    b.Color = Colors.red
    b.ContentMode = b.MODE_FIT
    b.Bitmap=LoadBitmap(File.DirAssets,sFile)
    b.Tag = tag
    bb.InitializeCustom(b)
    Return bb
End Sub

And the result is
IMG_1023.jpg



The same if i do it from the Designer.


Thanks in advance
 
Solution
Use this code:
B4X:
Private Sub CreateFABarImg(sFile As String, tag As Object) As BarButton
    Dim bb As BarButton
    Dim b As ImageView
    b.Initialize("img")
    b.ContentMode = b.MODE_FIT
    b.Bitmap = LoadBitmap(File.DirAssets, sFile)
    b.Tag = tag
    Dim pnl As B4XView = xui.CreatePanel("")
    pnl.SetLayoutAnimated(0, 0, 0, 35dip, 35dip)
    pnl.AddView(b, 0, 0, pnl.Width, pnl.Height)
    pnl.SetColorAndBorder(xui.Color_Transparent, 0, xui.Color_Transparent, pnl.Height / 2)
    bb.InitializeCustom(pnl)
    Return bb
End Sub

Erel

B4X founder
Staff member
Licensed User
Longtime User
Use this code:
B4X:
Private Sub CreateFABarImg(sFile As String, tag As Object) As BarButton
    Dim bb As BarButton
    Dim b As ImageView
    b.Initialize("img")
    b.ContentMode = b.MODE_FIT
    b.Bitmap = LoadBitmap(File.DirAssets, sFile)
    b.Tag = tag
    Dim pnl As B4XView = xui.CreatePanel("")
    pnl.SetLayoutAnimated(0, 0, 0, 35dip, 35dip)
    pnl.AddView(b, 0, 0, pnl.Width, pnl.Height)
    pnl.SetColorAndBorder(xui.Color_Transparent, 0, xui.Color_Transparent, pnl.Height / 2)
    bb.InitializeCustom(pnl)
    Return bb
End Sub
 
Upvote 0
Solution

Mike1970

Well-Known Member
Licensed User
Longtime User
Use this code:
B4X:
Private Sub CreateFABarImg(sFile As String, tag As Object) As BarButton
    Dim bb As BarButton
    Dim b As ImageView
    b.Initialize("img")
    b.ContentMode = b.MODE_FIT
    b.Bitmap = LoadBitmap(File.DirAssets, sFile)
    b.Tag = tag
    Dim pnl As B4XView = xui.CreatePanel("")
    pnl.SetLayoutAnimated(0, 0, 0, 35dip, 35dip)
    pnl.AddView(b, 0, 0, pnl.Width, pnl.Height)
    pnl.SetColorAndBorder(xui.Color_Transparent, 0, xui.Color_Transparent, pnl.Height / 2)
    bb.InitializeCustom(pnl)
    Return bb
End Sub
Thanks it works like expected :D
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
Slightly edited to specify the callback name:

B4X:
Private Sub CreateFABarImg(sFile As String, callback As String, tag As Object) As BarButton
    Dim bb As BarButton
    Dim b As ImageView
    b.Initialize(callback)
    b.ContentMode = b.MODE_FIT
    b.Bitmap = LoadBitmap(File.DirAssets, sFile)
    b.Tag = tag
    
    Dim pnl As B4XView = xui.CreatePanel("")
    pnl.SetLayoutAnimated(0, 0, 0, 35dip, 35dip)
    pnl.AddView(b, 0, 0, pnl.Width, pnl.Height)
    pnl.SetColorAndBorder(xui.Color_Transparent, 0, xui.Color_Transparent, pnl.Height / 2)
    bb.InitializeCustom(pnl)
    Return bb
End Sub

So you can do like so:

Create:
B4X:
B4XPages.GetNativeParent(Me).TopRightButtons = Array(CreateFABarImg("person.jpg", "btnProfile", "profile"))

Handle the events:
B4X:
Sub btnProfile_Click
    B4XPages.ShowPage("Profile")
End Sub
 
Upvote 0
Top