tisaconundrum tisaconundrum - 1 month ago 6
Android Question

(Android Studio) How can I make all images clickable without repeating lines of code?

enter image description hereenter image description here

As you can see from the two images, I am trying to add a bunch of tappable tiles that a user can play with. In order to make these images tappable I decided to utilize a switch statement from this calculator app.
Is there a way to turn the switch statement, that is involved with these tiles, into a loop or function, instead of this god-awful long piece of code?
Perhaps something like this?

onTapped(arg0.getId, [R.id.tile1, R.id.tile2, R.id.tile3, R.id.tile4, R.id.tile5, R.id.tile6, R.id.tile7], ['l','e','a','s','t')

onTapped(id, listOfTiles, listOfLetters){
int i = 0;
for each tile:
if (!tile1_flag) {
str = str.append(listOfLetters[i++]);
disp.setText(str);
tile1.setColorFilter(Color.argb(150, 200, 200, 200));
tile1_flag = true;
break;


Here's the original function.

boolean tile1_flag = false;
boolean tile2_flag = false;
boolean tile3_flag = false;
boolean tile4_flag = false;
boolean tile5_flag = false;
boolean tile6_flag = false;
boolean tile7_flag = false;
.
.
.
public void onClick(View arg0) {
TextView disp = (TextView) findViewById(R.id.tileOutput);
Editable str = (Editable) disp.getText();
switch (arg0.getId()) {
case R.id.tile1:
if (!tile1_flag) {
str = str.append('l');
disp.setText(str);
tile1.setColorFilter(Color.argb(150, 200, 200, 200));
tile1_flag = true;
break;
} else {
break;
}
case R.id.tile2:
if (!tile2_flag) {
str = str.append('e');
disp.setText(str);
tile2.setColorFilter(Color.argb(150, 200, 200, 200));
tile2_flag = true;
break;
} else {
break;
}
case R.id.tile3:
if (!tile3_flag) {
str = str.append('a');
disp.setText(str);
tile3.setColorFilter(Color.argb(150, 200, 200, 200));
tile3_flag = true;
break;
} else {
break;
}
case R.id.tile5:
if (!tile5_flag) {
str = str.append('s');
disp.setText(str);
tile4.setColorFilter(Color.argb(150, 200, 200, 200));
tile4_flag = true;
break;
} else {
break;
}
case R.id.tile6:
if (!tile6_flag) {
str = str.append('t');
disp.setText(str);
tile4.setColorFilter(Color.argb(150, 200, 200, 200));
tile4_flag = true;
break;
} else {
break;
}

Answer

You can create inner class with all image properties, like this

class ImageProperties {
    public boolean titleFlag;
    public String appendLetter;
    ...
    ImageProperties(boolean titleFlag, String appendLetter, ...) {
        this.titleFlag = titleFlag;
        ...
    }
}

and than fill them add as tag to your corresponding image e.g.

ImageView title1 = (ImageView) findViewById(R.id.title1);
title1.setTag(new ImageProperties(false, "l"))

and finally get them in

public void onClick(View arg0) {
    ImageView imageView = (ImageView) arg0;
    ImageProperties imageProperties = (ImageProperties) arg0.getTag();
    if (!imageProperties.titleFlag) {
        str = str.append(imageProperties.appendLetter);
        disp.setText(str);
        imageView.setColorFilter(Color.argb(150, 200, 200, 200));
        imageProperties.titleFlag = true;
    }
}
Comments