Android Question ImageView displaying .png file

aedwall

Active Member
Licensed User
Longtime User
UPDATE: When I moved code to "Activity_Create", then it started working. Apparently it is not a good idea to set everything up within a button - it has to be set up outside the button, then you can manipulate the .Visible method. So ignore the below except to learn what NOT to do.

I can display a .png file using ImageView, but I can't get rid of the thing once it is displayed. I have tried everything I can think of. It has to be simple. Please, what am I missing? Thank you.

Why the .Visible command doesn't work is baffling. Same with .Left.

The below code shows some of what I have experimented with - nothing removes the symbols.png graphic.

B4X:
    Dim imv As ImageView
    Dim Bitmaps, bmpDot, bmpSymbols As Bitmap
   
    bmpSymbols.Initialize(File.DirAssets,"symbols.png")
    bmpDot.Initialize(File.DirAssets,"dot.png")

    imv.Initialize("")
   
    If sym_flag = 0 Then
        imv.Initialize("")
       
        'imv.SetBackgroundImage(LoadBitmap(File.DirAssets, "symbols.png"))
       
        imv.Bitmap = bmpSymbols
       
        imv.Left = 10
           
        Activity.AddView(imv, 10, 0, 100%x, 100%y)
       
        imv.SetLayout(0, 0, 100%x, 100%y)
       
        imv.Visible = True
           
        sym_flag = 1      
    Else
        imv.Initialize("")
       
        imv.Invalidate
       
        'imv.SetBackgroundImage(LoadBitmap(File.DirAssets, "dot.png"))
       
        imv.Bitmap = bmpDot
       
        imv.Left = 1000
           
        Activity.AddView(imv, 1000, 0, 100%x, 100%y)
       
        imv.SetLayout(1000, 0, 0, 0)
       
        imv.RemoveView
       
        imv.Visible = False

        sym_flag = 0
     End If
 
Last edited:

klaus

Expert
Licensed User
Longtime User
Unfortunately you don't give enough information.
What exactly do you want to do?
Where is your code?
In Activity_Create or in a Button_Click event?
It would be much easier for us to help you if you posted a small example project showing the problem.
 
Upvote 0

aedwall

Active Member
Licensed User
Longtime User
I gave the code in my post. I failed to mention that the code was in the button event. Sorry. I thought the matter was so simple that someone could provide a simple example. I looked in various documentation including the PennyPress pdf and there is nothing there. Seems a shame that simple examples cannot be found. Too much information to sort through - like finding a needle in a haystack. I did update my original post after I discovered that the various code belongs in the Activity_Create part of the project, not in a button event. Some code has to go somewhere and other code has to go somewhere else. I am used to VB and it doesn't require the same sort of separation. Anyway, I am satisfied because it is now working. But it took several hours to get there. That's probably my fault. I did my best. Thank you for your hep.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
i think your problem is that imv.Initialize("") creates a new object and u add it again with Activity.AddView
if u will toggle imv visible or not use the same instance.
u can store in Globals
if the imv As ImageView cames from designer (i think not because u add it to the activity!?) do not Initialize it again. LoadLayout Initialize it for you.
 
Upvote 0

aedwall

Active Member
Licensed User
Longtime User
i think your problem is that imv.Initialize("") creates a new object and u add it again with Activity.AddView
if u will toggle imv visible or not use the same instance.
u can store in Globals
if the imv As ImageView cames from designer (i think not because u add it to the activity!?) do not Initialize it again. LoadLayout Initialize it for you.

Thank you for your help. I admit, I get a little confused with the difference between .Initialize and .AddView. I don't understand why both are needed (maybe they aren't), or if they are. But I did get it working, so I think you are right. I don't work with b4A every day, so rust seeps in.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
about Initialize, in c# its
B4X:
var a = new YourClass();
in b4x u need it to Initialize own classes/objects once.
if you make your gui design from code at runtime u can add these elements to a parent element. (.AddView)
if u use the formular designer u need only LoadLayout, and in the designer u have a context menu which add the dim x as y into the current open activity or class.
the possible events can also add from within the designer.
 
Upvote 0
Top