title: Moving a button (onClick) to a random position on the screen
body: <p>everyone. I'm sort of new to android development and I've ran into a bit of a problem.</p>
  
  <p>As is it's written in the title, I'm trying to create an app in which a button moves to a random location on the screen every time it is clicked. A sort of a "runaway button" thingy.</p>
  
  <p>Here is the XML code.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/activity_main"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:paddingBottom="@dimen/activity_vertical_margin"
          android:paddingLeft="@dimen/activity_horizontal_margin"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin"
          android:background="@color/colorPrimary"
          tools:context="com.example.a4ld.MainActivity"&gt;
  
          &lt;Button
              android:id="@+id/button01"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/button" /&gt;
       &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>And here is the main activity code.</p>
  
  <p>At first the solution seems pretty simple. Just move the random values into the onClick method. That way new coordinates would get generated every time the button is clicked. But whenever I do that the animationListener class cannot reach those values and move the view accordingly so that the clickable area of the button would match its position on the screen.</p>
  
  <pre><code>package com.example.a4ld;
  
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.util.DisplayMetrics;
  import android.view.View;
  import android.view.animation.Animation;
  import android.view.animation.TranslateAnimation;
  import android.widget.Button;
  import android.widget.LinearLayout;
  
  import java.util.Random;
  
     public class MainActivity extends AppCompatActivity {
  
           @Override
           protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_main);
  
               //get display size.
               DisplayMetrics displayMetrics = new DisplayMetrics();
               getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
               final int height = displayMetrics.heightPixels;
               final int width = displayMetrics.widthPixels;
  
               //generates random values within display size range.
               //doesn't work as intended. the button still goes out of bounds sometimes.
               Random n = new Random();
               final int height01 = n.nextInt(height - 0) + 0;
               Random m = new Random();
               final int width01 = m.nextInt(width - 0) + 0;
  
               final Button button01 = (Button) findViewById(R.id.button01);
  
               button01.setOnClickListener(new View.OnClickListener() {
  
                   @Override
                   public void onClick(View v) {
                      //animates button movement.
                      TranslateAnimation animation01 = new TranslateAnimation(0, width01, 0, height01);
                      animation01.setDuration(500);
                      animation01.setAnimationListener(new animationListener());
                      button01.startAnimation(animation01);
                   }
  
                   class animationListener implements Animation.AnimationListener {
                        @Override
                        public void onAnimationStart(Animation animation) {
                        }
                        @Override
                        public void onAnimationEnd(Animation animation) {
                      //moves the view(probably) to match the new button position.
                              LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(button01.getWidth(), button01.getHeight());
                              layoutParams.setMargins(width01, height01, 0, 0);
                              button01.setLayoutParams(layoutParams);
                       }
                       @Override
                       public void onAnimationRepeat(Animation animation) {
                       }
                   }
              });
          }
      }
  </code></pre>
  
  <p>Pardon me for any mistakes. This is my first time asking for help here.</p>
  
  <p>Every bit of help will be greatly appreciated and thank you for your time.</p>