How to prevent button onClick firing repeatly? - android

I have a Button which sets both onLongClickListener & onClickListener :
onLongClickListener :
#Override
public boolean onLongClick(View v) {
do something ...
return true;
}
onClickListener :
#Override
public void onClick(View v) {
do something else ...
}
When I long click the button, onLongClick fires repeatly
(sometimes onClick fires too when I release the button, it's weird ##")
What I want is to make the onLongClick be triggered only once for one long press.
So I modified the code :
onLongClickListener :
#Override
public boolean onLongClick(View v) {
do something ...
myButton.setLongClickable(false);
return true;
}
onClickListener :
#Override
public void onClick(View v) {
myButton.setLongClickable(true);
do something else ...
}
Unfortunately, the onClick callback was locked too after onLongClick fires!
I cant unlock the button anymore :|
Whats wrong with my code? Also, why onClick sometimes works when I release my button after a long click?

I've got the code you need, give me a minute to post it.
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//DO STUFF GRAH!
}
});
btn.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
//OTHER STUFF
return true;
}
});
This worked fine for me. I made an int and onLongClick added one and displayed it in a toast. Always incremented by one, and didn't do the onClick (reset it to 0).

Related

how to hide Text selection handle from webview : android

i have a custom webview on which user can select text, i want to hide the "TEXT SELECTION HANDLES" when user click on some button at the bottom , i want the text to be selected but want to hide the handles, as you can see below :
// call hideTextSelection() from your onCreate()
function hideTextSelection(){
//SOLUTION 1-> vibration caused by the long click not works here
webView.setHapticFeedbackEnabled(false);
//Solution 2:
/*webView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true;
}
});
webView.setLongClickable(false);*/
}
// whenever you wants to select the data ie. after onClicking the button 'from the bottom of your UI', make it to select as below
yourBottomButton.setOnClickListener(new OnClickListener() {
#Override
public boolean onClick(View v) {
webView.setHapticFeedbackEnabled(true);
}
});
Try this
mWebView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true;
}
});
mWebView.setLongClickable(false);

Boolean to check if a view is being clicked

I have a question concerning the views in android studio:
Is there a method which returns a Boolean to indicate if a "view is on a longclick"
By setting an onClickListener on it, then taking action when the listener's onClick() method is called, e.g.:
View btnView = view.findViewById(R.id.btnView);
btnView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do something
}
});

Is there a way to reset the OnClickListener to its default Android implementation?

I have an editText to which I have set an OnClickListener, which is set to open a Dialog. But I have an option in the Dialog to let the user enter data into the editText by manually typing. I tried calling setOnClickListener(null), but it makes the editText unresponsive.
As of yet I have tried a lot of things, but the only thing that works is recreating the activity by calling recreate(), but I'd rather the user not know that I'm recreating the Activity.
How do I reset the editText to behave normally like an Android editText works? (like opening the keyboard and entering data on tapping it)
Change
editText.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
//do as u wish
}
}
);
to
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// do as you wish
}
}
});
You can use a boolean member variable to keep track of when to allow user input and when to show the dialog.
private boolean mShouldAllowInput = false;
Then in your custom click listener you could do something like:
private View.OnClickListener editClickListener = new View.OnClickLIstener() {
#Override
public void onClick(View v) {
if(!mShouldAllowInput) {
showDialog();
mShouldAllowInput = true;
}
}
}
Now you need a way to revert back the value of the boolean member variable back to false. You can reset in the DialogInterface.OnClickListener as per your business logic.
To reset the OnClickListener of an EditText I tried:
View.OnClickListener defaultOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
v.requestFocus();
}
};
...
editText.setOnClickListener(defaultOnClickListener);
and it seems to work fine!

How to achieve on first click listener in android?

I have a customView , i want to set onClick which will only be called on the very first click. In which i want to start a thread which will start a counter on other TextView , with simple onClickListener with each click a new threads starts which is a problem . How can i achieve such task ?
Another option is in your onClick() method do set a null listener, i.e.
#Override
public void onClick(View view) {
// disable any other clicks from now on
customView.setOnClickListener(null);
...
}
I think this is only logic problem, So I solve this problem by using a boolean variable for the first click:
boolean isFristClick = true;
#Override
public void onClick(View v) {
if (isFristClick) {
// Start your counter Thread here
isFristClick = false;
} else {
// Do nothing
}
}
What about making a workaround for that?!! like assigning a boolean value to tell if it's the first click:
private boolean first_click = true;
your_view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(first_click){
first_click = false;
// Do something on first click
}else{
// Do another thing on later clicks
}
}
});

clicklistener and longclicklistener on the same button?

i am creating a call/dial button, when i click on that call/dial button, a call will be made based on the input that is displayed in the edittext. I managed to do that part. can you guys advise me whether i can do a longer click on that same call/dial button, so that a toast can come out to ask user to choose something else??
I did some research on "setOnLongClickListener" but i am not sure if i can combine it in the same call/dial button? I have attached on the working dial function which i managed to do, wondering if the "setOnLongClickListener" can be combined together somehere in the code?
private void dialANumber() {
try {
buttonCall = (ImageButton) findViewById(R.id.imageButton2);
buttonCall.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (display != null) {
Intent callNumber = new Intent();
callNumber
.setAction(android.content.Intent.ACTION_CALL);
callNumber.setData(Uri.parse("tel:" + display.getText()));
startActivity(callNumber);
}
}
});
} catch (ActivityNotFoundException anfe) {
Log.e("DialANumber", "Dialing the number failed", anfe);
}
this code is working. i hope a longer click can be made on the same call/dial button so the button can have a normal click to make a call, and longer click to pop out a toast. Thanks in advance.
Note that returning "false" on the long click listener will have the UI responding to the long click as a short click too. Return "true" if you want to kill that off. "True" means "yes, I used this event" and "false" means "whether I used it or not, the environment is free to respond as well." (I know this because I just used AkashG's answer in my own app.)
A GestureDetector with a SimpleOnGestureListener would help you differentiate between the different types of presses. A GestureDectector is a class which can read different types of touch events (for example, single taps and long presses), and sends them to a listener which handles each type differently. Here's the documentation on the Detector and Listener.
http://developer.android.com/reference/android/view/GestureDetector.html
http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html
First, set up your SimpleOnGestureListener, the important methods for you to override will be onSingleTapUp and onLongPress. In your onCreate, create an instance of GestureDetector that references your listener. Then, attach an OnTouchListener to your button that sends the event to your detector. You'll want it to look something like this:
//Set up the Detector
GestureDetector.SimpleOnGestureListener myGestureListener = new GestureDetector.SimpleOnGestureListener()
{
#Override
public boolean onSingleTapUp(MotionEvent e)
{
//Your onClick code
return false;
}
#Override
public void onLongPress(MotionEvent e)
{
//Your LongPress code
super.onLongPress(e);
}
};
//And make a variable for your GestureDetector
GestureDetector myGestureDetector;
...
#Override
onCreate(Bundle b)
{
...
myGestureDetector = new GestureDetector(myActivity.this, myGestureListener);
...
}
...
//And finally, wherever you are setting up your button
button.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
myGestureDetector.onTouchEvent(motionEvent);
return false;
}
There a a bunch of other types of events this class can interpret in case you want to get even more fancy. GestureDetector is a very good class to do a little research on, it can be very useful and isn't too complex. Hope this helps.
Yes you can do this:
XML file:
<Button
android:id="#+id/call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CALL"/>
<ImageButton
android:id="#+id/callBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"/>
For button click event:
Button button=(Button) findViewById(R.id.call);
button.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
Toast.makeText(getBaseContext(), "Long CLick", Toast.LENGTH_SHORT).show();
return false;
}
});
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (display != null) {
Intent callNumber = new Intent();
callNumber
.setAction(android.content.Intent.ACTION_CALL);
callNumber.setData(Uri.parse("tel:" + display.getText()));
startActivity(callNumber);
}
}
});
For imageButton:
ImageButton imageButton=(ImageButton) findViewById(R.id.callBtn);
imageButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(check==false){
Toast.makeText(getBaseContext(), "CLick", Toast.LENGTH_SHORT).show();
}
imageButton.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
check=true;
if(check){
Log.d("bool", check+"");
Toast.makeText(getBaseContext(), "Long CLick", Toast.LENGTH_SHORT).show();
check=false;
}
return false;
}
});
Declare this at the top(golbally):
boolean check=false;

Categories

Resources