Android Question [Solved] BAXDialog issue

Frankie Lagrange

Member
Licensed User
B4X UI Views are really nice and easy to use multiplatform views. As a recent B4X convert, I am quite impressed.

Here’s an issue that has been giving me a headache though. In the XUI Views Example demo, when you click on the List of Colours button, a dialog pops up and shows a list of colours that you can choose one from. Clicking on one of them will set the colour of the current activity . It’s great (see original demo code at bottom of this post).

But, I have been trying to modify that specific demo so that the dialog displays at the top its window the current activity colour by scrolling to, actually jumping to the index of that colour in the scrollview.

B4X:
'gColourIndex: index of colour to jump to and display at top of dialog window.
CustomListView1.JumpToItem(gColourIndex)

So I added it to btnListOfColors_Click, which now looks like this.

B4X:
#Region List Of Colors custom Dialog

Sub btnListOfColors_Click
    If ListOfColorsPanel.IsInitialized = False Then CreateListOfColorsLayout

    'Displays the current colour at top of window
    CustomListView1.JumpToItem(gColourIndex)

    Wait For (Dialog.ShowCustom(ListOfColorsPanel, "", "", "CANCEL")) Complete (Result As Int)
    If Result = XUI.DialogResponse_Positive Then
        Base.Color = ListOfColorsPanel.Tag
    End If
End Sub

But it will only work from the second time around, never the first time (if you're wondering, the gColourIndex is set correctly in some other part of the code).

Any idea on how to fix this without altering the library itself?

Thank you for any help.
PS: I do have a solution that works, but it involves an ugly hack between btnListOfColors_Click and CustomListView1_VisibleRangeChanged. There must be something simpler.

Original code dealing with List Of Colors custom Dialog:
B4X:
#Region List Of Colors custom Dialog

Sub btnListOfColors_Click
    If ListOfColorsPanel.IsInitialized = False Then CreateListOfColorsLayout
    Wait For (Dialog.ShowCustom(ListOfColorsPanel, "", "", "CANCEL")) Complete (Result As Int)
    If Result = XUI.DialogResponse_Positive Then
        Base.Color = ListOfColorsPanel.Tag
    End If
End Sub

Sub CreateListOfColorsLayout
    ListOfColorsPanel = XUI.CreatePanel("")
    ListOfColorsPanel.SetLayoutAnimated(0, 0, 0, 300dip, 300dip)
    ListOfColorsPanel.LoadLayout("ListTemplate") 'ListTemplate is part of XUI Views library.
    CustomListView1.sv.SetColorAndBorder(XUI.Color_Transparent, 0, 0, 0)
    For Each line As String In File.ReadList(File.DirAssets, "colors.txt")
        Dim s() As String = Regex.Split(":", line)
        Dim Name As String = s(0)
        Dim Color As Int = Bit.Or(0xff000000, Bit.ParseInt(s(1), 16))
        Dim item As B4XView = XUI.CreatePanel("")
        item.SetLayoutAnimated(0, 0, 0, ListOfColorsPanel.Width, 50dip)
        CustomListView1.Add(item, Array(Name,Color))
    Next
End Sub

'lazy creation of the items.
Sub CustomListView1_VisibleRangeChanged (FirstIndex As Int, LastIndex As Int)
    For i = Max(0, FirstIndex - 5) To Min(CustomListView1.Size - 1, LastIndex + 5)
        Dim p As B4XView = CustomListView1.GetPanel(i)
        If p.NumberOfViews = 0 Then
            p.LoadLayout("ListOfColors")
            Dim NameAndValue() As Object = CustomListView1.GetValue(i)
            ColorLabel.Text = NameAndValue(0)
            ColorPanel.Color = NameAndValue(1)
        End If
    Next
End Sub

Sub CustomListView1_ItemClick (Index As Int, Value As Object)
    Dialog.Close(XUI.DialogResponse_Positive)
    Dim NameAndValue() As Object = Value
    ListOfColorsPanel.Tag = NameAndValue(1)
End Sub
#End Region
 
Last edited:

Frankie Lagrange

Member
Licensed User
I'm surprised this issue did not generate any interest. Well anyway, I found out the problem. The B4XDialog does not inform the calling module when the dialog is visible. By slightly modifying the B4XDialog to add an event to that effect the issue is resolved as the code below will have the desired effect:

B4X:
'Displays the current colour at top of window
 CustomListView1.JumpToItem(gColourIndex)

It is possible to get the desired result without editing B4XDialog, but it's an horrible hack that I wanted to shy away from.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Modifying XUI Views code is a mistake. This library is being updated quite frequently.

This code works:
B4X:
Sub btnListOfColors_Click
    If ListOfColorsPanel.IsInitialized = False Then CreateListOfColorsLayout
    Dim sf As Object = Dialog.ShowCustom(ListOfColorsPanel, "", "", "CANCEL")
    Sleep(10)
    CustomListView1.JumpToItem(20)
    Wait For (sf) Complete (Result As Int)
    If Result = XUI.DialogResponse_Positive Then
        Base.Color = ListOfColorsPanel.Tag
    End If
End Sub
It also works with Sleep(0) but I went on the safe side.
 
Upvote 0

Frankie Lagrange

Member
Licensed User
Thank you for this. The simple and elegant solution I was looking for on the first place.

I agree that modifying library code should be of last resort.
 
Last edited:
Upvote 0
Top