1

I wanted to know how to randomly select a button in android. Like for example there got 4 button, I want the application to randomly choose a button from them and do some actions to it. Here is my code:

Button start;
ImageButton btn1, btn2, btn3, btn4, btn5;
Random random = new Random();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_memory);
    start = (Button)findViewById(R.id.button1);
    start.setOnClickListener(this);
    btn1 = (ImageButton)findViewById(R.id.imageButton1);
    btn2 = (ImageButton)findViewById(R.id.imageButton2);
    btn3 = (ImageButton)findViewById(R.id.imageButton3);
    btn4 = (ImageButton)findViewById(R.id.imageButton4);

}

ImageButton[] all= {btn1, btn2, btn3, btn4};

@Override
public void onClick(View v) {
    if (v == start)
    {
        btn5 = all[random.nextInt(all.length)];
        btn5.setBackgroundColor(Color.RED);
    }
}

If I change to this it work perfectly but then it will only be btn1 and not randomly select.

@Override
public void onClick(View v) {
    if (v == start)
    {
        btn5 = btn1;
        btn5.setBackgroundColor(Color.RED);
    }
}
Bar Akiva
  • 1,089
  • 1
  • 11
  • 23
user3153613
  • 255
  • 1
  • 2
  • 10

5 Answers5

2

You need to place this line inside your onCreate() method:

all= {btn1, btn2, btn3, btn4};

As it is now:

ImageButton[] all= {btn1, btn2, btn3, btn4};

It runs in the contractor time where all the buttons are null. Than in the onCreate you assign the buttons for new variables, how ever this does not change ImageButton. Alternativly you could wrote:

ImageButton[0] = (ImageButton)findViewById(R.id.imageButton1);
ImageButton[1] = (ImageButton)findViewById(R.id.imageButton2);
ImageButton[2] = (ImageButton)findViewById(R.id.imageButton3);
ImageButton[3] = (ImageButton)findViewById(R.id.imageButton4);
Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
2

all must be set after you have set btn1 etc: Otherwise it'll be an array of nulls.

    btn1 = (ImageButton)findViewById(R.id.imageButton1);
    btn2 = (ImageButton)findViewById(R.id.imageButton2);
    btn3 = (ImageButton)findViewById(R.id.imageButton3);
    btn4 = (ImageButton)findViewById(R.id.imageButton4);
    all= {btn1, btn2, btn3, btn4}; //here
}

ImageButton[] all; //not here
weston
  • 54,145
  • 21
  • 145
  • 203
0

How do I generate random integers within a specific range in Java?

check the above reference for generating random number then use

switch(number)
{
case 1:
button.performClick()

.....
}
Community
  • 1
  • 1
CoolMonster
  • 2,258
  • 25
  • 50
0

do this way

 ImageButton[] all= {btn1, btn2, btn3, btn4};

 Random rand = new Random();
 ImageButton randomBtn = all[rand.nextInt(all.length)];
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
0

There is following code which i have run currently:

    Button start;
    ImageButton btn1, btn2, btn3, btn4, btn5;
    Random random;

    int[] all;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        random = new Random();

        start = (Button)findViewById(R.id.button1);

        start.setOnClickListener(this);

        btn1 = (ImageButton)findViewById(R.id.ImageButton01);
        btn2 = (ImageButton)findViewById(R.id.ImageButton02);
        btn3 = (ImageButton)findViewById(R.id.ImageButton03);
        btn4 = (ImageButton)findViewById(R.id.ImageButton04);

        all = new int[]{R.id.ImageButton01,R.id.ImageButton02,R.id.ImageButton03,R.id.ImageButton04};
    }



    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button1)
        {
            int id = all[random.nextInt(all.length)];
            findViewById(id).setBackgroundColor(Color.BLACK);
        }
    }
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437