0

With an activity extending view, in the bottom of the layout, there is a going-left and going-right button at the left and right bottom corner of the screen.

There is an boy imagebutton in the middle of the screen.

I would like to implement in a way that when the user presses the left button, the boy imagebutton button will move to the left (limited to the left of the screen), so as the action for the right button: the boy will move to the right.

I would like to know how the above can be achieved?

I have tried the following in a hope to get the coordinates of the boy at first step

   public void get_boy_location()
   {
          Rect rectf = new Rect();
          image_boy.getLocalVisibleRect(rectf);
          textView_testing.setText( 
                  "\n WIDTH:"   + String.valueOf(rectf.width()) +
                  "\n HEIGHT:"  + String.valueOf(rectf.height()) +
                  "\n left:"    + String.valueOf(rectf.left) +
                  "\n right:"   + String.valueOf(rectf.right) +
                  "\n top:"     + String.valueOf(rectf.top) +
                  "\n bottom:"  + String.valueOf(rectf.bottom) 
                  );
   }

but all the values report 0.

Question: 1. How could we implement a controlling left and right button to move the boy imagebutton horizontally?

  1. How to obtain the coordinates of the imagebutton?

Thanks!!

pearmak
  • 4,979
  • 15
  • 64
  • 122

3 Answers3

0

There is a great explanation on how to move a view around withing the bounds of the screen on this link here:

Where does Android View.scrollTo(x, y) scroll to?

But basicly you will want to use the scrollTo(int x, int y) method

Community
  • 1
  • 1
Brianjs
  • 2,079
  • 2
  • 20
  • 32
0

I instead work it out using image_boy.setX(boy_x); where boy_x is an integer then will increase its value by 1 when the right button is being touched / touching, and deducts its value by 1 when the left is touched / touching.

pearmak
  • 4,979
  • 15
  • 64
  • 122
0

Not sure if this will help you but i was struck with the same problem i was able to do this using these methods, setTranslationX(float) setTranslationY(float)

you can use it Like this

Button button = (button) findViewById(your id); 
button.setTranslationX(a float value);

here's the android documentation that provide more information http://developer.android.com/reference/android/view/View.html#attr_android:translationX

also note that the minimum level of android SDK required is 11

Aniruddha K.M
  • 7,361
  • 3
  • 43
  • 52