Thread, handler and activity structure for a bouncing ball animation This is a custom MyActivity class with two custom views (bar and a ball):

public class MyActivity extends Activity {

    // set class variables
    RelativeLayout content;
    public Ball view;
    Bar bar;
    Handler handler;
    BounceLoop thread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        content = (RelativeLayout) findViewById(R.id.content);
    }

    @Override
    public void onWindowFocusChanged(boolean hasChanged) {
        view = new Ball(this);
        bar = new Bar(this);
        Random rand = new Random();
        int min = 50;
        int max = content.getWidth();
        int randNum = rand.nextInt(max-min+1) + min;
        view.setPosition(randNum, min);
        bar.setY(content.getHeight() - 220);
        bar.setMaxWidth(content.getWidth());
        content.addView(bar);
        content.addView(view);
        thread = new BounceLoop();
        thread.start();
        handler = new Handler() {
            public void handleMessage(Message msg) {
                view.setPosition(msg.arg1,msg.arg2);
                view.invalidate();
            }
        };
    }
    @Override
    protected  void onDestroy() {
        super.onDestroy();
        content.removeAllViews();
    }
    @Override
    protected  void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    class BounceLoop extends Thread {
        public boolean running;
        int speed = 1;
        boolean reachedWidth, reachedHeight;
        int x = view.x,y = view.y, maxWidth = content.getWidth(), maxHeight = content.getHeight();
        int radius = 40;

        public void onPause() {
            running = false;
        }
        public boolean isCollision(int x, int y) {
            if( x &gt; bar.x &amp;&amp; x &lt; bar.x + bar.getBarWidth()) {
                if(y &gt;= bar.getY() - 50) {
                    return true;
                }
            }
            return false;
        }
        public void run() {

            running = true;
            Log.d("prevPos", String.valueOf(x + " : " + y));



            while(running) {
                if(reachedWidth) {
                    x -= speed;
                } else {
                    x += speed;
                }

                if(reachedHeight) {
                    y -= speed;
                } else {
                    y += speed;
                }

                if( x &gt; maxWidth - radius) {
                    reachedWidth = true;
                } else if ( x &lt; 0 + radius) {
                    reachedWidth = false;
                }

                if( y &gt; maxHeight - radius || isCollision(x,y)) {
                    reachedHeight = true;
                } else if ( y &lt; 0 + radius) {
                    reachedHeight = false;
                }
                try {
                    Thread.sleep(1);
                    Message msg = new Message();
                    msg.arg1 = x;
                    msg.arg2 = y;
                    handler.sendMessage(msg);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


How do I re-construct it better and how can I put thread class outside? I really don't like having the thread class inside the activity class. The code seems to be working for now, but I really want to make it better! What can I do about it?
I don't have time to do a full review, but here are some pointers.


Don't use raw access to members (view.x), but always use getters/setters instead.  It has been a Java convention for about 15 years.  Also, you should declare all your members as private.
Don't do many assignements on a single line (int x = view.x,y = view.y, ...).  Again, it does work, but the Java convention is to have one assignment per line.
Don't have Ball being a member of MyActivity and MyActivity being a member of Ball.  That is spaghetti code.
Avoid using Thread in Android, or Java.  There are cleaner and less dangerous patterns for working with concurrent code. For Android, you should look into AsyncTask; for example this.  You can probably find a more appropriate pattern if you look harder.
I haven't touch Android for a while, but I don't think onWindowFocusChanged is the usual callback where everything is started.