title: Random Color change every 4 seconds from a set of colors - Android App
body: <p>I'm trying to make an app that picks a random color from a set of 4 colors every 4 seconds in Java using Eclipse. My only real experience with programming is using actionscript in flash and I'm having a bit of trouble getting the results I want.
  I followed a tutorial with some basics and know enough about coding to have changed it enough to get close to what I want but at the moment it I can only get it to cycle with the way I've got it so far.</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>package com.example.androidcountdowntimer;
  
  import com.example.androidcountdowntimer.SpotView.SpotCallBack;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.TextView;
  
  public class MainActivity extends Activity 
    implements SpotCallBack{
  
   TextView myState, myCounter;
   Button btnStart;
  
   SpotView mySpotView;
  
   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  
    mySpotView = (SpotView)findViewById(R.id.myspotview);
    mySpotView.setCallback(this);
  
    myState = (TextView) findViewById(R.id.mystate);
  
    myCounter = (TextView) findViewById(R.id.mycounter);
    btnStart = (Button) findViewById(R.id.start);
    btnStart.setOnClickListener(new OnClickListener() {
  
     @Override
     public void onClick(View v) {
      mySpotView.startRun();
     }
    });
  
   }
  
   @Override
   public void cb_onTick(String msg) {
    myCounter.setText("Milliseconds: " + msg);
   }
  
   @Override
   public void cb_onFinish(String msg) {
    myState.setText(msg);
   }
  
  }
  </code></pre>
  
  <p>SpotView.java</p>
  
  <pre><code>package com.example.androidcountdowntimer;
  
  import android.content.Context;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.graphics.Paint;
  import android.os.CountDownTimer;
  import android.util.AttributeSet;
  import android.view.View;
  
  public class SpotView extends View{
  
   //used to pass back something to MainActivity
   interface SpotCallBack {
    void cb_onTick(String msg);
    void cb_onFinish(String msg); 
   }
  
   SpotCallBack spotCallback;
  
   CountDownTimer countDownTimer;
  
   int state;
   final static int STATE_IDLE = 0;
   final static int STATE_RED = 1;
   final static int STATE_GREEN = 2;
   final static int STATE_BLUE = 3;
   final static int STATE_YELLOW = 4;
  
   Paint paintRed, paintGreen, paintBlue, paintYellow;
  
   public SpotView(Context context) {
    super(context);
    init();
   }
  
   public SpotView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
   }
  
   public SpotView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
   }
  
   private void init(){
    state = STATE_IDLE;
  
    paintRed = new Paint();
    paintRed.setColor(Color.RED);
    paintRed.setStrokeWidth(1);
    paintRed.setStyle(Paint.Style.FILL);
  
    paintGreen = new Paint();
    paintGreen.setColor(Color.GREEN);
    paintGreen.setStrokeWidth(1);
    paintGreen.setStyle(Paint.Style.FILL);
  
    paintBlue = new Paint();
    paintBlue.setColor(Color.BLUE);
    paintBlue.setStrokeWidth(1);
    paintBlue.setStyle(Paint.Style.FILL);
  
    paintYellow = new Paint();
    paintYellow.setColor(Color.YELLOW);
    paintYellow.setStrokeWidth(1);
    paintYellow.setStyle(Paint.Style.FILL);
   }
  
   public void setCallback(SpotCallBack cb){
    spotCallback = cb;
   }
  
   public void startRun(){
    state = STATE_RED;
  
    if(countDownTimer!=null){
     countDownTimer.cancel();
    }
  
    countDownTimer = new CountDownTimer(4000, 500) {
  
     @Override
     public void onTick(long millisUntilFinished) {
      spotCallback.cb_onTick(String.valueOf(millisUntilFinished));
     }
  
     @Override
     public void onFinish() {
      if(state == STATE_RED){
       state = STATE_GREEN;
       spotCallback.cb_onFinish("GREEN");
      }else if(state == STATE_GREEN){
       state = STATE_BLUE;
       spotCallback.cb_onFinish("BLUE");
      }else if(state == STATE_BLUE){
       state = STATE_YELLOW;
       spotCallback.cb_onFinish("YELLOW");
      }else if(state == STATE_YELLOW){
          state = STATE_RED;
          spotCallback.cb_onFinish("RED");
      }
      countDownTimer.start();
      invalidate();
     }
    };
  
    countDownTimer.start();
    spotCallback.cb_onFinish("RED");
    invalidate();
   }
  
   @Override
   protected void onDraw(Canvas canvas) {
  
    float w = getWidth();
    float h = getHeight();
  
    if(state == STATE_RED){
     canvas.drawCircle(w/2, h/2, 400, paintRed);
    }else if(state == STATE_GREEN){
     canvas.drawCircle(w/2, h/2, 400, paintGreen);
    }else if(state == STATE_BLUE){
     canvas.drawCircle(w/2, h/2, 400, paintBlue);
    }else if(state == STATE_YELLOW){
        canvas.drawCircle(w/2, h/2, 400, paintYellow);
    }
   }
  
  }
  </code></pre>
  
  <p>activity_main.xml</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_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:orientation="vertical"
      tools:context="com.example.androidcountdowntimer.MainActivity" &gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center_horizontal"
          android:autoLink="web"
          android:text="Tynes Timer Thing"
          android:textStyle="bold"
          android:textSize="24sp" /&gt;
  
      &lt;Button
          android:id="@+id/start"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="Start CountDownTimer" /&gt;
  
      &lt;TextView
          android:id="@+id/mystate"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center_horizontal"
          android:textSize="24sp" /&gt;
  
      &lt;TextView
          android:id="@+id/mycounter"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center_horizontal"
          android:textSize="24sp" /&gt;
  
      &lt;com.example.androidcountdowntimer.SpotView 
          android:id="@+id/myspotview"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>So they way I've tried to make the color change randomly is by storing the color used for the state as a variable and then trying to use that variable to make the part of the code that is currently just running through a set of states dynamic like:</p>
  
  <pre><code>String[] colours = {"STATE_RED", "STATE_GREEN", "STATE_BLUE", "STATE_YELLOW"};
  int idx = new Random().nextInt(colours.length);
  String random = (colors[idx]);
  </code></pre>
  
  <p>and using this to try to set the state in the elseif</p>
  
  <pre><code>state = random;
  </code></pre>
  
  <p>But I just keep getting red Xs and errors in my code and cant figure out how to make the state change random in java.</p>
  
  <p>Any help and/or advice would be greatly appreciated.</p>
  
  <p>Thanks,
  Liam</p>