onClick event is not triggering function
- JavaScript is case-sensitive. The property is
onclick
(but you should probably be using addEventListener anyway). - Putting
()
on the end of a function name will call the function. You want to assign it to a property. Remove the parenthesis.
onClick event is not triggering | Android
Overview, when a user interacts with any UI component the various listeners are called in a top-down order. If one of the higher priority listeners "consumes the event" then the lower listeners will not be called.
In your case these three listeners are called in order:
- OnTouchListener
- OnFocusChangeListener
- OnClickListener
The first time the user touches an EditText it receives focus so that the user can type. The action is consumed here. Therefor the lower priority OnClickListener is not called. Each successive touch doesn't change the focus so these events trickle down to the OnClickListener.
Buttons (and other such components) don't receive focus from a touch event, that's why the OnClickListener is called every time.
Basically, you have three choices:
Implement an OnTouchListener by itself:
ed1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(MotionEvent.ACTION_UP == event.getAction())
editTextClicked(); // Instead of your Toast
return false;
}
});This will execute every time the EditText is touched. Notice that the listener returns
false
, this allows the event to trickle down to the built-in OnFocusChangeListener which changes the focus so the user can type in the EditText.Implement an OnFocusChangeListener along with the OnClickListener:
ed1.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
editTextClicked(); // Instead of your Toast
}
});This listener catches the first touch event when the focus is changed while your OnClickListener catches every other event.
(This isn't a valid answer here, but it is a good trick to know.) Set the focusable attribute to
false
in your XML:android:focusable="false"
Now the OnClickListener will fire every time it is clicked. But this makes the EditText useless since the user can no longer enter any text...
Note:
getApplicationContext()
can create memory leaks. A good habit is to avoid using it unless absolutely necessary. You can safely use v.getContext()
instead.
Hope that helps!
onClick event is not working on Android Chrome
Here's a fiddle using jQuery.
$('#btn').on('touchstart click', function() { var cupon = $('#btn').attr('val1'); var link = $('#btn').attr('val2'); var copyText = document.createElement("input"); document.body.appendChild(copyText); copyText.setAttribute("id", "copyTextId"); copyText.setAttribute('value', cupon); copyText.select(); document.execCommand("copy"); document.body.removeChild(copyText); alert("Cuponul " + "'" + copyText.value + "'" + " a fost copiat. Spor la cumparaturi! :)"); window.open(link);});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><button id='btn' val1='test to copy' val2='http://www.google.ro'>Copy coupon</button>
Android DataBinding onClick event not working
You have to call setActivity()
on your binding to actually initialize your activity variable.
Android onClick event not working
Several suggestions:
you declared android:onClick="onClickRadGard"
in your xml. That means that your activity will expect to find a method with the following signature:
public void onClickRadGard(View view) {
}
if it is not declared your application will crash. This method will be invoked when you press on your android:id="@+id/button1"
, so you do not need to call setOnClickListener
on it.
About the compile time error you are getting, I took a look to the the code you posted in the link. Those two lines have to be defined after setContentView
, in the body of onCreate
final Button buttonx = (Button) findViewById(R.id.button1);
buttonx.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//other code
}
});
Related Topics
Java.Lang.Illegalmonitorstateexception: Object Not Locked by Thread Before Wait()
Android Import Java.Nio.File.Files; Cannot Be Resolved
Customised Listview Using Arrayadapter Class in Android
How to Create Android Virtual Device
Android Audio Fft to Retrieve Specific Frequency Magnitude Using Audiorecord
Opengl Extensions Available on Different Android Devices
Android Resource - Array of Arrays
Intellij Idea with Junit 4.7 "!!! Junit Version 3.8 or Later Expected:"
Elegant Way to Read File into Byte[] Array in Java
How to Use Okhttp to Upload a File
How to Know Whether I am in a Call on Android
What's a Good Library for Parsing Mathematical Expressions in Java
Android How to Listen for Volume Button Events
Android: Switch Camera When Button Clicked
How to Change a Bitmap's Opacity
What Is Difference Between @+Id/Android:List and @+Id/List