How to create game loop for Android Wear app
<p>What I'm trying to do is make an version of the game Simon for android wear.  I don't want to use libgdx, tho I could if I have no other choice.</p>

<p>The way the game works is that the computer shows a button sequence to the player and then the player is to repeat the sequence back.  I've got it all working fine except for I can't seem to animate a sequence to the player.</p>

<p>What I have is a random number generator.  Using the number generated I want to change the color of the button background for a small time, then change it back.  And I loop this a set number of times.  I've tried SystemClock.sleep(500), but it just runs the loop before even showing the app.</p>

<p>The main problem is that everything has to be done inside an onLayoutInflated because of how wear has to choose between round and square faces.  And this has to be done in the onCreate method as far as I know.</p>

<p>Does anyone know a way of doing some sort of game loop and/or animation sequence for android wear.</p>

<p>P.S. I will append my code as soon as I fix it.  I've been fiddling with it a lot.</p>

<pre><code>package com.happypantzinc.memory;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.support.wearable.view.WatchViewStub;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;

import java.util.ArrayList;
import java.util.Random;

public class MainActivity extends Activity {

    private ImageButton mOverlay, mTopLeft, mTopRight, mBotLeft, mBotRight;
    private Boolean isPlayerTurn = false;
    private Boolean isRunning = false;
    private Random random = new Random();
    private int level = 10;
    private ArrayList&lt;Integer&gt; compSeq;
    private ArrayList&lt;Integer&gt; playSeq;

    private float screenW, screenH;

    //setup colors
    final int RED_UP        = Color.rgb(180, 0, 0);
    final int RED_DOWN      = Color.rgb(255, 77, 0);
    final int GREEN_UP      = Color.rgb(0, 180, 0);
    final int GREEN_DOWN    = Color.rgb(0, 255, 77);
    final int BLUE_UP       = Color.rgb(0, 0, 180);
    final int BLUE_DOWN     = Color.rgb(0, 77, 255);
    final int YELLOW_UP     = Color.rgb(180, 180, 0);
    final int YELLOW_DOWN   = Color.rgb(255, 255, 0);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        compSeq = new ArrayList&lt;Integer&gt;();
        playSeq = new ArrayList&lt;Integer&gt;();

        setContentView(R.layout.activity_main);
        final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
        stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
            @Override
            public void onLayoutInflated(WatchViewStub stub) {
                mOverlay = (ImageButton) stub.findViewById(R.id.overlay);
                mTopLeft = (ImageButton) stub.findViewById(R.id.topLeft);
                mTopRight = (ImageButton) stub.findViewById(R.id.topRight);
                mBotLeft = (ImageButton) stub.findViewById(R.id.botLeft);
                mBotRight = (ImageButton) stub.findViewById(R.id.botRight);

                setupButtons();

                setupButtonActions(mTopLeft, GREEN_UP, GREEN_DOWN);
                setupButtonActions(mTopRight, RED_UP, RED_DOWN);
                setupButtonActions(mBotLeft, YELLOW_UP, YELLOW_DOWN);
                setupButtonActions(mBotRight, BLUE_UP, BLUE_DOWN);

                mOverlay.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        isRunning = true;
                        mOverlay.setClickable(false);
                        mOverlay.setVisibility(View.GONE);

                        gameLoop();
                    }
                });

            }
        });

        //get screen size
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();

        display.getSize(size);
        screenW = size.x;
        screenH = size.y;
    }

    private void gameLoop() {
        while(isRunning) {
            //Computer turn
            while (compSeq.size() &lt; level) {
                int val = random.nextInt(4);

                if (!mOverlay.isShown()) {
                    //Computer shows sequence
                    switch (val) {
                        case 0:
                            mTopLeft.getBackground().setColorFilter(GREEN_DOWN, PorterDuff.Mode.MULTIPLY);
                            System.out.println(val);
                            SystemClock.sleep(500);
                            mTopLeft.getBackground().setColorFilter(GREEN_UP, PorterDuff.Mode.MULTIPLY);
                            break;
                        case 1:
                            mTopRight.getBackground().setColorFilter(RED_DOWN, PorterDuff.Mode.MULTIPLY);
                            System.out.println(val);
                            SystemClock.sleep(500);
                            mTopRight.getBackground().setColorFilter(RED_UP, PorterDuff.Mode.MULTIPLY);
                            break;
                        case 2:
                            mBotLeft.getBackground().setColorFilter(YELLOW_DOWN, PorterDuff.Mode.MULTIPLY);
                            System.out.println(val);
                            SystemClock.sleep(500);
                            mBotLeft.getBackground().setColorFilter(YELLOW_UP, PorterDuff.Mode.MULTIPLY);
                            break;
                        case 3:
                            mBotRight.getBackground().setColorFilter(BLUE_DOWN, PorterDuff.Mode.MULTIPLY);
                            System.out.println(val);
                            SystemClock.sleep(500);
                            mBotRight.getBackground().setColorFilter(BLUE_UP, PorterDuff.Mode.MULTIPLY);
                            break;
                        default:
                            break;
                    }
                    compSeq.add(val);
                        SystemClock.sleep(300);
                }
            }
            isPlayerTurn = true;

            while (playSeq.size() &lt; level) {
                //Check for correct input
                if (playSeq.size() &gt; 0 &amp;&amp; (playSeq.get(playSeq.size()-1) != compSeq.get(playSeq.size()-1))) {
                    isRunning = false;
                    break;
                }
            }
            playSeq.clear();
            compSeq.clear();
            isPlayerTurn = false;
        }
    }

    private void setupButtons() {
        //set width and height of buttons
        android.view.ViewGroup.LayoutParams params;

        params = mOverlay.getLayoutParams();
        params.height   = (int) screenH;
        params.width    = (int) screenW;
        mOverlay.setLayoutParams(params);

        params = mTopLeft.getLayoutParams();
        params.height   = (int)(screenH/2);
        params.width    = (int)(screenW/2);
        mTopLeft.setLayoutParams(params);

        params = mTopRight.getLayoutParams();
        params.height   = (int)(screenH/2);
        params.width    = (int)(screenW/2);
        mTopRight.setLayoutParams(params);

        params = mBotLeft.getLayoutParams();
        params.height   = (int)(screenH/2);
        params.width    = (int)(screenW/2);
        mBotLeft.setLayoutParams(params);

        params = mBotRight.getLayoutParams();
        params.height   = (int)(screenH/2);
        params.width    = (int)(screenW/2);
        mBotRight.setLayoutParams(params);

        //set position of buttons
        mOverlay.setX(0);
        mOverlay.setY(0);

        mTopLeft.setX(0);
        mTopLeft.setY(0);

        mTopRight.setX(screenW / 2);
        mTopRight.setY(0);

        mBotLeft.setX(0);
        mBotLeft.setY(screenH / 2);

        mBotRight.setX(screenW / 2);
        mBotRight.setY(screenH / 2);

        //set initial background tints
        mTopLeft.getBackground().setColorFilter(GREEN_UP, PorterDuff.Mode.MULTIPLY);
        mTopRight.getBackground().setColorFilter(RED_UP, PorterDuff.Mode.MULTIPLY);
        mBotLeft.getBackground().setColorFilter(YELLOW_UP, PorterDuff.Mode.MULTIPLY);
        mBotRight.getBackground().setColorFilter(BLUE_UP, PorterDuff.Mode.MULTIPLY);
    }

    private void setupButtonActions(final ImageButton button, final int tint_up, final int tint_down) {
        //create button listeners and tints
        button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (isPlayerTurn) {
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        button.getBackground().setColorFilter(tint_down, PorterDuff.Mode.MULTIPLY);
                    } else if (event.getAction() == MotionEvent.ACTION_UP) {
                        button.getBackground().setColorFilter(tint_up, PorterDuff.Mode.MULTIPLY);
                    }

                    //Enter correct sequence value
                    int val = 4;

                    if (button == mTopLeft) {
                        val = 0;
                    } else if (button == mTopRight) {
                        val = 1;
                    } else if (button == mBotLeft) {
                        val = 2;
                    } else if (button == mBotRight) {
                        val = 3;
                    }

                    playSeq.add(val);
                }

                return false;
            }
        });
    }
}
</code></pre>