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);
}
Related
How to create a fade in ImageView to change it to other image (i.e. from Image 1 to Image 2) when imageView is clicked and vica versa
I have created another click method but it is not working because i think as I have already put image-2 aplha to 0.
public void fade(View view)
{
ImageView mickey=findViewById(R.id.mickey);
ImageView mouse = findViewById(R.id.imageView2);
mickey.animate().alpha(0f).setDuration(2000);
mouse.animate().alpha(1f).setDuration(2000);
}
Here is a simple method to play around with: :)
boolean firstImage = false;
private void fadeFlip(View view) {
AlphaAnimation aa = new AlphaAnimation(1f,0f);
aa.setDuration(1*1000);
view.startAnimation(aa);
aa.start();
if(firstImage) {
view.setBackgroundResource(R.drawable.ic_launcher);
} else {
view.setBackgroundResource(R.drawable.image_2);
}
AlphaAnimation bb = new AlphaAnimation(0f,1f);
bb.setDuration(1*1000);
view.startAnimation(bb);
bb.start();
firstImage = !firstImage;
}
I want to have a favorite button on my app like in Gmail
So, when the user click on the star, the star became yellow, and when the user clicked it again, it turn back to normal
How can i make this happen with my custom image?
i have two images
when its not favorited (heart-grey.png)
and when its favorited (heart-red.png)
You can use visibilty to do this.You have to define the xml to have both images at the same postion(It can be done using Relative layout).
final ImageView play = (ImageView) findViewById(R.id.play);
final ImageView play2 = (ImageView) findViewById(R.id.play2);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Drawable playImage = play.getDrawable();
if (playImage.isVisible()){
play.setVisibility(View.GONE);
play2.setVisibility(View.VISIBLE);
mediaPlayer.start(); }
}
});
play2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Drawable playImage2 = play2.getDrawable();
if (playImage2.isVisible()){
play2.setVisibility(View.GONE);
play.setVisibility(View.VISIBLE);
mediaPlayer.pause();
}
If your star is an ImageButton you can do something like this :
starSelected.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//add a condition to detect if it is already a favorite or not
if(starSelected.getDrawable() == R.drawable.theimage2) {
starSelected.setImageResource(R.drawable.thenewimage);
}else{
starSelected.setImageResource(R.drawable.thenewimage2);
}
}
});
Hope it helps
In your java code create a boolean flag:
boolean isSelected = false
Set an onClickListener inside your onCreate for the star (ImageView, Button whatever it is). Inside onClick, check for the flag like this:
if (isSelected) {
// change image src to unselected
isSelected = false;
} else {
// change image src to selected
isSelected = true;
}
and also you can save the boolean state with SharedPreferences to make sure you get the correct state every time.
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
I am trying to change the background image of a button that is clicked. The button whose image I am trying to toggle is the same button that is clicked. I ultimately want the program to test the current background image and change it to the other picture given the result of the test.
final Button testButton = (Button) findViewById(R.id.buttonTestButton);
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//toggle picture
if (testButton.equals(getResources().getDrawable(R.drawable.fakepicture))) {
testButton.setBackgroundResource(R.drawable.alternatepicture);
}
else {
testButton.setBackgroundResource(R.drawable.fakpicture);
}
}//end void onClick
});//end test button on click listener
try
testButton.getBackground().equals(getResources().getDrawable(R.drawable.fakepicture));
However ToggleButton might suit your case better.
As others have said, your equals method is comparing the button itself with the image, but you need to compare the background drawables.
I recommend loading the images drawables you want to use and then using their references later to make things more clear, something like this:
final Drawable first = getResources().getDrawable(
android.R.drawable.arrow_up_float);
final Drawable second = getResources().getDrawable(
android.R.drawable.arrow_down_float);
final Button testButton = (Button) findViewById(R.id.toggleButton);
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (testButton.getBackground().equals(first)) {
testButton.setBackgroundDrawable(second);
} else {
testButton.setBackgroundDrawable(first);
}
}
});
as the other friends answered , it is preferable to use the ToggleButton in Android ,
and in your case, if you want to keep your code , so your method should be like this :
final Button testButton = (Button) findViewById(R.id.buttonTestButton);
int status = 0;//GLOBAL VARIABLE : the status of the Button ( 0 or 1 )
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//toggle picture
if (status == 0) {
testButton.setBackgroundResource(R.drawable.alternatepicture);
status=1 ; // change the status to 1 so the at the second clic , the else will be executed
}
else {
testButton.setBackgroundResource(R.drawable.fakpicture);
status =0;//change the status to 0 so the at the second clic , the if will be executed
}
}//end void onClick
});//end test button on click listener
You can simply use ToggleButton: Android ToggleButton and use StateList for the changing of the background: StateList using the check attribute.
You can use Buttons or Image buttons..
private ImageButton mod1,mod2;
mod1 = (ImageButton) findViewById(R.id.mod1);
mod2 = (ImageButton) findViewById(R.id.mod2);
mod1.setOnClickListener(this);
mod2.setOnClickListener(this);
public void onClick(View v) {
mod1.getDrawable().clearColorFilter();
mod2.getDrawable().clearColorFilter();
switch (v.getId()) {
case R.id.mod1:
mod1.getDrawable().setColorFilter(0xfff47521,PorterDuff.Mode.SRC_ATOP);
break;
case R.id.mod2:
mod2.getDrawable().setColorFilter(0xfff47521,PorterDuff.Mode.SRC_ATOP);
break;
}
}
Can I make ImageView in android make some action when I click on that ImageView ?
For example, when I click on ImageView imgV that some panel be visible and when I click on other ImageView imgUV that he become unvisible .
imgV.setOnClickListener( new OnClickListener() {
public void onClick(View v)
{
int i=tbrSearchNear.VISIBLE;
tbrSearchNear.setVisibility(1-i);
tbrSearchCriterium.setVisibility(i);
}
});
It looks like that doesn't register click at all .
Maybe ImageButton is what you are looking for? http://developer.android.com/reference/android/widget/ImageButton.html
Setting visbility to 1-View.VISIBLE translates to 1-0; if you want to hide it, write something like:
imgV.setOnClickListener( new OnClickListener() {
public void onClick(View v)
{
// toggle visibility
int visibility=tbrSearchNear.getVisbility() == View.VISIBLE ? View.GONE : View.VISIBLE;
tbrSearchCriterium.setVisibility(tbrSearchNear.getVisbility());
tbrSearchNear.setVisibility(visbility);
}
});