Sub Class_Globals
Private Root As B4XView
Private xui As XUI
Private BackgroundComboBox As ComboBox ' New ComboBox for changing background
Private ImageView1 As ImageView
Private Background As String
'Dim backgroundFile As String
Private BackgroundComboBox As ComboBox ' Dropdown to select background
Private BackgroundImageView As ImageView ' ImageView for the background
End Sub
Public Sub Initialize
' B4XPages.GetManager.LogEvents = True
End Sub
Private Sub B4XPage_Created(Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
' Copy image files from DirAssets to DirApp
CopyJpgFilesToAppDir
' Populate ComboBox
Dim options As List
options.Initialize
options.AddAll(Array("Background 1", "Background 2", "Background 3"))
BackgroundComboBox.Items.AddAll(options)
' Set default background
SetBackgroundImage("background1.jpg")
'
Sub CopyJpgFilesToAppDir
' List of files to copy
Dim files As List = Array("background1.jpg", "background2.jpg", "background3.jpg")
For Each f As String In files
' Check if the file exists in DirAssets and not already copied to DirApp
If File.Exists(File.DirAssets, f) And Not(File.Exists(File.DirApp, f)) Then
File.Copy(File.DirAssets, f, File.DirApp, f)
Log($"Copied ${f} to DirApp"$)
Else If Not(File.Exists(File.DirAssets, f)) Then
Log($"File ${f} does not exist in DirAssets"$)
End If
Next
End Sub
Sub SetBackgroundImage(backgroundFile As String)
' Check if the file exists in DirApp
If File.Exists(File.DirApp, backgroundFile) Then
' Load the image from DirApp
Dim bmp As B4XBitmap = xui.LoadBitmap(File.DirApp, backgroundFile)
' Set the image to ImageView
BackgroundImageView.SetImage(bmp)
Log($"Background changed to: ${backgroundFile}"$)
Else
Log($"Background file ${backgroundFile} not found!"$)
End If
End Sub
Sub BackgroundComboBox_SelectedIndexChanged(Index As Int)
If Index = -1 Then Return
Dim backgroundFile As String
Select Case Index
Case 0
backgroundFile = "background1.jpg"
Case 1
backgroundFile = "background2.jpg"
Case 2
backgroundFile = "background3.jpg"
End Select
SetBackgroundImage(backgroundFile)
End Sub