0

I'm new at androidstudio and want to compare a imageView by the following:

I have 2 imageView, both are using a drawable i named "blank" at the start of the app, using if/else I want to chance those images to another drawable i have, i tried the following:

private ImageView equipament1;
private ImageView equipament2;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_analise)                        
    equipament1 = findViewById(R.id.equipamento1);
    equipament2 = findViewById(R.id.equipamento2);


public void sentImg() {
    if (equipament1.equals(R.drawable.blank)){
  equipament1.setImageResource(R.drawable.reactor);
}
else if (equipament2.equals(R.drawable.blank)){
  equipament2.setImageResource(R.drawable.reactor);
} else  {finish();}

but it doesn't work, the app just replaces the first image and if i click on the button again, nothing happens (this if/else is inside a button).

I want to check if the first image is blank, if it is, the app should replace the blank image with the image "reactor" or, if is not blank, the app should move to the second blank image, and replace it and this go on for more 2 blank spaces.

I'm doing this because I'm building an program similar to LucidChart where you put your equipments in the app.

  • Possible duplicate of [How to check which current image resource is attached to ImageView in android xml?](https://stackoverflow.com/questions/23357706/how-to-check-which-current-image-resource-is-attached-to-imageview-in-android-xm) – tomas Mar 03 '19 at 08:59
  • I'm not really sure about your if and else conditions, can you explain a bit more on what you're trying to do? Note that equipament1 and equipament2 are View subclass objects, and R.drawable.blank is an int. While Java confusingly allows these two be compared with equals, it will always fail, because they aren't the same class. – Pamela Hill Mar 03 '19 at 09:01
  • @PamelaHill I want to check if the first image is blank, if it is, the app should replace the blank image with the image "reactor" or, if is not blank, the app should move to the second blank image, and replace it and this go on for more 2 blank spaces. I'm doing this because I'm building an program similar to LucidChart where you put your equipments in the app. – Rafael N. De Pietro Mar 03 '19 at 20:19

2 Answers2

1

The problem is that the second time you have already changed the value of the comparator.

If the objective is just to change the images you don't need the if/else.

private ImageView equipament1;
private ImageView equipament2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_analise)                        
    equipament1 = findViewById(R.id.equipamento1);
    equipament2 = findViewById(R.id.equipamento2);


public void sentImg() {
    equipament1.setImageResource(R.drawable.reactor);
    equipament2.setImageResource(R.drawable.reactor);
}
Marc LaQuay
  • 199
  • 1
  • 6
0

When the user clicks your button, you want to do 2 things. You want to show some images, or you want to call finish().

I would suggest using a boolean as a flag the the state and compare that instead of comparing the ImageView itself. This'll be easier, and make your code easier to read.

I created a flag called firstClick that is set to true by default. When the user clicks your button (button1 in this example), we check against that and show the images. Then we set it to false, so the next click will call finish().

private ImageView equipament1;
private ImageView equipament2;

// The current state of the Activity
private boolean firstClick = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_analise)                        
    equipament1 = findViewById(R.id.equipamento1);
    equipament2 = findViewById(R.id.equipamento2);


    // Setting your OnClickListener
    Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            if( firstClick ) {
                firstClick = false;
                sentImg();
            } else {
                finish();
            }
        }
    });
}

public void sentImg() {
    equipament1.setImageResource(R.drawable.reactor);
    equipament2.setImageResource(R.drawable.reactor);
}
advice
  • 5,778
  • 10
  • 33
  • 60