Click, Doble click and Hold Button - android

i have a little question. How can i implement click, doble click and hold in a Button. I really need add this functions to my button. I have AndroidStudio 2,3,3. Thanks you!!

button.setOnLongClickListener and button.setOnClickListener should do the trick for long and single clicks respectively.
For double tap here's what I do in the setOnClickListener.
boolean click=false;
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(click==true)
//DO SOMETHING
new Handler().postDelayed(new Runnable(){
public void run(){
click=true;
}, 1000};
});

Your activity has to implement the following interfaces: View.OnClickListener, View.OnLongClickListener.
When you have your ButtonId defined like this: android:id="#+id/button":
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button: //what should happen when the button is pressed
break;
}
}
You also have to set the Listeners in onCreate:
setOnClickListener(this);
setOnLongClickListener(this);
The code for onLongClick looks exactly the same.
Double Tap is a bit more complicated, here you can find how to implement double tap.

Related

Software debounce for Android buttons

I have a simple ImageButton in my Android program which, when clicked, appends a "0" in a TextView. When this button is long clicked, it is supposed to append "+" in that TextView. The program works fine but I'm facing a typical key bouncing effect. When I long press the button, it do appends a "+", but when I release the button, it also appends a "0". It seems like Android registers a second single click when long click ends. How can I eliminate this? Here's what I'm doing:
ImageButton button0=(ImageButton)V.findViewById(R.id.imageButtonzero);
button0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
enterNumber.append("0");
}
});
button0.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
enterNumber.append("+");
return false;
}
});
Thanks for your help!
You need to return true in the OnLongClickListener, to inform other listeners that the event has been consumed and does not need to be actioned upon further down the line :
button0.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
enterNumber.append("+");
return true;
}
});
Source of information : Android javadoc

Android single button multiple functions

I am beginner to Android development. I have 3 edit boxes and one "Edit" button. When I launch the activity all the edit boxes should be disabled. When I click on the Edit button all the 3 edit boxes should get enabled and button text should change to "Save". After updating the data in the edit boxes, when I click on the "Save" button, I should be able to send the updated data to the backend.
My problem is how can I make use of a single button for two function "Edit" and "Save".
Please help me.
You can do it this way:
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String ButtonText = button.getText().toString();
if(ButtonText.equals("Save"){
//code for save
button.setText("Edit");
}
else{
//code for edit
button.setText("Save");
}
}
});
If I were you I would actually use two buttons one for edit, and one for save. Make them the same size and in the same position, when you want to switch between them make one invisible, and the other visible. Doing it that way would let you keep your onClickListeners separate which would make your code more understandable in my mind.
That being said you could technically achieve it with a single button as well. Just change the text on the button when you want to switch between them, and add an if statement into your click listener to check which "mode" your button is currently in to determine which action it should take.
I am not sure there is an easy way to do this or not. but you can sure use different behaviors of button clicks like
// When you press it for long time.
dummyButton.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true; // Can do lot more stuff here I am just returning boolean
}
});
// Normal click of button
dummyButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//do lot more stuff here.
}
});
Do it this way :
Make a public boolean variable
public boolean isClickedFirstTime = true;
make your 3 editTexts enabled false in xml and
onClick of your button
#Override
public void onClick(View v) {
if (v.getId() == R.id.edit_button_id) { //whatever your id of button
Button button = (Button) findViewById(R.id.edit_button_id);
if(isClickedFirstTime)
{
edit1.setEnabled(true);
edit2.setEnabled(true);
edit3.setEnabled(true);
butt.setText("Save");
isClickedFirstTime = false;
}
else
{
....//Get your values from editText and update your database
isClickedFirstTime = true;
}
}

Button get clicked multiple times by clicking it once

I click a Button once, but it seems to be that onClick() gets fired multiple times. I tried with mybutton.setClickable(false); in onClick(), but it doesn't seem to work. Any solutions?
Prevent a widget from getting clicked multiple times in a short span
..
Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mAllowSave) {
mAllowSave = false;
//
}
}
});
and set mAllowSave to true when you need button active orcan use send.postDelay to activate that again...
so put these line in onClick as well so your button will be active after 1/2 second than previous click
send.postDelayed(new Runnable() {
#Override
public void run() {
mAllowSave = true;
}
}, 500);

How to determine which button pressed on android

i need to know, how to recognize, which button is pressed.
Like if i have two buttons ,say button 1 and button2,and both of them performing the same method, say method(),how to determine which button pressed ?
Regards
Most ellegant pattern to follow:
public void onClick(View v) {
switch(v.getId())
{
case R.id.button_a_id:
// handle button A click;
break;
case R.id.button_b_id:
// handle button B click;
break;
default:
throw new RuntimeException("Unknow button ID");
}
This way it's much simplier to debug it and makes sure you don't miss to handle any click.
OR... you can just put a android:onClick="foo" in the xml code of the
button, and define a method on java with the signature. Inside the
method foo, get the id and compare it with the one you need
public void foo(View v){
if (v.getId() == R.id.yourButton){
}
else if (v.getId() == R.id.nextButton){
}
}
I have 10 buttons performing the same method updateText(), I used this code to get the clicked button's text:
public void updateText(View v){
Button btn = (Button) findViewById(v.getId());
String text = btn.getText().toString();
}
If by "performing the same method" you mean theirs OnClickListener then you have to reference the parameter being passed to it.
public void onClick(View v) {
if(v==btnA) {
doA();
} else if(v==btnB) {
doB();
}
}
Ok got the solution
if (yesButton.getId() == ((Button) v).getId()){
// remainingNumber
}
else if (noButton.getId() == ((Button) v).getId())
{
// it was the second button
}

Android: What's the difference between a boolean for a click and button.isPressed()?

There are a lot of options on how to define a click/tap on the touchscreen. One of them for example is setting a boolean.
Example for boolean:
boolean buttonClicked = true;
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (buttonClicked) {
//do that and this
}
}
});
And there's a isPressed() method:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (button.isPressed()) {
//do that and this
}
}
});
What exactly is the difference between them? And when and why do I use boolean and the method isPressed()?
Because you are referring to a button in both of your examples, I assume that you are referring to the user tapping on a button, not just a random touch on the screen.
That being said, both of the examples you provided are not good.
In your first example, the boolean is useless because it is always true, so //do that and this will always be reached.
In your second example, your if statement is useless, because the onClick method by its nature is only reached when the button is tapped.
A good way to listen for a button press is using a click listener like this:
Button button = (Button) findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Code placed here will run every time the button is tapped
}
});
...where R.id.buttonId is the ID of your button in the layout.
If you need to define click event for a View you can use onClickListener, onTouchListener.
For more information check for Android official Documentation.
onTouchListener
onTouchListener
When considering your first code snippet, You can use boolean to perform another operation on button click event. as example something like this ,
boolean buttonClicked = false;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//true after button clicked
buttonClicked = true;
}
});
//if buttonClicked equals true
if (buttonClicked){
//perform operation only after button clicked
}
when considering your second code snippet, no need of button.isPressed() inside
button's onClick() callback. Because what you want to do by checking button.isPressed() is done without it inside button's onClick() callback.
Keep in mind these things.
isPressed() is a public method of View Class
Button is a subclass of View Class
isPressed() is a public method of Button Class as well.
About isPressed() from Android official documentation.
Indicates whether the view is currently in pressed state. Unless
setPressed(boolean) is explicitly called, only clickable views can
enter the pressed state.
Returns true if the view is currently pressed, false otherwise.

Categories

Resources