Code Module and Image

aaronk

Well-Known Member
Licensed User
Longtime User
Hello,

I have a image on my Main Activity.

Am I am using a code module.

I want to be able to call a sub in the code module and allow it to change the image on the Main Activity.

Does anyone know how to change a image on the Activity from a Code Module ?

I know how to call the sub in the code module but can't seem to work out how to change the image from the code module.

Any ideas ?
 

aaronk

Well-Known Member
Licensed User
Longtime User
Hello,

I tried that but it didn't work.

Here is what I am doing..

Code Module:
Module Name = code1
B4X:
Sub ShowImage (data As String)
 Msgbox (data,"") ' Displays the data in a message box (show_green)
 Log(data) ' writes show_green
 Dim MyImage As ImageView

   MyImage.Initialize (MyImage) ' also tried the Activity Page Name (Main)
   
   If data = "show_green" Then 
      Log("set Image to Green")
      MyImage.Bitmap = LoadBitmap(File.DirAssets,"green.png")
   End If   
End Sub

Activity Module:
Activity Name = Main
B4X:
Sub Globals
 Dim MyImage As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
  Activity.LoadLayout("MainGUI") ' This has just 1 ImageView on it called MyImage, and I have added the image called green.png in there as well
  code1.ShowImage("show_green") ' change the image from the code1 module
End Sub

I have also added the image in the 'Files' from the right side bar in the IDE.

However, the image does not display but it does write 'set Image to Green' in the Log, so I know it is going to that part of the code.

Hope someone can help me out..
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
In Sub ShowImage you need to remove the line
B4X:
Dim MyImage As ImageView
As you have already Dim'ed it as a global.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Hello,

In Sub ShowImage you need to remove the line

B4X:
Dim MyImage As ImageView
As you have already Dim'ed it as a global.

If I remove that then I get an error when I run the app

Error description: Undeclared variable 'MyImage' is used before it was assigned any value.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You should use this code in code1:
B4X:
Sub ShowImage (imv As ImageView, data As String)
  Msgbox (data,"") ' Displays the data in a message box (show_green)
  Log(data) ' writes show_green

  If data = "show_green" Then 
    Log("set Image to Green")
    imv.Bitmap = LoadBitmap(File.DirAssets,"green.png")
  End If    
End Sub
and in Main this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
  Activity.LoadLayout("MainGUI") ' This has just 1 ImageView on it called MyImage, and I have added the image called green.png in there as well
  code1.ShowImage(MyImage, "show_green") ' change the image from the code1 module
End Sub
Best regards.
 
Upvote 0
Top