0

I am a beginner to android, with some java under my belt. I have completed the make my first app tutorial only.

Next I want to make an app to use simple 2D graphics

To start, I want to find a way of flashing the screen between two colors (red and green) in an infinite loop

wait int seconds;
make screen red;
wait int seconds;
make screen green;

loop forever;

Could anyone please point me to a tutorial(s) or source code(s) that may help?

Many Thanks

Craig Turner
  • 21
  • 1
  • 10

5 Answers5

3

How about you make a layout which fills the screen, lets call it llayout.

put this in the code:

static final int[] COLORS = {0xff0000, 0x00ff00};
static final int WAITTIME = 1000;
int currentColor = 0;

public void onCreate(Bundle b) {
    setContentView(R.layout.mylayout);
    final LinearLayout llayout = (LinearLayout) findViewById(R.id.llayout);
    new Thread() {
        public void run() {
            while(true) {
                Thread.sleep(WAITTIME);
                currentColor++;
                if (currentColor > COLORS.length)
                    currentColor = 0;
                YourActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        llayout.setBackgroundColor(COLORS[currentColor]);
                    }
                });
            }
        }
    }.start();
}

This should work. Using this code, you can add additional colors to the array COLORS.

stealthjong
  • 10,858
  • 13
  • 45
  • 84
3

Thanks for the input, This code seems to work OK

package biz.consett.mydraw;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

/* Flash the screen between red and green each second */
public class MainActivity extends Activity {
    final static int INTERVAL = 1000; // 1000=1sec   
    private static View myView = null;
    boolean whichColor = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myView = (View) findViewById(R.id.my_view);
        myView.setBackgroundColor(Color.RED);// set initial colour
        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(INTERVAL);
                    } 
                    catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    updateColor();
                    whichColor = !whichColor;
                }
            }
        }).start();
    }

    private void updateColor() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (whichColor) 
                    myView.setBackgroundColor(Color.RED);
                else 
                    myView.setBackgroundColor(Color.GREEN);
            }
        });
    }
}

Layout, activity_main.xml:

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

<View

android:id="@+id/my_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

</View>

</RelativeLayout>
Community
  • 1
  • 1
Craig Turner
  • 21
  • 1
  • 10
0

Taken from this post and edited for your case:

private int m_interval = 1000; // 1 second by default, can be changed later
private Handler m_handler;
private Layout mYourMainLayout //dont know what type of layout you use
private boolean which;

@Override
protected void onCreate(Bundle bundle)
{
  ...
  mYourMainLayout = (Layout) this.findViewById(R.id.yourMainLayout);
  m_handler = new Handler();
}

Runnable m_colorChanger = new Runnable()
{
     @Override 
     public void run() {
if(which){
         mYourMainLayout.setBackgroundColor(Color.GREEN)
         }
else {
   mYourMainLayout.setBackgroundColor(Color.RED)
    }
which=!which
          m_handler.postDelayed( m_colorChanger, m_interval);
     }
};
Community
  • 1
  • 1
slezadav
  • 6,104
  • 7
  • 40
  • 61
  • `protected void onCreate(Bundle bundle) { tag_br space space mYourMainLayout = (RelativeLayout) this.findViewById(R.id.flashscreen); m_handler = new Handler(); } Runnable m_colorChanger = new Runnable() { @Override public void run() { if(which){ mYourMainLayout.setBackgroundColor(Color.GREEN); } else { mYourMainLayout.setBackgroundColor(Color.RED); } which=!which; m_handler.postDelayed( m_colorChanger, m_interval); } };` **compiles but App just stops at runtime**. – Craig Turner Mar 08 '13 at 17:01
  • oops silly mistake forgot `super.onCreate(bundle);` ***but now just shows grey screen - no flashing red/green*** – Craig Turner Mar 08 '13 at 17:59
  • ***Layout XML***` ` – Craig Turner Mar 08 '13 at 18:08
0

You already got a lot of solutions but just adding my own...

You will need a timer

@Override
protected void onCreate(Bundle bundle)
{
   ...
   Timer timer = new Timer();
   TimerTask updateColor = new SwitchColorTask();
   timer.scheduleAtFixedRate(updateColor, 100, 100);
   ...
}

A timer task.

private boolean colorSwitched;
class SwitchColorTask extends TimerTask {
   public void run() {
       LinearLayout main = (LinearLayout)findViewById(R.id.main);
       if(colorSwitched){
          main.setBackgroundColor (Color.GREEN)
       }
       else{
          main.setBackgroundColor (Color.RED)
       }           
       colorSwitched=!colorSwitched
   }
}
Edward van Raak
  • 4,841
  • 3
  • 24
  • 38
0

This code implements the array of colors and works fine - many thanks.
(uses same manifest and layout) package biz.consett.mydraw;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

/* 
 * Simple use of Android Thread
 * Flash the screen between an array of colours at an interval 
 * 
 */

public class MainActivity extends Activity


static final int[] COLORS =
{AColor.RED,  AColor.BLUE, AColor.GREEN};// colour array
// Colour Red Green Blue 
// Index [0] [1] [2]

private int currentColor = 0;

private View MYVIEW = null;
//boolean whichColor = true;

final int interval = 100; // 0.6 second static final?

@Override
public void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MYVIEW = (View) findViewById(R.id.my_view);

    MYVIEW.setBackgroundColor(Color.RED);// set initial colour

    new Thread(new Runnable()
    {

        // @Override
        public void run()
        {

            while (true)
            {
                try
                {
                    Thread.sleep(interval); // sleep for interval

                } 

                catch (InterruptedException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                    updateColor();

            } // end of  while true loop


        }// end of run

    }).start(); // end of runnable

} // end of onCreate bundle

private void updateColor()
{

    runOnUiThread(new Runnable()
    {

        @Override
        public void run()
        {

            if (currentColor > COLORS.length - 1)
            { 
                currentColor = 0;

            }
            MYVIEW.setBackgroundColor(COLORS[currentColor]);

            currentColor++;

        }// end of run
    });

}
}// -------------END OF MainActivity extends Activity-----------------------

package biz.consett.mydraw;

public class AColor
{

final static int RED = 0xFFFF0000; // hex alpha_R_G_B 
final static int GREEN = 0xFF00FF00;
final static int BLUE = 0xFF0000FF; 
final static int PURPLE = 0xFFAA00AA;
}  
Craig Turner
  • 21
  • 1
  • 10