score:8

Accepted answer

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

score:-1

Simply use getText() to get text from EditText And then set text of TextView using setText().

score:0

try this once
UPDATED

override onResume method in your activity and then clear focus from your edittext and set focus to your main layout

edittext.clearFocus();
layout.requestFocus();

or

button.requestFocus();

if the problem is with virtual keyboard it might help you

in your AndroidManifest.xml file inside your Activity tag put this line

android:windowSoftInputMode="stateHidden"

score:0

I had the same problem as the OP. I tried all the focus-related suggestions, but none of them worked every time for me.

I tried removing the NavigationDrawer from my layout, but that didn't work.

Lastly, I tried replacing my CoordinatorLayout with a LinearLayout and now my buttons click first time every time. Could be worth a try.

score:0

By adding the following lines :

 <Button
    android:id="@+id/sauvegarder"
    android:text="Sauvegarder"
    android:layout_gravity="center"
    android:background="@color/colorAccent"
    android:layout_margin="@dimen/fontmargin"
    style="@style/edit"
    android:gravity="center"
    android:textColor="@color/colorPrimary"
    android:layout_width="220dp"
    android:layout_height="wrap_content"
    android:focusableInTouchMode="false"
    />

in my xml file it works perfectly !

score:2

Sometime I had it problem when click on btn or txt or edt on the fragment, and realy helps use instead .setOnClickListener() need .setOnTouchListener like this example:

txtClose.setOnTouchListener((v, event) -> {
                // do staff...
                return false;
            });

score:3

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

view.setFocusable(false);

worked for me.

score:50

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.


Related Query

More Query from same tag