replace image on ImageButton - android

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);
}

Related

Android transform ImageView to Button programmiticaly with two images and onClickListener

I want to create a button animation with two image on an ImageView programmiticaly only and by using onClickListener. I have two image that i can set with setImageResource() fonction but i don't know how to play the button animation before the button action launches.
Please, could you explain a bit more what you are wanting? Do you want to replace the ImageButton resource when you click on it? You can achieve it setting the OnClickListener
new View.OnClickListener() {
public void onClick(View v) {
imageButtonView.setImageResource(idOfTheNewResource);
}
};
Is this what you are wanting?

GIF Listener in android

i'm using this How to animate .gif images in an android? solution (first answer of Shobhit Puri) to implements in my app some Gif. now i want to add some Listener on them (click listner).
every single procedure i tried didn't worked.
the idea is:
the gif is made by 3 image, when you click the first image the gif will change in another one, and so on.
Thanks for your attention.
EDIT:
actualy i'm trying this way
findViewById(R.id.ivAnimation).setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Log.i(TAG, "onClickGif PrincipalActivity");
}
});
on onCreate() of the mainActivity. the Log is well generated but i can't go over this
I'm not sure about gifs, but if you have separate files, you could do something like this.
Warning: Untested :)
final int[] imgs = new int[] {R.drawable.img1, R.drawable.img2, R.drawable.img3};
int position = 0;
final ImageView gifView = (ImageView) findViewById(R.id.ivAnimation);
gifView.setImageResource(imgs[position]);
gifView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Log.i(TAG, "onClickGif PrincipalActivity");
position = (position + 1) % imgs.length; // wrap around to "restart" gif
gifView.setImageResource(imgs[position]);
}
});

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);

hide one image and show other

I have two images(above, below) place on each other. I want to hide the part of the above image where user touchs. At the end above image will completely remove and below image completely become visible.
How will I achieve this thing in Android. I am really clueless. Please help
example:
button = (Button) findViewById(R.id.button);
leftButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
image.setVisibility(View.INVISIBLE); //to set the image as invisible
image.setVisibility(View.VISIBLE); // to set it back to visible
}
});
hope this will help you.

change button image in android

I want to change the image of a button in my code. I found this can be done in xml:
please check this link
but the image will not stay on after I released the button. I want to click the button
and the button change to a new image. Can this be done?
in onClick method you need to change the Button's image, this way..
public void onClick(View v) {
if(v==buttonName){
buttonName.setBackgroundResource(R.drawable.imageName);
}
}
Assuming an ImageButton...
You can change the background image in an onClick listener, similar to the following:
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//set button image
myButton.setImageBitmap(myBitmapFile)
}
});

Categories

Resources