dimanche 8 décembre 2019

How to position buttons at random by clicking an image button but the image button itself remains at its original position?

The effect that I want in Android Studio is as follows:

CASE: 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. They are not overlapping each other. After the image button is clicked, all the buttons change their positions at random but the image button itself remains at its original position.

Hope you will help me. 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