Reputation: 1922
This is the code I'm working on:
if(place.equalsIgnoreCase("Department Store")){
Thread thread = new Thread()
{
@Override
public void run() {
t = Toast.makeText(Map.this, "Department Store", Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
}
};
thread.start();
}
Basically, what I wanna do is when user clicks on the button and if it satisfies the condition (i.e. "department store"), the imagebutton will change its image resource for 5 seconds and then returns to its default image resource. How can I do this? I'm thinking of using thread as per my post above, but I can't seem to think of nice way to implement it. Any help is much appreciated. Thanks.
Upvotes: 1
Views: 5409
Reputation: 51571
Use Handler().postDelayed()
for this:
// Original image
// This could have been set in your layout file.
// In this case, you can skip this statement.
imageButton. setImageDrawable(getResources().getDrawable(
R.drawable.some_drawable_id));
someButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (someCondition) {
// Change image
imageButton.setImageDrawable(getResources().getDrawable(
R.drawable.some_drawable_that_will_stay_for_5_secs));
// Handler
new Handler().postDelayed(new Runnable() {
public void run() {
// Revert back to original image
imageButton.setImageDrawable(getResources().getDrawable(
R.drawable.some_drawable_id));
}
}, 5000L); // 5000 milliseconds(5 seconds) delay
}
}
});
Upvotes: 4