3

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.

Cata
  • 83
  • 1
  • 1
  • 9

2 Answers2

5

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;
    }
  }
 }
}
Sarthak Mittal
  • 5,794
  • 2
  • 24
  • 44
  • hello...suppose im performing this one activity.user click to glow image... what if user goes from one activity to another and again coming back to activity one will the button remain as glow? – Wini Jan 18 '21 at 13:12
  • @Wini I would suggest you to try these things out, and also look into saving instance state. – Sarthak Mittal Feb 08 '21 at 17:21
1

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

Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221
  • Hmm, I understand. But why you use set.ImageDrawable? I mean, you could use setImageResource too, right? Edit: Oh, now I see the bottom text! Thanks for your answer!! – Cata Nov 23 '14 at 08:45
  • hello...suppose im performing this one activity.user click to glow image... what if user goes from one activity to another and again coming back to activity one will the button remain as glow? – Wini Jan 18 '21 at 13:13