Reputation: 37
I have an array of ImageViews. These ImageViews can be selected once you click on them. Their stroke property changes from 0 (not selected) and 4 (selected). Now I have two buttons, the 'ClearAll' and 'Back'. If I click the back button, all of the selected ImageViews description would display in a Toast message (ex. "You've chosen iv1, iv2, iv3"). What the 'ClearAll' button does is it clears the selection - let's say I've click ImageView 1, 2, and 3 - If I click the 'ClearAll', their stroke would go back to 0 (indicating that every selection was cleared). But I have a problem, let's say I've click ImageView 1, 2, and 3 then my mind changes and I want to click ImageView 4, 5, 6 instead then click 'Back'. The Toast message displays the description of those ImageView that I've cleared too, the output is something like this, "You've chosen iv1, iv2, iv3, iv4, iv5, iv6. How can I fix my code so that those that have been cleared by 'ClearAll' would not be displayed in the Toast?
This is the code snippet of each above.
ImageViews:
for (int i = 0 ; i < color.length ; i++) {
color[i] = (ImageView) findViewById (ivCirclesId[i]);
final GradientDrawable ivCircleOnClick = (GradientDrawable) color[i].getBackground ();
color[i].setTag ("0");
color[i].setOnClickListener (new OnClickListener() {
public void onClick (View v) {
if (v.getTag ().toString ().equals ("1")) {
--ctr;
v.setTag ("0");
ivCircleOnClick.setStroke (0, Color.WHITE);
} else {
if(ctr >= 8) {
Toast.makeText (getApplicationContext (), "Limit reached!", Toast.LENGTH_SHORT).show ();
return;
}
++ctr;
v.setTag ("1");
ivCircleOnClick.setStroke (4, Color.WHITE);
message = message + "#" + v.getContentDescription ();
return;
}
}
});
ClearAll:
Button btnClear = (Button) findViewById (R.id.btnClearAll);
btnClear.setOnClickListener (new OnClickListener () {
@Override
public void onClick (View v) {
for (int i = 0 ; i < color.length; i++) {
final GradientDrawable ivCircleOnClick = (GradientDrawable) color[i].getBackground ();
ivCircleOnClick.setStroke (0, Color.WHITE);
color[i].setTag ("0");
ctr = 0;
}
}
});
Back:
Button btnBack = (Button) findViewById (R.id.btnBack);
btnBack.setOnClickListener (new OnClickListener () {
@Override
public void onClick (View v) {
if (ctr >= 1) {
Toast.makeText (Filter.this, message, Toast.LENGTH_SHORT).show();
message = "";
}
finish();
}
});
Upvotes: 2
Views: 294
Reputation: 825
This should also work when you deselect only one imageview. Hope this helps.....
ImageViews :
for (int i = 0 ; i < color.length ; i++) {
color[i] = (ImageView) findViewById (ivCirclesId[i]);
final GradientDrawable ivCircleOnClick = (GradientDrawable) color[i].getBackground ();
color[i].setTag ("0");
color[i].setOnClickListener (new OnClickListener() {
public void onClick (View v) {
if (v.getTag ().toString ().equals ("1")) {
--ctr;
v.setTag ("0");
ivCircleOnClick.setStroke (0, Color.WHITE);
} else {
if(ctr >= 8) {
Toast.makeText (getApplicationContext (), "Limit reached!", Toast.LENGTH_SHORT).show ();
return;
}
++ctr;
v.setTag ("1");
ivCircleOnClick.setStroke (4, Color.WHITE);
//message = message + "#" + v.getContentDescription ();
return;
}
}
});
ClearAll:
Button btnClear = (Button) findViewById (R.id.btnClearAll);
btnClear.setOnClickListener (new OnClickListener () {
@Override
public void onClick (View v) {
for (int i = 0 ; i < color.length; i++) {
final GradientDrawable ivCircleOnClick = (GradientDrawable) color[i].getBackground ();
ivCircleOnClick.setStroke (0, Color.WHITE);
color[i].setTag ("0");
ctr = 0;
message = "";
}
}
});
Back:
Button btnBack = (Button) findViewById (R.id.btnBack);
btnBack.setOnClickListener (new OnClickListener () {
@Override
public void onClick (View v) {
message = "";
for(int i = 0 ; i < color.length ; i++){
if(color[i].getTag().toString().equals("1")){
message = message + "#" + color[i].getContentDescription();
}
}
if (ctr >= 1) {
Toast.makeText (Filter.this, message, Toast.LENGTH_SHORT).show();
message = "";
}
//finish();
}
});
Upvotes: 1