0

Ok so in here i have this xml like this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

     <RelativeLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerHorizontal="true"
         android:layout_centerVertical="true"
         android:layout_margin="30dp"
         android:background="@drawable/backgroundblankred" >

         <Button
             android:id="@+id/button1"
             android:layout_width="100dp"
             android:layout_height="100dp"
             android:background="@drawable/eeclevelfirstblackbuton" />

     </RelativeLayout>

</RelativeLayout>

it is show like this

enter image description here

the button was inside a layout so i want to make the button when clicked it will be like this

enter image description here

the button randomly move inside the layout there every second and not out from the layout that can make app force close

Second question, how to set the fast of random button move? like set the buton random move every 2 second and increase it more fast, it is possible set the button move faster?

i just have this code now

public class tested extends Activity {

Button buttonblack;
int score=0;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.tested);


    buttonblack = (Button)findViewById(R.id.black1);
    buttonblack.setOnClickListener(new View.OnClickListener() {

              public void onClick(View v){
             //If clicked, Move the button randomly
                                         }                
            });

Anyone can help with some code? Thanks in Advance.

RichFounders
  • 169
  • 21

2 Answers2

2

Hope this video helps for the 1st part: Android App Development for Beginners - 34 - Animations and Transitions.

For second part you may use a loop like this:

while(true){
    delay(time);//time can be taken input from any EditText.
    placeButton(x,y); // x and y are random numbers
}
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
0

generate random integers within your view area random number

1)this should be the pseudo code you are looking for

int x = rnd(view.getX(), view.getX()+view.getWidth());
int y = rnd(view.getY(), view.getY()+view.getHeight());
view.setTranslationX(x);
view.setTranslationY(y);

2) use a thread with a sleep duration you can modify, the ui changes needs be called on the main thread, use a handler or asynctask for that

Community
  • 1
  • 1
ir2pid
  • 5,604
  • 12
  • 63
  • 107