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:)
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:)
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:
When o answer the wrong answer:
For the right one:
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
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);
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 Handler
and 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);