0

i have an image button and i want to move it randomly, i have no idea how to do that, i also tried other questions but was not able to understand, i want my image button to move randomly on screen and it should not move out,it should first be compatible with every device.

I also tried it by making buttons in a gridlayout and then individually making them visible and invisible for 1000ms,but it is not the efficient way... any other way to do so?

public void display(int x){
        String q=score.toString();
       s.setText(q);
            switch (x) {
                case 1: {
                    new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {

                        b1.setVisibility(View.VISIBLE);
                        b1.setEnabled(true);
                        b1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                score++;

                            }
                        });

                    }
                }, 1000);


                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {

                        disable();
                    int x=random();
                    display(x);
                    }
                }, 2000);



            } 
Sarmad Shah
  • 3,725
  • 1
  • 20
  • 42

1 Answers1

0

Instead of creating multiple buttons, you can use 1 and change its position using setX and setY on click.

For example;

new Handler().postDelayed(new Runnable() {

    @Override
    public void run() {
        b1.setX( Enter random number within your screen boundary );
        b1.setY( Enter random number within your screen boundary );
    }
}, 2000);

You'll need to calculate the boundaries of the screen and take into account that setX and setY position applies to the top left of the button's view.

For API lower than 8 you'll need to use LayoutParams as shown below, or you can also refer codes here (How can I dynamically set the position of view in Android?)

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //WRAP_CONTENT param can be FILL_PARENT
params.leftMargin = 206; //XCOORD
params.topMargin = 206; //YCOORD
childView.setLayoutParams(params);
Community
  • 1
  • 1
Red Dango
  • 372
  • 2
  • 7
  • how can i calculate boundaries of the screen(all devices) – Sarmad Shah Jun 16 '16 at 16:08
  • You can try use the method from this [answer from how to get bounding rectangle of view](http://stackoverflow.com/questions/5730240/android-get-bounding-rectangle-of-a-view). First you generate the random X and Y values and then check using the method if it is within the boundary. If it is true, use it to change the location of the button, else generate a new X and Y value :) you can loop this until it generates a usable value – Red Dango Jun 19 '16 at 06:00
  • I suggest you to copy the entire method from that question I linked, then use it like this while(!isViewContains(view,x,y)) however, to solve your error, you have to initialize your v first. Add an id to your layout then call findbyviewid :) – Red Dango Jun 21 '16 at 03:04