Issue
I want to implement the button on android. That button has two functions, single press to toast message, if the user instantly press button twice within millisecond then it will show the alert dialog, if delay to press than it will show toast message. Please give me some idea how to perform these functions android Button.
Solution
Try this,
Create a boolean before onCreate
private boolean pressedOnce = false;
In onClickListener use an handler for a second
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (pressedOnce) {
// show the alert dialog
}
pressedOnce = true;
Toast.makeText(MainActivity.this, "Clicked once!", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
pressedOnce=false;
}
}, 1000);
}
});
Answered By - Raghavendra
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.