android change image onclick imageviev - android

How can I change Image of an Imageview?
I want to get the image associated; if it is img1 I want set the image to img2, if it is img2 I want to set the image to img2.

first set the tag of imageview in xml to 1
final ImageView imageview = (ImageView) findViewById(R.id.imageView1);
imageview.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (Integer.parseInt(imageview.getTag().toString()) == 1) {
imageview.setBackgroundResource(R.drawable.image2);
imageview.setTag(2);
} else {
imageview.setBackgroundResource(R.drawable.image1);
imageview.setTag(1);
}
}
});

I think you are looking for ImageView.setTag() and ImageView.getTag().It checks if the image is associated with the Image view as #ρяσѕρєя K mentioned in comment.
There are two version of setTag one which takes object as an argument and other takes key and object as an argument

Related

How to interchange images in imageview in android studio?

If I want to change a image to another by the click of a button and then back to the previous image again by the click of the same button on imageview in android studio how to do that in short and easiest way? As I am new to it I am not familiar with all the functions of imageview.
For example:-
I wrote this code to do what I needed after a lot of failure in finding a easier way.
int i=0;
public void change(View v){
int img[] = {R.drawable.cat1,R.drawable.cat2};
ImageView cat = findViewById(R.id.imageView2);
if(i==0)
{cat.setImageResource(img[1]);
i=1;}
else {cat.setImageResource(img[0]);
i=0;}
}
Before I was trying to do something like this:-
public void change(View v){
ImageView cat = findViewById(R.id.imageView2);
if(cat.getDrawable()==R.drawable.cat2;)
{cat.setImageResource(R.drawable.cat1);}
else
{cat.setImageResource(R.drawable.cat1};
}
But it kept giving error that they have different type and I also tried some other functions named getId() but it didnt work either...
So my main objective is, is there a function through which I can campare the resource of image view directly with the image in drawable folder and how to implement it in if else or some other conditional statement?
The first approach should work, but the i value seems not tightly coupled to the ImageView. So, instead you can set a tag to the ImageView that equals to the current drawable id:
Initial tag:
ImageView cat = findViewById(R.id.imageView2);
cat.setImageResource(R.drawable.cat1);
cat.setTag(R.drawable.cat1);
And click listener callback:
public void change(View v){
ImageView cat = findViewById(R.id.imageView2);
int tag = (int) cat.getTag();
if(tag == R.drawable.cat2){
cat.setImageResource(R.drawable.cat1);
cat.setTag(R.drawable.cat1);
} else {
cat.setImageResource(R.drawable.cat2);
cat.setTag(R.drawable.cat2);
}
}
You could try StateListDrawable, LevelListDrawable, with each state/level, it will change image depend on your state/level

switching my imageView between two sources

When I click a button it changes my ImageView from pic1 to pic2, I use this...
ImageView myImageView = (ImageView) findViewById(R.id.myImageView );
myImageView.setImageResource(R.drawable.pic2);
and it changes my ImageView to pic2 now I want to be able to click the button again and change it back to pic 1 using...
myImageView.setImageResource(R.drawable.pic1);
but I need some way to make a getImageResource so I can run an if statement on which pic is showing and display the other when the button is clicked. For Example if pic 2 is showing it will check which pic is showing and returns pic2 so it knows when the button is clicked to switch it to pic1
Set a flag to identify which image is set.
By default pic1 is selected, add the flag
boolean flag = true;
Then on imageview click listener check for the flag, if the flag is set then set the imageview with pic2 else pic2.
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(flag) {
imageView.setImageResource(R.drawable.pic2);
} else {
imageView.setImageResource(R.drawable.pic1);
}
flag = !flag;
}
});
Try Below code and see if it works for you:
Write this before onCreate: private boolean imageIs = false;
Write this in onCreate:
btnImageChange.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!imageIs) {
imageV.setImageResource(R.mipmap.ic_launcher);
imageIs = true;
} else {
imageV.setImageResource(R.mipmap.ic_launcher_round);
imageIs = false;
}
}
});
You can add a variable, which will have a value when you click the button like
int x= R.drawable.pic2;
And when you click the button for changing the image to pic2 your code will be like
ImageView myImageView = (ImageView) findViewById(R.id.myImageView );
myImageView.setImageResource(R.drawable.pic2);
x=R.drawable.pic2;
And if you want to add pic1 again by clicking the button you can use if loop like
ImageView myImageView = (ImageView) findViewById(R.id.myImageView );
if(x=R.drawable.pic2){
myImageView.setImageResource(R.drawable.pic1);
}
else if(x=R.drawable.pic1){
myImageView.setImageResource(R.drawable.pic2);
x=R.drawable.pic2;
}
else{
myImageView.setImageResource(R.drawable.pic1);
}

Changing Imageview on button dynamically

In my application, I have button and ImageView.
Here when i press button i want to change ImageView. I have 5 images in my drawable folder. On press button ImageView changes images one by one based on button click. I want it's solution.
Grateful to anyone that can help.
Maintain an array of image ids and inside onClick, set images using id from the array, then increment index.
Eg:-
ArrayList<Integer> ids=new ArrayList<Integer>();
ids.add(R.drawable.image1);
ids.add(R.drawable.image2);
ids.add(R.drawable.image3);
ids.add(R.drawable.image4);
ids.add(R.drawable.image5);
Int index=0
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(index<ids.size()){
imageview.setImageResource(ids.get(index));
index++;
}
else index=0;
}
});
As #Nizam said just maintain an array of id and load dinamically the image in the onClick(). Instead of the Random use a field variable and increment it. Be careful to the array length!
final int[] ids = new int[] { R.drawable.img1, R.drawable.img2 };
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int randomId = new Random().nextInt(ids.length);
ImageView imageView = (ImageView) findViewById(R.id.imageview);
imageView.setImageDrawable(getResources().getDrawable(randomId));
}
});

Compare ImageView Objects

I need to compare two ImageView objects either by their image resource (the cover image being used on the button) or the filepath of the image resource.
Something along the lines of:
final ImageView button01 = (ImageView) findViewById(R.id.button01);
final ImageView button02 = (ImageView) findViewById(R.id.button02);
button01.setImageResource(R.drawable.myDogsPhoto);
button02.setImageResource(R.drawable.myDogsPhoto);
if (button01.getImageResource() == button02.getImageResource()) {
return true;
}
Can someone please tell me how I can go about comparing two ImageView components?
Thanks
compare two ImageView Objects ::
button01 =(ImageView)findViewById(R.id.button01 );
button02 =(ImageView)findViewById(R.id.button02 );
Drawable d=button01 .getDrawable();
Drawable d1=button02 .getDrawable();
if( d== d1){
Toast.makeText(Example.this, "hello", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(Example.this, "hai", Toast.LENGTH_SHORT).show();
}
One potential way is to use the View.setTag(); method to store the resource value (or filepath string). The setTag() and getTag() methods allow you to attach arbitrary data to the View object that you can recall later for whatever purpose you need.
For example:
final ImageView button01 = (ImageView) findViewById(R.id.button01);
final ImageView button02 = (ImageView) findViewById(R.id.button02);
button01.setImageResource(R.drawable.myDogsPhoto);
button01.setTag(R.drawable.myDogsPhoto);
button02.setImageResource(R.drawable.myDogsPhoto);
button02.setTag(R.drawable.myDogsPhoto);
if (button01.getTag().equals(button02.getTag())) {
return true;
}
Note I didn't compile this, it might want you to make Integer objects to pass to setTag().
Also I don't know if this is the best way to go about what you are wanting to do but it is the first that came to mind.

Replace the previous selected image with original image if one clicks on a new image

I am using grid view with ImageAdapter to display images.
I have two set of images that is mThumbIds containing original images and cThumbIds containing the selected images.
Right now when i click on an image i change the normal image with the selected image. The code is as below:
final ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
iv.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//iv.setColorFilter(Color.LTGRAY);
iv.setImageResource(cThumbIds[position]);
//iv.bringToFront();
index= position;
}
});
iv.setImageResource(mThumbIds[position]);
But the problem arises when i click on the another image the other image also shows as selected. The correct way would be to show the new image as selected and remove the older one as selected .In other words the older one should revert back to original one.
Please help me on this
Thanks,
Pankaj
You need to create a variable and keep the clicked image's id in that. When the user clicks some other image, first reset the other image as per the id in the variable and then replace the variable value with the id of the currently clicked image.
I'm assuming you are using a modified copy of the ImageAdapter class in this tutorial and that the code you have posted is in the getView(int,View,ViewGroup) method of that class.
You save the index of the image that is selected but you don't save the view itself. You need to save both in order to revert the old image, something like this:
private int selectedPosition = -1;
private ImageView selectedView = null;
...
public View getView(int position, View convertView, ViewGroup parent) {
// I don't understand what this line is about??
ImageView iv = (ImageView) v.findViewById(R.id.icon_image);
// Why not something like this??
// ImageView iv = new ImageView(mContext);
iv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Set the selected image for the ImageView just pressed.
iv.setImageResource(cThumbIds[position]);
// Revert to the original image for the ImageView previously
// pressed.
if (selectedPosition != -1) {
selectedView.setImageResource(mThumbIds[selectedPosition]);
}
// Save the position and ImageView just pressed so it can be
// reverted next time an ImageView is pressed
selectedPosition = position;
selectedView = (ImageView) view;
}
});
iv.setImageResource(mThumbIds[position]);
return (iv);
}
I'm a bit confused about the line ImageView iv = (ImageView) v.findViewById(R.id.icon_image); though (as I mention in my code example).

Categories

Resources