Android Question random image with no repeat in button

Discussion in 'Android Questions' started by juniorteacher, May 5, 2015.

  1. juniorteacher

    juniorteacher Member Licensed User

    after fix my problems with random without repeat in button text, i want add image in button based on the button text, but get failed

    Code:
    Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim btn(8As Button
    Dim i, k, x,j As Int
    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")

    For i = 0 To 7
    btn(i).Initialize(
    "btn")
    Activity.AddView(btn(i),5%x,15dip + i*75,80%x,60dip)
    btn(i).Text = i
    Next
    For j = 0 To 7
    k = 
    Rnd(j, 8'error in this section
    x = btn(j).Text
    btn(j).SetBackgroundImage(
    LoadBitmap(File.DirAssets,j & ".png"))
    btn(j).Text = btn(k).Text
    btn(k).SetBackgroundImage(
    LoadBitmap(File.DirAssets,k & ".png"))
    btn(k).Text = x
    Next

    End Sub
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Change 75 with 75dip.

    Which error do you get?
     
  3. juniorteacher

    juniorteacher Member Licensed User

    the image can't random,
     
  4. asales

    asales Well-Known Member Licensed User

    I tested the code and it works.
    See the example in attached:
     

    Attached Files:

  5. juniorteacher

    juniorteacher Member Licensed User

    the number random, but the image can't random :)
    i find code for the image can random.
    thanks for trying help,.
     
  6. asales

    asales Well-Known Member Licensed User

    Fixed. Try this code:
    Code:
    For j = 0 To 7
            k = 
    Rnd(08)
            btn(j).TextColor = 
    Colors.Blue
            btn(j).TextSize = 
    18
            btn(j).SetBackgroundImage(
    LoadBitmap(File.DirAssets,k & ".png"))
            btn(j).Text = k
        
    Next
     
    juniorteacher likes this.
  7. RandomCoder

    RandomCoder Well-Known Member Licensed User

    @juniorteacher if this is the same problem as you previously posted where you want unique and random values then I suggest that you add each image and text to a list and randomly remove from the list and assign to each button as previously suggested. Trust me, it will be far simpler. Have a go and if you encounter problems I'll help.
     
    DonManfred likes this.
  8. juniorteacher

    juniorteacher Member Licensed User

    thanks very much, for the answer
     
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