Android Question Using variable as image name

Reproretro

Member
Licensed User
Longtime User
Apologies if this has been asked before.

I have about 100 images: icon_000, icon_001 to icon_100

A number will be entered and the appropriate icon displayed. Is there any way to Dim, Initialize and use them as a variable, rather than repeating everything 100 times?
 

sorex

Expert
Licensed User
Longtime User
you can build your name with

name="icon_"&x

where x can be edtNumer.text or a loop counter
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
maybe it was wrong advice as I thought icon_000 was the filename of the icon.

It all depends on how the app will work, it if just shows 1 icon after a number is entered than the hint is valid
and you can use loadbitmap with 1 imageview and the variable filename instead of 100 imageviews that you hide and just show 1.
 
Upvote 0

Reproretro

Member
Licensed User
Longtime User
Sorry, my bad
They are icon_000.gif, icon_002.gif ... icon_100.gif

And I want to avoid

Dim icon000, icon001, ... icon100 as bitmap

icon_000.Initialize(File.DirAssets,"icon_000.gif")
icon_000.Initialize(File.DirAssets,"icon_001.gif")
...
icon_000.Initialize(File.DirAssets,"icon_100.gif")


Select x
case x = 0
imgIcon.bitmap = icon_000
case x = 1
imgIcon.bitmap = icon_001
...
case x = 100
imgIcon.bitmap = icon_100
end select
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
B4X:
Dim icon as imageview
dim nt as string
icon.initialize("")

nt=1
nt="00"&nt
nt=nt.SubString(nt.Length-3)
icon.bitmap=loadbitmap(file.dirassets,"icon_"&nt&".gif")

changing nt to 1, 64 or 397 will always remain in the 3 digit format.
 
Upvote 0

Reproretro

Member
Licensed User
Longtime User
I tried that but it produces a compile error - src\b4a\example\main.java:956: error: incompatible types: String cannot be converted to Bitmap
 
Upvote 0

Reproretro

Member
Licensed User
Longtime User
Thank you for that. I will work on it tomorrow, it's past my bedtime.
But thanks for confirming it works, so I know I have to look elsewhere my code for the problem
 
Upvote 0
Top