Switch case between images
Basically I want to switch between drawable resources, but I don’t know how to do it. I wrote it all in one If statement, but it was too long and messy and it didn’t execute the way I wanted it to because it wouldn’t stop if they typed the wrong letter.
What I want to do is if the user enters a letter, the background will change to the
next letter, for example, if the user enters A, the background will change to B, and so on. It does work now, but the mistake is that if the user is on the letter A and the user enters the letter P incorrectly, the background switches to Q instead of telling the user that they were wrong.
I
do treat this code as an else if instead of an If statement, but I just thought I’d change it today. Can anyone explain how I put this If statement inside a case statement? Thank you for your help
if (words.getText().toString().equalsIgnoreCase("A")) {
gestureViewer.setBackgroundResource(R.drawable.lettersb);
} else {
onDestroy();
}
if (words.getText().toString().equalsIgnoreCase("B")) {
gestureViewer.setBackgroundResource(R.drawable.lettersc);
} else {
}
if (words.getText().toString().equalsIgnoreCase("C")) {
gestureViewer.setBackgroundResource(R.drawable.lettersd);
} else {
}
if (words.getText().toString().equalsIgnoreCase("D")) {
gestureViewer.setBackgroundResource(R.drawable.letterse);
} else {
}
if
(words.getText().toString().equalsIgnoreCase("E")) {
gestureViewer.setBackgroundResource(R.drawable.lettersf);
} else {
}
if (words.getText().toString().equalsIgnoreCase("F")) {
gestureViewer.setBackgroundResource(R.drawable.lettersg);
} else {
}
if (words.getText().toString().equalsIgnoreCase("G")) {
gestureViewer.setBackgroundResource(R.drawable.lettersh);
}else {
}
if (words.getText().toString().equalsIgnoreCase("H")) {
gestureViewer.setBackgroundResource(R.drawable.lettersi);
}else {
}
if (words.getText().toString().equalsIgnoreCase("I")) {
gestureViewer.setBackgroundResource(R.drawable.lettersj);
} else {
}
if (words.getText().toString().equalsIgnoreCase("J")) {
gestureViewer.setBackgroundResource(R.drawable.lettersk);
}else {
}
if (words.getText().toString().equalsIgnoreCase("K")) {
gestureViewer.setBackgroundResource(R.drawable.lettersl);
}else {
}
if (words.getText().toString().equalsIgnoreCase("L")) {
gestureViewer.setBackgroundResource(R.drawable.lettersm);
}else {
}
if (words.getText().toString().equalsIgnoreCase("M")) {
gestureViewer.setBackgroundResource(R.drawable.lettersn);
}else {
}
if (words.getText().toString().equalsIgnoreCase("N")) {
gestureViewer.setBackgroundResource(R.drawable.letterso);
}else {
}
if (words.getText().toString().equalsIgnoreCase("O")) {
gestureViewer.setBackgroundResource(R.drawable.lettersp);
}else {
}
if (words.getText().toString().equalsIgnoreCase("P")) {
gestureViewer.setBackgroundResource(R.drawable.lettersq);
}else {
}
if (words.getText().toString().equalsIgnoreCase("Q")) {
gestureViewer.setBackgroundResource(R.drawable.lettersr);
} else {
}
if (words.getText().toString().equalsIgnoreCase("R")) {
gestureViewer.setBackgroundResource(R.drawable.letterss);
} else {
}
if (words.getText().toString().equalsIgnoreCase("S")) {
gestureViewer.setBackgroundResource(R.drawable.letterst);
}else {
}
if (words.getText().toString().equalsIgnoreCase("T")) {
gestureViewer.setBackgroundResource(R.drawable.lettersu);
}else {
}
if (words.getText().toString().equalsIgnoreCase("U")) {
gestureViewer.setBackgroundResource(R.drawable.lettersv);
}else {
}
if (words.getText().toString().equalsIgnoreCase("V")) {
gestureViewer.setBackgroundResource(R.drawable.lettersw);
}else {
}
if (words.getText().toString().equalsIgnoreCase("W")) {
gestureViewer.setBackgroundResource(R.drawable.lettersx);
}else {
}
if (words.getText().toString().equalsIgnoreCase("X")) {
gestureViewer.setBackgroundResource(R.drawable.lettersy);
}else {
}
if (words.getText().toString().equalsIgnoreCase("Y")) {
gestureViewer.setBackgroundResource(R.drawable.lettersz);
}else {
}
if (words.getText().toString().equalsIgnoreCase("Z")) {
gestureViewer.setBackgroundResource(R.drawable.lettersa);
} else {
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
public void afterTextChanged(Editable s) {
}
});
}
Solution
Use follow
char ch=words.getText().toString().charAt(0));
switch(ch-'A')
{
case 0:
A;
break;
case 1:
B;
break;
.....
}
With this you can convert the if statement to switch case:
You can also have drawable arrays, so you don’t need to use switch case:
int[] arr=new int[]{R.id.drawable_A, .......};
Then the setting color is
char ch=words.getText().toString().charAt(0));
int index=ch-'A';
view.setBackgroundResource(index);