B4J Question Background error

ibra939

Active Member
Licensed User
Longtime User
i made background change by using combo box but i don't knows the error background not found ?


' Populate the BackgroundComboBox with options for changing background
Dim backgroundOptions As List
backgroundOptions.Initialize
backgroundOptions.Add("Background 1")
backgroundOptions.Add("Background 2")
backgroundOptions.Add("Background 3")

' Set the items for background selection
BackgroundComboBox.Items.AddAll(backgroundOptions)

End Sub

Private Sub SetBackgroundImage(backgroundFile As String)
' Check if the background file exists in the assets folder
If File.Exists(File.DirAssets, backgroundFile) Then
Try

' Load the background image from assets using LoadBitmap
Dim bmp As B4XBitmap = xui.LoadBitmap(File.DirAssets, backgroundFile)
If bmp.IsInitialized Then
BackgroundImageView.SetImage(bmp) ' Set the image directly to the BackgroundImageView
Else
Log("Bitmap is not initialized for: " & backgroundFile)
End If
Catch
Log("Error setting background image: " & backgroundFile)
End Try
Else
Log("Background file not found: " & backgroundFile)
End If
End Sub

' Event handler for changing the background based on the ComboBox selection
Private Sub BackgroundComboBox_SelectedIndexChanged(Index As Int, Value As Object)
If Index = -1 Then Return ' No selection was made

' Select the background image based on the index
Dim selectedBackground As String
Select Case Index
Case 0
selectedBackground = "background1.jpg"
Case 1
selectedBackground = "background2.jpg"
Case 2
selectedBackground = "background3.jpg"
Case Else
Return ' Do nothing if the index is not valid
End Select

' Set the background image based on the selected file
SetBackgroundImage(selectedBackground)


End Sub
 

teddybear

Well-Known Member
Licensed User
Please use [code]code here...[/code] tags when posting code.
Copy the jpg files from DirAssets to DirApp.
The Exists method does not support File.DirAssets.
error.png
 
Upvote 0

zed

Well-Known Member
Licensed User
The tests you do on File.DirAssets are useless. You put the images in the folder.
Remove tests and try again
 
Upvote 0

ibra939

Active Member
Licensed User
Longtime User
where store File.DirApp

from DirAssets to DirApp:
' This code copies the image files from DirAssets to DirApp
Sub CopyBackgroundImages
    Dim backgroundFiles As List
    backgroundFiles.Initialize
    backgroundFiles.Add("background1.jpg")
    backgroundFiles.Add("background2.jpg")
    backgroundFiles.Add("background3.jpg")
    
    ' Loop through the list of image files and copy them to DirApp
    For Each file As String In backgroundFiles
        If Not File.Exists(File.DirApp, file) Then
            File.Copy(File.DirAssets, file, File.DirApp, file)
            Log($"Copied ${file} to DirApp."$)
        End If
    Next
End Sub
 
Upvote 0

ibra939

Active Member
Licensed User
Longtime User
source code:
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
 
Upvote 0

teddybear

Well-Known Member
Licensed User
As zed said, the files of the DirAssets are put into by yourself, you don't need to do test if file is existed.
Either put them to DirApp folder or remove the existed test
 
Upvote 0

ibra939

Active Member
Licensed User
Longtime User
As zed said, the files of the DirAssets are put into by yourself, you don't need to do test if file is existed.
Either put them to DirApp folder or remove the existed test
You mean i put picture on files folder of project ?
 
Upvote 0

teddybear

Well-Known Member
Licensed User
I test and move picture on Objects not working
Just modify your code on post#1 like this DirAssets to DirAPP
B4X:
Private Sub SetBackgroundImage(backgroundFile As String)
    ' Check if the background file exists in the assets folder
    If File.Exists(File.DirApp, backgroundFile) Then
        Try
            ' Load the background image from assets using LoadBitmap
            Dim bmp As B4XBitmap = xui.LoadBitmap(File.DirApp, backgroundFile)
            If bmp.IsInitialized Then
                BackgroundImageView.SetImage(bmp) ' Set the image directly to the BackgroundImageView
            Else
                Log("Bitmap is not initialized for: " & backgroundFile)
            End If
        Catch
            Log("Error setting background image: " & backgroundFile)
        End Try
    Else
        Log("Background file not found: " & backgroundFile)
    End If
End Sub
 
Upvote 0
Top