Android Question how do I create an array of imageview from designer

gz7tnn

Member
Licensed User
Longtime User
Hi all

Background:
I need to place 12 small images onto a panel, but as the location of these is based on a client provided artwork, their position is essentially random.

So far I have achieved the placement of the images by using Designer, and now am looking for the way to fill them with the final icon. I has hoped to be able to

Each image will be initially filled with a transparent gif, then progressively, based on a set of criteria, I want to fill the images with a small icon.

I have achieved this via code in another app which had a constant layout where I was able to set the positions in the code by doing the following

Dim img(15) As ImageView
...
img(n).initialize("img")
...

img(x).Bitmap=LoadBitmap(File.DirAssets ,"placeholder.png")

But cannot find the way to take the Designer generated ImageView1 / ImageView2 etc images I have placed on the Layout and put them into a usable array.

Any help would be appreciated.
As my understanding in some areas is low, I would appreciate to know what goes in Globals / Process Globals / etc -- where appropriate
 

DonManfred

Expert
Licensed User
Longtime User
Asuming you have named the imageviews
ImageView1 to ImageView10
in the designer.
You need to build a global list with the imageviews like in this example

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private ImageView1 As ImageView
    Private ImageView2 As ImageView
    Private ImageView3 As ImageView
    Private ImageView4 As ImageView
    Private ImageView5 As ImageView
    Private ImageView6 As ImageView
    Private ImageView7 As ImageView
    Private ImageView8 As ImageView
    Private ImageView9 As ImageView
    Private ImageView10 As ImageView
    Dim iv(10) As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    ' Now all IVs are loaded to the positions designed in the designer.
    ' Put them into a global list (after loading the layout)
    iv = Array As ImageView (ImageView1, ImageView2, ImageView3, ImageView4, ImageView5, ImageView6, ImageView7, ImageView8, ImageView9, ImageView10 )
    '
    ' Let´s create ONE Placeholderbitmap instead of loading 10
    Dim bmp As Bitmap
    bmp.Initialize(File.DirAssets ,"placeholder.png")
    For i = 0 To 9
        iv(i).Bitmap = bmp
    Next
End Sub
 

Attachments

  • tenimageviews.zip
    14.3 KB · Views: 229
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Assuming that all the image views are in the same panel
and there are no other views inside this panel. Or did i missunderstood this?
Does
B4X:
For Each iv As ImageView in Panel1
ONLY grab the ImageViews out of the panel and no other views?
 
Upvote 0

gz7tnn

Member
Licensed User
Longtime User
Asuming you have named the imageviews
ImageView1 to ImageView10
in the designer.
You need to build a global list with the imageviews like in this example

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private ImageView1 As ImageView
    Private ImageView2 As ImageView
    Private ImageView3 As ImageView
    Private ImageView4 As ImageView
    Private ImageView5 As ImageView
    Private ImageView6 As ImageView
    Private ImageView7 As ImageView
    Private ImageView8 As ImageView
    Private ImageView9 As ImageView
    Private ImageView10 As ImageView
    Dim iv(10) As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    ' Now all IVs are loaded to the positions designed in the designer.
    ' Put them into a global list (after loading the layout)
    iv = Array As ImageView (ImageView1, ImageView2, ImageView3, ImageView4, ImageView5, ImageView6, ImageView7, ImageView8, ImageView9, ImageView10 )
    '
    ' Let´s create ONE Placeholderbitmap instead of loading 10
    Dim bmp As Bitmap
    bmp.Initialize(File.DirAssets ,"placeholder.png")
    For i = 0 To 9
        iv(i).Bitmap = bmp
    Next
End Sub


Hi Gents
With DonManfreds code I have it working the way I envisioned. I can now:
design the layout in Designer with scripts
Create an array of images
assign an initial default image to each one in the array
then set each one to a different image based on the criteria in my program.

In my case all the imageviews (1 thru 10) are on the same panel, so if there was a way of integrating Erel's code ( For Each iv As ImageView in Panel1) into the code would be good.

Thanks to both of you for your quick response.
 
Last edited:
Upvote 0

gz7tnn

Member
Licensed User
Longtime User
What exactly is the question?
What do you want to archieve with the combined version of erels and mine?

I was wondering if maybe the line in yours -- iv = ArrayAsImageView (ImageView1, ImageView2, ImageView3, ImageView4, .. .. ..)
was able to be done in some way by using Erels.. For Each iv As ImageView in Panel1
ie. loop thru the imageviews on the panel and add them into the array list.

More me just wondering if it could be done that way. Thinking about it, there may be no guarantee that order of the items in the array was the same as the order that I wanted to use them in. ie the "for each" may add them in a random order and not ImageView1, ImageView2, ImageView3, ...

But your code has given me exactly what I needed. Thanks
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Maybe this code will help you.
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private ImageView1 As ImageView
    Private ImageView2 As ImageView
    Private ImageView3 As ImageView
    Private ImageView4 As ImageView
    Private ImageView5 As ImageView
    Private ImageView6 As ImageView
    Private ImageView7 As ImageView
    Private ImageView8 As ImageView
    Private ImageView9 As ImageView
    Private ImageView10 As ImageView
    Private ImageViews As List
    Dim images(10) As ImageView
    Private Panel1 As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    ' Now all IVs are loaded to the positions designed in the designer.
    ' Put them into a global list (after loading the layout)
    'iv = Array As ImageView (ImageView1, ImageView2, ImageView3, ImageView4, ImageView5, ImageView6, ImageView7, ImageView8, ImageView9, ImageView10 )
    '
    ' Let´s create ONE Placeholderbitmap instead of loading 10
    Dim bmp As Bitmap
    bmp.Initialize(File.DirAssets ,"placeholder.png")
    ImageViews.Initialize
    For Each iv As ImageView In Panel1
        ImageViews.Add(iv)
    Next

    For i = 0 To ImageViews.Size-1
        Dim img As ImageView = ImageViews.Get(i)
        img.Bitmap = bmp       
        images(i) = img
    Next
   
    For i = 0 To 9
        images(i).Height = Rnd(50dip,100dip)
        images(i).Width = Rnd(50dip,100dip)
    Next
   
End Sub
 

Attachments

  • tenimageviews2.zip
    14.6 KB · Views: 255
Upvote 0

gz7tnn

Member
Licensed User
Longtime User
Maybe this code will help you.
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private ImageView1 As ImageView
    Private ImageView2 As ImageView
    Private ImageView3 As ImageView
    Private ImageView4 As ImageView
    Private ImageView5 As ImageView
    Private ImageView6 As ImageView
    Private ImageView7 As ImageView
    Private ImageView8 As ImageView
    Private ImageView9 As ImageView
    Private ImageView10 As ImageView
    Private ImageViews As List
    Dim images(10) As ImageView
    Private Panel1 As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    ' Now all IVs are loaded to the positions designed in the designer.
    ' Put them into a global list (after loading the layout)
    'iv = Array As ImageView (ImageView1, ImageView2, ImageView3, ImageView4, ImageView5, ImageView6, ImageView7, ImageView8, ImageView9, ImageView10 )
    '
    ' Let´s create ONE Placeholderbitmap instead of loading 10
    Dim bmp As Bitmap
    bmp.Initialize(File.DirAssets ,"placeholder.png")
    ImageViews.Initialize
    For Each iv As ImageView In Panel1
        ImageViews.Add(iv)
    Next

    For i = 0 To ImageViews.Size-1
        Dim img As ImageView = ImageViews.Get(i)
        img.Bitmap = bmp      
        images(i) = img
    Next
  
    For i = 0 To 9
        images(i).Height = Rnd(50dip,100dip)
        images(i).Width = Rnd(50dip,100dip)
    Next
  
End Sub


Will give that a try over the next few days. Thanks
 
Upvote 0
Top