Cata
Cata

Reputation: 83

Change two images on button click (Android)

So, I want to make a button which every time it gets pressed, it changes the image above the button. I have 2 images in total, so I don't use arrays. Let's say I have image1.png and image2.png. The default image is image1 and after I press the button it turns to image2. If I press it again it turns to image1.

package blablablabla;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {

ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    image = (ImageView) findViewById(R.id.myicon);
}


public void onClick(View view) {
    switch(view.getId()){
    case R.id.button1:{
        image.setImageResource(R.drawable.initial);
        return;
    }
  }
 }
}

I thought of making an if statment: if the image is image1, then change it to image2 and vice-versa.

The problem: after I change to image2, I can't switch back. I know I didn't write the code for that because I made it wrrong.

Upvotes: 3

Views: 10970

Answers (2)

Sarthak Mittal
Sarthak Mittal

Reputation: 6104

Ok i think this might do:

package blablablabla;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {

ImageView image;
boolean flag = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    image = (ImageView) findViewById(R.id.myicon);
}


public void onClick(View view) {
    switch(view.getId()){
    case R.id.button1:{
    if(flag)
    {       
        image.setImageResource(R.drawable.initial);
        flag=false;
    }
    else
    {
        image.setImageResource(R.drawable.secondary);
        flag=true;
    }
        return;
    }
  }
 }
}

Upvotes: 5

An SO User
An SO User

Reputation: 24998

Ok, in your onCreate() where you retrieve your ImageView, you first need to use setTag() so that you can identify what image is already in it. Once an image for ImageView is set, you cannot get the resource ID back for it. This is just a work around so, do this:

image = (ImageView) findViewById(R.id.image);
image.setTag(R.drawable.image1);  

Now, in your onClick() you can do as follows:

int tag = (int) image.getTag();
if( tag == R.drawable.image1 ){
    image.setImageDrawable(getResources().getDrawable(R.drawable.image2));
    image.setTag(R.drawable.image2);
}else{
    image.setImageDrawable(getResources().getDrawable(R.drawable.image1));
    image.setTag(R.drawable.image1);
}  

Based On: https://stackoverflow.com/a/14474954/1894684

You can also use setImageResource() in place of setImageDrawable(). The former does the image decoding on the UI thread, though

Upvotes: 1

Related Questions