Onclick Event Is Not Triggering | Android

onClick event is not triggering function

  1. JavaScript is case-sensitive. The property is onclick (but you should probably be using addEventListener anyway).
  2. 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:

  1. OnTouchListener
  2. OnFocusChangeListener
  3. 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:

  1. 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.

  2. 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.

  3. (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



Leave a reply



Submit