How can I change the image of an ImageView on Android - android

I am developing a card game on android, and I am looking to change the Image of an ImageView when a button is pressed to pick a card, and I have looked at several other ideas, but all I have seem either crash or it does not change.
cimg = new ImageView(this);
nextCard.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
cimg.setImageResource(R.drawable.c2);
doCard();
}
});
Here is what I have for code, and with this, the image does not change
XML:
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="#drawable/icon" android:id="#+id/imageView1"></ImageView>

You have to do
cimg = findViewById(R.id.imageView1); // in your onCreate
cimg.setImageResource(R.drawable.c2);
In your snippet you are just creating a new ImageView that isn't added to your Activity's view.

have you tried referencing the ImageView by Id rather than just a new blank one?
ImageView cimg = (ImageView)findViewById(R.id.imageView1);

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

Change between images with a Button

I'm new to Android and I'm trying to change the content of an ImageView with a button and if I press the button again the image changes back. I thought it would be easy with an if-else statement but I have been looking around in the ImageView API and I don't see the method that allows me to get the image that is being displayed in that moment... Any ideas?
Here is my code so far...
boton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
imagen.setImageResource(R.mipmap.imag1);
}
});
I didn't copy the rest of the code because I dont think it's necessary
You can get the image of the button with using this method:
Bitmap bitmap = ((BitmapDrawable)imagen.getDrawable()).getBitmap();
To set image of a button with another bitmap you can use:
imagen.setImageBitmap(bitmap);

android gif animation and button

I want to create an animate img, so when I click a button I can see the animation.
in my project, I've just create buttons and images. In the drawable folder, I put the three png images and a frame_animation.xml, I set the images and the duration. In the java file, I put this code:
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setBackgroundResource(R.drawable.frame_animation);
myFrameAnimation=(AnimationDrawable) img.getBackground();
}
});
but when I run the app, the animation doesn't work (I can see only one image, not all the three image)
You have to also START the animation, setting the resource is not enough.
In your case simply add myFrameAnimation.start();
Full example:
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setBackgroundResource(R.drawable.frame_animation);
myFrameAnimation=(AnimationDrawable) img.getBackground();
myFrameAnimation.start();
}
});
Seems like you just left it out by accident, since you already got the AnimationDrawable from your resource.

replace image on ImageButton

I'm new to andorid, and I'm trying to do a simple thing:
when button A is clicked, I want to replace the image displayed
on ImageButton B.
I've tried all sort of things like:
msortByButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Test change image
mdefineLocationButton.setBackgroundResource(0);
mdefineLocationButton.invalidate();
mdefineLocationButton.setBackgroundResource(R.drawable.notdefined);
mdefineLocationButton.invalidate();
mdefineLocationButton.refreshDrawableState();
}
});
But it seems that the new image is painted, but on top of it the old image
is painted also. (I can see the old image, below it I can see edges of the new image).
Any idea how to do this right?
Thanks,
Omer
Use setImageResource (int resId)
(http://developer.android.com/reference/android/widget/ImageView.html#setImageResource(int))
ImageButtons can have a background and an actual image src. My guess is initially you set the image src and now in code you are setting the background. So both show.
#Override
public void onClick(View v) {
mdefineLocationButton.setImageResource(R.drawable.notdefined);
}

Android Contact: how to achieve the same picture "expansion" in a popup

I guess that you know the ICS contact application.
There is a fancy and usefull way to display pictures in a cropped way and then on a click, the full picture appear.
I just wanted to know how to achieve this..
Is that a new Activity?
Should I create a popup with the full picture and create the animation in the same Activity?
Thank a lot for any help..
I have already tried:
final ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
AlertDialog.Builder builder = new AlertDialog.Builder(
ActivityDetail.this);
ImageView view = new ImageView(ActivityDetail.this);
//build the view
builder.setView(view);
builder.create().show();
}
});
The big problem is the animation!
The dialog is not as neat as the Contact apps.
you can use scaleType attribute of ImageView like this,
<ImageView android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:src="#drawable/eureka"
android:scaleType="matrix">
</ImageView>
android:scaleType has different values given on android:scaleType samples
then,
You can pass the imagesourceid as intent extra somewhat like this.
Intent newintent = new Intent(test.this, image.class);
newintent.putExtra("IMAGE", R.drawable.yor_image);
and then accept in the destination activity and use as below
int imgid = getIntent().getIntExtra("IMAGE", 0);
ImageView img = (ImageView)findViewById(R.id.img);
img.setImageResource(imgid);

Categories

Resources