I am working on an android application, Eclipse IDE + ADT, targeting API number 17 and debugging on BlueStacks. I have to trigger certain actions on the click of a button, for that I am using a method in the activity class, and in the XML code I am using this:
android:onClick="myCoolMethod"
And it seems to work OK, but it works always after a second click on the button. My question is: Is this supposed to be this way given the attribute? If so, is there another attribute that responds to the first click? If not, why is it not responding to the first click, and what can I do to solve it?
In response to comments:
public void myCoolMethod(View v){
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v==findViewById(R.id.button1)){
Intent inta = new Intent(MainActivity.this, datainsert.class);
startActivity(inta);
}
}
});
And yes, it is a standalone button.
You are setting the on click listener twice! Remove the on click listener from my cool method and it will work
public void myCoolMethod(View v){
Intent inta = new Intent(MainActivity.this, datainsert.class);
startActivity(inta);
}
I hope you see why this is happening. You first set the onclick in XML. Then set it again once the first click happens (only this time programatically) so it overrides the first onclick. So only after the second click the intent is sent. Program functions as intended. Just use the code above to fix