0

I'm trying to test something out, but all I need is an activity that blinks between black and white screens at an interval of 1 second. I've looked at postDelayed, but I'm still not sure how I would implement that. This is what I have right now, but it doesn't "blink"

MainActivity:

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;

public class Blink extends Activity {
    int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blink);

        while (i == 0) {
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                public void run() {
                    changeColor();
                }
            }, 1000);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void changeColor() {
        if (findViewById(R.id.mylayout).getBackground().equals("#ffffff")) {
            findViewById(R.id.mylayout).setBackgroundResource(Color.BLACK);
        } else {
            findViewById(R.id.mylayout).setBackgroundResource(Color.WHITE);

        }
    }
}

Updated Code:

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;

public class Blink extends Activity {
    int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blink);

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                changeColor();
            }
        }, 1000);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void changeColor() {
        if (i == 0) {
            findViewById(R.id.mylayout).setBackgroundColor(Color.WHITE);
            i++;
        } else if (i == 1) {
            findViewById(R.id.mylayout).setBackgroundColor(Color.BLACK);
            i--;

        }
    }
}

Update 2:

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;

public class Blink extends Activity {
    int i = 0;
    Boolean bool = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blink);

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                changeColor();
            }
        }, 1000);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void changeColor() {
        if (bool) {
            findViewById(R.id.mylayout).setBackgroundColor(Color.WHITE);
            bool = false;
        } else {
            findViewById(R.id.mylayout).setBackgroundColor(Color.BLACK);
            bool = true;

        }
    }
}
EGHDK
  • 17,818
  • 45
  • 129
  • 204

1 Answers1

0

I narrowed down the exact question I should be asking right here: Why doesn't this postDelayed run forever?

To fix this code, all I had to do was add handler.postDelayed(this, 1000); inside of run.

Community
  • 1
  • 1
EGHDK
  • 17,818
  • 45
  • 129
  • 204