Android Question Views for Layout only in Sub Global?

D

Deleted member 103

Guest
Hello,

why do you have to define all views for a layout in Sub Global?
I can understand it when the views are in the main layout, but not when it comes to views that are simply added to a customlistview with a layout.

Here is a sub of an Erel example:
B4X:
Sub Process_Globals
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private CLV1 As CustomListView
    Private ImageView1 As B4XView
    Private lblTitle As B4XView
    Private lblContent As B4XView
    Private lblAction1 As Label
    Private lblAction2 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("1")
    Activity.AddMenuItem3("", "refresh", xui.LoadBitmapResize(File.DirAssets, "ic_cached_white_24dp.png", 32dip, 32dip, True), True)
    Activity.AddMenuItem3("", "done", xui.LoadBitmapResize(File.DirAssets, "ic_done_white_24dp.png", 32dip, 32dip, True), True)
    Dim bitmaps As List = Array("pexels-photo-446811.jpeg", "pexels-photo-571195.jpeg", _
        "pexels-photo-736212.jpeg", "pexels-photo-592798.jpeg")
    For i = 1 To 4
        Dim content As String = $"Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."$
        CLV1.Add(CreateItem(CLV1.AsView.Width, $"This is item #${i}"$, bitmaps.Get(i - 1), content), "")
    Next
End Sub

Private Sub CreateItem(Width As Int, Title As String, Image As String, Content As String) As Panel
    Dim p As B4XView = xui.CreatePanel("")
    Dim height As Int = 280dip
    If GetDeviceLayoutValues.ApproximateScreenSize < 4.5 Then height = 310dip
    p.SetLayoutAnimated(0, 0, 0, Width, height)
    p.LoadLayout("Card1")
    
    lblTitle.Text = Title
    lblContent.Text = Content
    SetColorStateList(lblAction1, xui.Color_LightGray, lblAction1.TextColor)
    SetColorStateList(lblAction2, xui.Color_LightGray, lblAction2.TextColor)
    ImageView1.SetBitmap(xui.LoadBitmapResize(File.DirAssets, Image, ImageView1.Width, ImageView1.Height, True))
    Return p
End Sub

The views ImageView1, lblTitle, lblContent, lblAction1 and lblAction2 could also be defined in sub "CreateItem", or what speaks against it?
For example:
B4X:
Private Sub CreateItem(Width As Int, Title As String, Image As String, Content As String) As Panel
    Dim ImageView1 As B4XView
    Dim lblTitle As B4XView
    Dim lblContent As B4XView
    Dim lblAction1 As Label
    Dim lblAction2 As Label

    Dim p As B4XView = xui.CreatePanel("")
    Dim height As Int = 280dip
    If GetDeviceLayoutValues.ApproximateScreenSize < 4.5 Then height = 310dip
    p.SetLayoutAnimated(0, 0, 0, Width, height)
    p.LoadLayout("Card1")
    
    lblTitle.Text = Title
    lblContent.Text = Content
    SetColorStateList(lblAction1, xui.Color_LightGray, lblAction1.TextColor)
    SetColorStateList(lblAction2, xui.Color_LightGray, lblAction2.TextColor)
    ImageView1.SetBitmap(xui.LoadBitmapResize(File.DirAssets, Image, ImageView1.Width, ImageView1.Height, True))
    Return p
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
why do you have to define all views for a layout in Sub Global?
You don't need to define all views. Only the views that you want to access with a variable.
It is technically not possible to set references to local variables, only to globals.

Note that you can access the views by index:
B4X:
p.GetView(1).Text = Title 'assuming that lblTitle is the second child
p.GetView(2).Text = Content 'assuming that lblContent is the third child
 
Upvote 0
D

Deleted member 103

Guest
You don't need to define all views. Only the views that you want to access with a variable.
It is technically not possible to set references to local variables, only to globals.

Note that you can access the views by index:
B4X:
p.GetView(1).Text = Title 'assuming that lblTitle is the second child
p.GetView(2).Text = Content 'assuming that lblContent is the third child
Not very comfortable, but it works, thanks.

But you could also do so if you need to access more properties.

B4X:
Private Sub CreateItem(Width As Int, Title As String, Image As String, Content As String) As Panel
    Dim p As B4XView = xui.CreatePanel("")
    Dim height As Int = 280dip
    If GetDeviceLayoutValues.ApproximateScreenSize < 4.5 Then height = 310dip
    p.SetLayoutAnimated(0, 0, 0, Width, height)
    p.LoadLayout("Card1")

    Dim ImageView1 As B4XView = p.GetView(1)
    Dim lblTitle As B4XView = p.GetView(2)
    Dim lblContent As B4XView = p.GetView(3)
    Dim lblAction1 As Label = p.GetView(4)
    Dim lblAction2 As Label = p.GetView(4)
   
    lblTitle.Text = Title
    lblContent.Text = Content
    SetColorStateList(lblAction1, xui.Color_LightGray, lblAction1.TextColor)
    SetColorStateList(lblAction2, xui.Color_LightGray, lblAction2.TextColor)
    ImageView1.SetBitmap(xui.LoadBitmapResize(File.DirAssets, Image, ImageView1.Width, ImageView1.Height, True))
    Return p
End Sub
 
Upvote 0
Top