-2

I only found ways to change the position of a button when clicking the same button. However, I simply can't find a way to change the position of a button when clicking an image button (different button). When changing their positions, the buttons only position at random at the upper landscape screen and not overlapping one another. Anyone can help?

Activity code:

public class keyboard extends AppCompatActivity implements View.OnClickListener {
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 = findViewById(R.id.txt);
    money = findViewById(R.id.money_display);
    countdown = findViewById(R.id.countdown);
    buttons[0] = findViewById(R.id.bt1);
    buttons[0].setOnClickListener(this);
    buttons[1] = findViewById(R.id.bt2);
    buttons[1].setOnClickListener(this);
    buttons[2] = findViewById(R.id.bt3);
    buttons[2].setOnClickListener(this);
    buttons[3] = findViewById(R.id.bt4);
    buttons[3].setOnClickListener(this);
    buttons[4] = findViewById(R.id.bt5);
    buttons[4].setOnClickListener(this);
    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()));

    imagebutton.setOnClickListener(new View.OnClickListener()
}

@Override
public void onClick (View view) {
    //
}

1 Answers1

-1

Dont know if i understand what you want but this change the position of the button by click on the imageButton

public class MainActivity extends AppCompatActivity {

    private Button buttons[];
    private Button button;
    private Button button3;
    private Button button2;
    private Button button4;
    private Button button5;
    private ImageButton imagebutton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        buttons = new Button[5];
        button = findViewById(R.id.button);
        buttons[0] = button;
        button2 = findViewById(R.id.button2);
        buttons[1] = button2;
        button3 = findViewById(R.id.button3);
        buttons[2] = button3;
        button4 = findViewById(R.id.button4);
        buttons[3] = button4;
        button5 = findViewById(R.id.button5);
        buttons[4] = button5;
        imagebutton = findViewById(R.id.imageButton);
        start();
    }

    public void start(){
        for (int i = 0; i < buttons.length; i++){
            buttons[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });
        }
        imagebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < buttons.length; i++){
                    Button buttonToChange = buttons[i];
                    buttonToChange.setX(x);
                    buttonToChange.setY(y);
                }
            }
        });
    }
}
DoFlamingo
  • 232
  • 1
  • 18
  • I have updated my code: the first line added code-implements... As I need to use this first line of code, I cant use your solution because of the "implemnts View....." Dp you have other solutions to fix this? Thanks. – Sincere Coder Dec 18 '19 at 08:44
  • why you cant use it? – DoFlamingo Dec 18 '19 at 10:28
  • I have implemented the first line of code (implements View.OnClickListener), so I cant use start(); and hence the codes at public void start() cant be used. This is because I must implement this line of code: imagebutton = findViewById(R.id.Imgbtn); } @Override public void onClick(View v) { (pls refer to my original code) – Sincere Coder Dec 18 '19 at 15:25
  • your original code won't work. You just add a click Listen but not for a button – DoFlamingo Dec 19 '19 at 07:47
  • you cant just say onClick you need to add this listener to a button – DoFlamingo Dec 19 '19 at 07:48
  • I added the first line of code so that I dont need to repeat typing set.onclicklistener for every buttons because I got a lot of buttons. Do you have any other solutions? Thanks!!! https://stackoverflow.com/questions/25905086/multiple-buttons-onclicklistener-android This link's answer might make you understand what method I am using. So, in my case, do you have any solution? – Sincere Coder Dec 19 '19 at 16:05