mardi 3 décembre 2019

How to make a few buttons position at random when an image button is clicked but the image button itself remains in its original position?

I am having a problem in Android Studio. The effect that I want is as follows:

CASE 1: When an image button is clicked, a few buttons change their positions and position at random. All buttons are located at the upper half of the landscape screen and they are allowed to change their position at random only at the upper half of the landscape screen, but not the lower half screen. After the image button is clicked, all the buttons change their positions at random but the image button itself remains at its original position.

CASE 2: After clicking the image button, I would like to set some conditions that determine whether the buttons are to change their position at random OR NOT.

CASE 3: If the conditions allow the buttons to change their position, I want to set a time interval, meaning that after clicking the image button, the buttons change their positions at random after 1 or 2 seconds.

Hope you all will help me to solve this problem by editing my codes. Thanks in advance!

Main Activity codes:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_keyboard);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

    Button buttons[] = new Button[5];
    text = (TextView) findViewById(R.id.txt);
    buttons[0] = (Button) findViewById(R.id.bt1);
    buttons[0].setOnClickListener(this);
    buttons[1] = (Button) findViewById(R.id.bt2);
    buttons[1].setOnClickListener(this);
    buttons[2] = (Button) findViewById(R.id.bt3);
    buttons[2].setOnClickListener(this);
    buttons[3] = (Button) findViewById(R.id.bt4);
    buttons[3].setOnClickListener(this);
    buttons[4] = (Button) findViewById(R.id.bt5);
    buttons[4].setOnClickListener(this);
    Imgbtn = (ImageButton) findViewById(R.id.Imgbtn);

}
    @Override
    public void onClick(View v) {
    Random rand = new Random();
        View decorView = getWindow().getDecorView();
        int screenWidth = decorView.getWidth();
        int screenHeight = decorView.getHeight();
        v.setX(rand.nextInt(screenWidth - v.getWidth()));
        v.setY(rand.nextInt(screenHeight - v.getHeight()));}


Imgbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(text.getText().toString().equals("COC")){
                LayoutInflater li = getLayoutInflater();
                View layout = li.inflate(R.layout.correct_toast, (ViewGroup) findViewById(R.id.correct_toast_layout_id));
                Toast toast = new Toast(getApplicationContext());
                toast.setView(layout);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setView(layout);//setting the view of custom correct_toast layout
                toast.show();
                MediaPlayer ring= MediaPlayer.create(getApplicationContext(),R.raw.correct);
                ring.start();
            }else{
                LayoutInflater li = getLayoutInflater();
                View layout = li.inflate(R.layout.wrong_toast, (ViewGroup) findViewById(R.id.wrong_toast_layout_id));
                Toast toast = new Toast(getApplicationContext());
                toast.setView(layout);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setView(layout);//setting the view of custom correct_toast layout
                toast.show();
                MediaPlayer ring= MediaPlayer.create(getApplicationContext(),R.raw.wrong);
                ring.start();
            }
        }
    });



Aucun commentaire:

Enregistrer un commentaire