In this code snippet, I want the user to experience the following in the respective order: 1. See the letter he/she entered. 2. Hear the 'wrongAns' sound. 3. After the sound has finished, replace his text by the default "0".
Here is what I tried:
tv_1.setText(input1);
tv_1.setTextColor(Color.BLACK);
wrongAns.start();
try{
Thread.sleep(2300);
}catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
// fullSentence.start();
tv_1.setText("0");
tv_1.setTextColor(Color.RED);
I tried including another sound clip 'fullSentence' to see in exactly what order everything is executing.
What actually happens: 1st: I see "0" in red, and at the same time, I hear the 'wrongAns' sound. 2nd: I hear 'fullSentence' 2.3s after 'wrongAns' began.
How do I get this to work properly?
EDIT: (This is the Class)
public class TestCorrectSentence extends Activity implements OnClickListener {
MediaPlayer fullSentence, correctAns, wrongAns;
TextView tv_1, tv_2, tv_3, tv_4, tv_5, tv_6, tv_7, tv_8, tv_9, tv_10;
String A_1, A_2, A_3, A_4, A_5, A_6, A_7, A_8, A_9, A_10;
int counter;
EditText user_input;
Button b_Chk;
String input1, input2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sentence_activity);
SoundsMethod();
TextViewsMethod();
InitializeMethod();
ETandButtonMethod();
}
private void SoundsMethod() {
fullSentence = MediaPlayer.create(TestCorrectSentence.this, R.raw.voice_sent);
wrongAns = MediaPlayer.create(TestCorrectSentence.this, R.raw.voice_wrong);
}
private void TextViewsMethod() {
tv_1 = (TextView) findViewById(R.id.tvAns1);
}
private void InitializeMethod() {
A_1 = tv_1.getText().toString();
counter = 1;
}
private void ETandButtonMethod() {
user_input = (EditText) findViewById(R.id.etInput);
b_Chk = (Button) findViewById(R.id.bCheck);
b_Chk.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.bCheck:
try{
CheckAnsMethod();
}catch(Exception e){
e.printStackTrace();
}
break;
}
}
private void CheckAnsMethod() {
// TODO Auto-generated method stub
if (counter == 1){
if ("0".equals(A_1)){
input1 = user_input.getText().toString();
if ("T".equals(input1)){
tv_1.setText(input1);
tv_1.setTextColor(Color.GREEN);
correctAns.start();
counter++;
}else{
wrongAns.start();
tv_1.setText(input1);
tv_1.setTextColor(Color.BLACK);
try{
Thread.sleep(2300);
}catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
tv_1.setText("0");
tv_1.setTextColor(Color.RED);
fullSentence.start();
}
}else{
// wrongAns.start();
// fullSentence.start();
}
}else if (counter == 2){
if ("0".equals(A_2)){
input2 = user_input.getText().toString();
if ("h".equals(input2)){
// tv_2.setText(input2);
// tv_2.setTextColor(Color.GREEN);
// tv_2.setTypeface(null, Typeface.BOLD);;
// correctAns.start();
// correctAns.start();
// counter++;
}else{
// tv_2.setText(input2);
// tv_2.setTextColor(Color.BLACK);
// tv_2.setTypeface(null, Typeface.BOLD);;
// wrongAns.start();
// wrongAns.start();
// tv_2.setText("0");
// tv_2.setTypeface(null, Typeface.BOLD);;
}
}
}
}
}