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);
Related
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?
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.
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);
}
I want to create simply application where user can review some pictures. In list I have some pictures and I want to show next pictures after click button. So this is the way: user start application and after click on the button, previous picture is replaced by next picture from the list. I want to use ImageView to show pictures. Can anyone help me?
I tried:
ImageView img = (ImageView)findViewById(R.id.image);
img.setImageResource(picturelist.get(pictureLeft));
where picturelist is list with pictures, and pictureLeft is int variable represents my index. To my list I add elements by:
picturelist.add(R.drawable.car);
do something like this:
yourButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
if(pictureLeft < picturelist.size())
{
img.setImageResource(picturelist.get(pictureLeft));
pictureLeft++;
}
}
});
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)
}
});