0

I would like to have an edittext with a blinking background, e.g. for a quiz, someone write an answer in an edittext and after pushing a button the background should blinking red or green depending on the answer.

Do you have any idea? Thanks:)

Peter Panne
  • 475
  • 1
  • 5
  • 15
  • possible duplicate of [How to make the textview blinking](http://stackoverflow.com/questions/9294112/how-to-make-the-textview-blinking) – gipinani Apr 16 '14 at 14:46
  • You could do something similar to that but instead of changing the visibility of the textview, change the background color. – nasch Apr 16 '14 at 14:50
  • http://stackoverflow.com/questions/2614545/animate-change-of-view-background-color-in-android – Alex Salauyou Apr 16 '14 at 14:51
  • Have you tried Thread.Sleep(1000) and editText.setBackgroundColor(Color.RED/GREEN) in a while loop? This should be fairly simple... – Vinit Shandilya Apr 16 '14 at 15:00

3 Answers3

0

Is like that what you need?

public class MainActivity extends Activity {

    int correctAnswer = 12;

    EditText answerET;
    Button answerBtn;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        answerET = (EditText) findViewById(R.id.editText1);
        answerBtn = (Button) findViewById(R.id.button1);

        //Question for example is 2x6=?
        answerBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                int answer = Integer.parseInt((answerET.getText().toString()));

                if(answer == correctAnswer){
                    answerET.setBackgroundColor(Color.GREEN);
                    answerET.setAnimation(startBlicking());

                }else{
                    answerET.setBackgroundColor(Color.RED);
                    answerET.setAnimation(startBlicking());
                }
            }

            //blinking animation :)
            private Animation startBlicking(){
                Animation fadeIn = new AlphaAnimation(1, 0);
                fadeIn.setInterpolator(new DecelerateInterpolator()); //add this
                fadeIn.setDuration(1000);
                fadeIn.setRepeatCount(-1);

                return fadeIn;
            }
        });
    }
}

This how it looks like: enter image description here


When o answer the wrong answer: enter image description here


For the right one: enter image description here


it's just working as you need i guess, sorry i can't give a test for that it needs video not a picture :)

Good Luck, let me know if something goes wrong

Mouad EL Fakir
  • 3,609
  • 2
  • 23
  • 37
0

There may well be a cleaner solution but you can use something similar to.

private class BlinkTask extends AsyncTask<Integer, Boolean, Boolean> {
 protected Long doInBackground(Integer... timeout) {
     boolean active = true;
     int counter = 10;
     // If timeout 1s this will flash for 10s
     while(counter-- != 0)
     {
         try {
             Thread.Sleep(timeout[0])
             publishProgress(active);
             active =! active;
         } catch(...){}
     }
     return true;
 }

 protected void onProgressUpdate(Boolean... active) {
     if(active)
         // Change it to one colour here depending on correct answer
         mAnswerText.setBackgroundColor(mCorrectAnswer ? Color.GREEN : Color.RED)
     else
         // Change it to standard colour
 }

 protected void onPostExecute(Boolean result) {

 }
 }

Start it in your `onClick' method with.

new BlinkTask.execute(1);
Ne0
  • 2,688
  • 3
  • 35
  • 49
  • `while(counter--)` such statement is illegal in Java. You cannot use any types other then `boolean` as conditions, it's not JavaScript! – Alex Salauyou Apr 16 '14 at 15:14
0

Give it a try with a simple handler.

//How many times does it blink
private final static int NUM= 10;
//Milliseconds interval
private final static long INTERVAL= 100; 

Initialize your Handlerand Runnable.

Handler handler = new Handler();

Runnable r = new Runnable(){
    int aux = 0;
    public void run(){
        mEditTextView.setBackgroundColor(mCorrectAnswer ? Color.GREEN : Color.RED);
        if(aux < NUM)
            handler.postDelayed(this, INTERVAL);    
        else
            aux = 0; 
    }
};

By the time you want to trigger it just do:

handler.post(r);
diogobernardino
  • 1,142
  • 8
  • 11