francis Escolar
francis Escolar

Reputation: 9

Image button in android pressed and get a random image in an array sequentially

public abstract class MainActivity extends Activity  implements View.OnClickListener{

    private ImageButton bt1;
    public int index =0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Drawable[][] images = new Drawable[4][16];
        bt1 = (ImageButton) findViewById(R.id.im_bt1);
        bt1.setImageDrawable(images[index][(int)(Math.random()*16)]);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                index++;
                ((ImageButton)v).setImageDrawable(images[index][(int)(Math.random()*16)]);

            }
        });

    }


    Red RR1 = new Red(R.drawable.rr1);
    Red RY1 = new Red(R.drawable.ry1);
    Red RG1 = new Red(R.drawable.rg1);
    Red RB1 = new Red(R.drawable.rb1);
    Green GB1 = new Green(R.drawable.greenblue);
    Green GG1 = new Green(R.drawable.greengreen);
    Green GR1 = new Green(R.drawable.greenred);
    Green GY1 = new Green(R.drawable.greenyellow);
    Yellow YY1 = new Yellow(R.drawable.yellowyellow);
    Yellow YB1 = new Yellow(R.drawable.yellowblue);
    Yellow YG1 = new Yellow(R.drawable.yellowgreen);
    Yellow YR1 = new Yellow(R.drawable.yellowred);
    Blue BR1= new Blue(R.drawable.br1);
    Blue BB1=new Blue(R.drawable.bb1);
    Blue BY1=new Blue(R.drawable.by1);
    Blue BG1= new Blue(R.drawable.bg1);

    Red[] Redarray = new Red[]{
            RR1,RY1,RG1,RB1
    };
    Green[] Greenarray = new  Green[]{
            GR1,GB1,GY1,GG1
    };

    Blue[] Bluearray = new  Blue[]{
      BB1,BY1,BG1,BR1
    };

    Yellow[] Yellowarray = new Yellow[]{
            YB1,YG1,YR1,YY1
    };


}

I have an ImageButton in my layout and I want to press the button and get another random ImageButton from an array and the get another when I press that one, but I want it to show sequentially on the arrays that I made.

array of images 1 array of images 2 array of images 3 array of images 4

Image Button from array1 to another random Image Button from array2 to another Image Button from array 3...

edit: so I made a class of all the arrays and it's not working.. I'm struggling to make an array of arrays.. can u guys check my code? thanks..

Upvotes: 0

Views: 533

Answers (2)

hughjdavey
hughjdavey

Reputation: 1140

The problem seems to be that you were never resetting the index variable. You could solve like

...
final Random random = new Random();
...
@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    bt1 = (ImageButton) findViewById(R.id.im_bt1);
    bt1.setImageDrawable(randomFromArray(images[index]));

    bt1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (++index == 4) {
                // reset index if it is too high after increment
                index = 0;
            }
            ((ImageButton) v).setImageDrawable(randomFromArray(images[index]));
}

public <T> T randomFromArray(final T[] array) {
    return array[random.nextInt(array.length)];
}

Upvotes: 0

rFleige
rFleige

Reputation: 11

First: I would not change to another ImageButton everytime, but rather just change the displayed image.

Second: You need an array of arrays of ImageButtons (or drawables). Then you need an index for your outer array and need a random value for your inner array.

Drawable[][] images = new Drawable[4][10]; //your 4 outer arrays with 10 drawables to choose randomly from // needs to be a member variable
fillArray();//add your drawables to the arrays here
int index = 0;//needs to be a member variable too
ImageButton button = new ImageButton(this);
button.setImageDrawable(images[index][(int)(Math.random()*10)]);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    index++;
        ((ImageButton)v).setImageDrawable(images[index][(int)(Math.random()*10)]);
    }
});

Upvotes: 1

Related Questions