I have to click the button twice for it to work

If you are inflating view to another one, try to set on parent view:

view.setFocusable(false);

worked for me.


My problem was the Button XML defining:

android:focusableInTouchMode="true"

Remove this attribute and the button doesn't require being touched twice. It appears as though the first touch is consumed to assign focus on the button and the second then triggers the OnClickListener.

Note that the Button works without issue with the android:focusable="true" attribute.


Okay so I finally figured out what caused the problem, by myself. I can't believe I missed such an obvious issue. The thing that caused problem wasn't focus, but the method itself. In my XML file I called onClick method by android:onClick="onClick" and then I also added a buttonlistener inside the onClick method to java code.

All I did was remove the buttonlistener and there's no more double clicking neccessary! So if anyone has this problem in future simply make sure you don't have an onClick method AND buttonlistener at the same time.

Wrong code:

public void submitQuantityButton (View v){

submitButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
.
.
. //REST OF THE CODE

To make it work I simply deleted the onclick listener, leaving only:

public void submitQuantityButton (View v){
.
.
. //REST OF THE CODE