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

Discussion in 'Android Questions' started by gz7tnn, May 24, 2015.

  1. gz7tnn

    gz7tnn Member Licensed 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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Assuming that all the image views are in the same panel:
    Code:
    Sub Globals
    Private ImageViews As List

    Sub Activity_Create

    ImageViews.Initialize
    For Each iv As ImageView in Panel1
      ImageViews.Add(iv)
    Next
    End Sub

    'usage:
    Dim iv As ImageView = ImageViews.Get(1'get the second image view
     
  3. DonManfred

    DonManfred Expert Licensed 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

    Code:
    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(10As 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
     

    Attached Files:

  4. DonManfred

    DonManfred Expert Licensed User

    and there are no other views inside this panel. Or did i missunderstood this?
    Does
    Code:
    For Each iv As ImageView in Panel1
    ONLY grab the ImageViews out of the panel and no other views?
     
  5. gz7tnn

    gz7tnn Member Licensed User


    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: May 24, 2015
  6. DonManfred

    DonManfred Expert Licensed User

    What exactly is the question?
    What do you want to archieve with the combined version of erels and mine?
     
  7. gz7tnn

    gz7tnn Member Licensed User

    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
     
  8. DonManfred

    DonManfred Expert Licensed User

    Maybe this code will help you.
    Code:
    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(10As 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
     

    Attached Files:

  9. gz7tnn

    gz7tnn Member Licensed User


    Will give that a try over the next few days. Thanks
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice