Visible ImageView with onClick get invisible again?
Talk guys, I have an image that starts invisible and when you click the button it is visible, but I need it to be invisible again because when you click the button again, it has to appear again, because it has "disappeared" for time in the animation. Do you have a simple way to do this? For by methods or Classes, I always curl up. For I do not find the full explanation of how to pull the method and talz.
Edit for working code.
btQuestions.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
seuLayout.setVisibility(View.VISIBLE);
seuLayout.animate().alpha(0f).setDuration(5000);
showButtons();
}
});
Obs: seuLayout has to be INVISIBLE in xml.
2 answers
You can use a Handler
, which is a class that schedules tasks as per your need through a Thread
according to the chosen time.
btQuestions.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView img = (ImageView) findViewById(R.id.image);
img.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable(){
public void run() {
img.setVisibility(View.INVISIBLE);
}
}, 3000);
}
});
Good Morning. If anyone ever needs it, it's that way. Note: *seuLayout is Relative, contrain, linear... that will do the action on everything inside. We .alpha(1f).setDuration (0); it is to see the seuLayout again because on the click of the button in addition to being visible had Animation to go disappearing after a while. And to complete after a while seuLayout becomes INVISIBLE again, so that the action of the button can make it VISIBLE again as needed.
private void showButtons(){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
seuLayout.setVisibility(View.INVISIBLE);
seuLayout.animate().alpha(1f).setDuration(0);
}
},2000); //Tempo para tornar seuLayout INVISIBLE.
}
To pull the method for inside the onClick I used showButtons (); inside the button action.