sai
sai

Reputation: 558

How to randomize cases in Switch condition?

In this code, I want to shuffle or randomize my 5 cases in the switch condition*After Clicking the Correct button, the Image is appearing fullscreen and going back to the same Case.Instead of this, I want to load Images from remaining cases in Random.*

public class MainActivity extends Activity{


ImageButton ib1,ib2,ib3,ib4,ib5,ib6,ib7,ib8,ib9,ib10,ib11,ib12,ib13,ib14,ib15;
ImageView iv;
Handler handler = new Handler();

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

    iv = (ImageView)findViewById(R.id.img);

    ib1 = (ImageButton)findViewById(R.id.imgbt1);
    ib2 = (ImageButton)findViewById(R.id.imgbt2);
    ib3 = (ImageButton)findViewById(R.id.imgbt3);
    ib4 = (ImageButton)findViewById(R.id.imgbt4);
    ib5 = (ImageButton)findViewById(R.id.imgbt5);
    ib6 = (ImageButton)findViewById(R.id.imgbt6);
    ib7 = (ImageButton)findViewById(R.id.imgbt7);
    ib8 = (ImageButton)findViewById(R.id.imgbt8);
    ib9 = (ImageButton)findViewById(R.id.imgbt9);
    ib10 = (ImageButton)findViewById(R.id.imgbt10);
    ib11 = (ImageButton)findViewById(R.id.imgbt11);
    ib12 = (ImageButton)findViewById(R.id.imgbt12);
    ib13 = (ImageButton)findViewById(R.id.imgbt13);
    ib14 = (ImageButton)findViewById(R.id.imgbt14);
    ib15 = (ImageButton)findViewById(R.id.imgbt15);




    Random rand = new Random();
    x = rand.nextInt(5);

    switch (x) {
    case 1:

        Toast.makeText(getApplicationContext(), "Select red", Toast.LENGTH_SHORT).show();
        ib1.setVisibility(View.VISIBLE);
        ib1.setBackgroundResource(R.drawable.carrot);

        //ib1.setOnClickListener(myListener);
        ib1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        iv.setVisibility(View.GONE);
                        x++;

                    }
                }, 2000);

                iv.setVisibility(View.VISIBLE);
                iv.setBackgroundResource(R.drawable.carrot);

            }
        });

        ib2.setVisibility(View.VISIBLE);
        ib2.setBackgroundResource(R.drawable.ic_launcher);
        //ib2.setOnClickListener(myListener);


        ib3.setVisibility(View.VISIBLE);
        ib3.setBackgroundResource(R.drawable.ic_launcher);
        //ib3.setOnClickListener(myListener);


        break;

    case 2:
        ib4.setVisibility(View.VISIBLE);
        ib4.setBackgroundResource(R.drawable.ic_launcher);
        ib4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        iv.setVisibility(View.GONE);
                        x++;
                    }
                }, 2000);

                iv.setVisibility(View.VISIBLE);
                iv.setBackgroundResource(R.drawable.ic_launcher);
                //ib4.setOnClickListener(myListener);


            }
        });



        ib5.setVisibility(View.VISIBLE);
        ib5.setBackgroundResource(R.drawable.ic_launcher);
        //ib5.setOnClickListener(myListener);


        ib6.setVisibility(View.VISIBLE);
        ib6.setBackgroundResource(R.drawable.ic_launcher);
        //ib6.setOnClickListener(myListener);


        break;


    case 3:
        Toast.makeText(getApplicationContext(), "Select Violet", Toast.LENGTH_SHORT).show();

        ib7.setVisibility(View.VISIBLE);
        ib7.setBackgroundResource(R.drawable.brinjal);
        //ib7.setOnClickListener(myListener);
        ib7.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        iv.setVisibility(View.GONE);
                        x++;
                    }
                }, 2000);

                iv.setVisibility(View.VISIBLE);
                iv.setBackgroundResource(R.drawable.brinjal);
            }
        });


        ib8.setVisibility(View.VISIBLE);
        ib8.setBackgroundResource(R.drawable.ic_launcher);
        //ib8.setOnClickListener(myListener);


        ib9.setVisibility(View.VISIBLE);
        ib9.setBackgroundResource(R.drawable.ic_launcher);
        //ib9.setOnClickListener(myListener);


        break;

    case 4:

        Toast.makeText(getApplicationContext(), "Select Any thing", Toast.LENGTH_SHORT).show();

        ib10.setVisibility(View.VISIBLE);
        ib10.setBackgroundResource(R.drawable.ic_launcher);
        //ib10.setOnClickListener(myListener);
        ib10.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        iv.setVisibility(View.GONE);
                        x++;
                    }
                }, 2000);

                iv.setVisibility(View.VISIBLE);
                iv.setBackgroundResource(R.drawable.ic_launcher);

            }
        });


        ib11.setVisibility(View.VISIBLE);
        ib11.setBackgroundResource(R.drawable.ic_launcher);
        //ib11.setOnClickListener(myListener);


        ib12.setVisibility(View.VISIBLE);
        ib12.setBackgroundResource(R.drawable.ic_launcher);
        //ib12.setOnClickListener(myListener);


        break;



    case 5:
        Toast.makeText(getApplicationContext(), "Select Blue", Toast.LENGTH_SHORT).show();

        ib13.setVisibility(View.VISIBLE);
        ib13.setBackgroundResource(R.drawable.blue_bird);
        //ib13.setOnClickListener(myListener);

        ib13.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        iv.setVisibility(View.GONE);
                        x++;
                    }
                }, 2000);

                iv.setVisibility(View.VISIBLE);
                iv.setBackgroundResource(R.drawable.blue_bird);

            }
        });





        ib14.setVisibility(View.VISIBLE);
        ib14.setBackgroundResource(R.drawable.ic_launcher);
        //ib14.setOnClickListener(myListener);


        ib15.setVisibility(View.VISIBLE);
        ib15.setBackgroundResource(R.drawable.ic_launcher);
        //ib15.setOnClickListener(myListener);


        break;          

    default:
        break;
    }
    }

Thanks.

Upvotes: 0

Views: 201

Answers (1)

Pragnani
Pragnani

Reputation: 20155

Because Button onclick will only execute the code that we have given in onClick, You haven't called your switch statement in the onClick of the button.

Call Switch Statement in a Separate method and call it onclick of the button

on your onClick call like this

int num= rand.nextInt(5);
setRandomImages(num)

Write your switch statement in a separate method like this

public void setRandomImages(int n)
{
//your switch statement here
}

Upvotes: 2

Related Questions