I have to click the button twice for it to work
Asked Answered
L

8

37

So I have a button in my app and an edittext. When I click the button and write something in the edittext, a textview changes. It all works as it should except for one thing. I have to click on the button twice to make it work (only the first time I open activity). The very first time after I open activity I press the button and nothing happens, after that it works as it should.

I already did my research on this and as far as I know the thing that is causing trouble is focus, but I tried a few things and nothing worked.

Button XML code:

<Button
    android:id="@+id/submitButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/editText1"
    android:layout_alignBottom="@+id/editText1"
    android:layout_alignLeft="@+id/checkBox25"
    android:text="@string/addMaterial"
    android:onClick="submitQuantityButton" >
</Button>

Edittext XML code:

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/spinner1"
    android:ems="3"
    android:inputType="number"
    android:maxLength="3" >
</EditText>

I tried adding android:focusableInTouchMode="false" to button XML, I also tried adding requestFocus to button XML and it still doesn't work. I also removed the requestFocus from edittext and it doesn't work. I'm running out of ideas what else to try.

onClick method:

public void submitQuantityButton (View v){
    Button submitButton = (Button)findViewById(R.id.submitButton);
    final Spinner sItems = (Spinner)findViewById(R.id.spinner1);
    final Context context = this;
    final CheckBox cb4 = (CheckBox) findViewById(R.id.checkBox4);
    final CheckBox cb5 = (CheckBox) findViewById(R.id.checkBox5);
    final CheckBox cb33 = (CheckBox) findViewById(R.id.checkBox33);
    final CheckBox cb30 = (CheckBox) findViewById(R.id.checkBox30);
    final CheckBox cb6 = (CheckBox) findViewById(R.id.checkBox6);
    final CheckBox cb7 = (CheckBox) findViewById(R.id.checkBox7);
    final CheckBox cb9 = (CheckBox) findViewById(R.id.checkBox9);
    final CheckBox cb10 = (CheckBox) findViewById(R.id.checkBox10);
    final CheckBox cb11 = (CheckBox) findViewById(R.id.checkBox11);
    final CheckBox cb12 = (CheckBox) findViewById(R.id.checkBox12);

    //
    final AlertDialog.Builder emptyETextErrorBuilder = new AlertDialog.Builder(context);
    emptyETextErrorBuilder.setTitle("Warning");
    emptyETextErrorBuilder.setMessage("Please enter a value before pressing this button");
    emptyETextErrorBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    submitButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final int position = sItems.getSelectedItemPosition();
            EditText quantityEditText = (EditText)findViewById(R.id.editText1);

            switch (position){
            case 0:
                AlertDialog.Builder spinnerErrorBuilder = new AlertDialog.Builder(context);
                spinnerErrorBuilder.setTitle("Warning");
                spinnerErrorBuilder.setMessage("Please choose an item from the list above and then enter a certain value");
                spinnerErrorBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
                AlertDialog spinnerError = spinnerErrorBuilder.create();
                spinnerError.show();
                break;
            case 1:
                String item1 = quantityEditText.getText().toString();
                if (item1.matches(""))
                {
                    AlertDialog emptyETextError = emptyETextErrorBuilder.create();
                    emptyETextError.show();
                }
                else
                {
                    cb4.setText("Elaborate Totem (" + item1 + "/250)");
                }
                break;
            case 2:
                String item2 = quantityEditText.getText().toString();
                if (item2.matches(""))
                {
                    AlertDialog emptyETextError = emptyETextErrorBuilder.create();
                    emptyETextError.show();
                }
                else
                {
                    cb5.setText("Pile of Crystalline Dust (" + item2 + "/250)");
                }
                break;
            case 3:
                String item3 = quantityEditText.getText().toString();
                if (item3.matches(""))
                {
                    AlertDialog emptyETextError = emptyETextErrorBuilder.create();
                    emptyETextError.show();
                }
                else
                {
                    cb33.setText("Pile of Crystalline Dust (" + item3 + "/250)");
                }
                break;
            case 4:
                String item4 = quantityEditText.getText().toString();
                if (item4.matches(""))
                {
                    AlertDialog emptyETextError = emptyETextErrorBuilder.create();
                    emptyETextError.show();
                }
                else
                {
                    cb30.setText("Pile of Crystalline Dust (" + item4 + "/250)");
                }
                break;
            case 5:
                String item5 = quantityEditText.getText().toString();
                if (item5.matches(""))
                {
                    AlertDialog emptyETextError = emptyETextErrorBuilder.create();
                    emptyETextError.show();
                }
                else
                {
                    cb6.setText("Powerful Venom Sac (" + item5 + "/250)");
                }
                break;
            case 6:
                String item6 = quantityEditText.getText().toString();
                if (item6.matches(""))
                {
                    AlertDialog emptyETextError = emptyETextErrorBuilder.create();
                    emptyETextError.show();
                }
                else
                {
                    cb7.setText("Vial of Powerful Blood (" + item6 + "/250)");
                }
                break;
            case 7:
                String item7 = quantityEditText.getText().toString();
                if (item7.matches(""))
                {
                    AlertDialog emptyETextError = emptyETextErrorBuilder.create();
                    emptyETextError.show();
                }
                else
                {
                    cb9.setText("Ancient Bone (" + item7 + "/250)");
                }
                break;
            case 8:
                String item8 = quantityEditText.getText().toString();
                if (item8.matches(""))
                {
                    AlertDialog emptyETextError = emptyETextErrorBuilder.create();
                    emptyETextError.show();
                }
                else
                {
                    cb10.setText("Armored Scale (" + item8 + "/250)");
                }
                break;
            case 9:
                String item9 = quantityEditText.getText().toString();
                if (item9.matches(""))
                {
                    AlertDialog emptyETextError = emptyETextErrorBuilder.create();
                    emptyETextError.show();
                }
                else
                {
                    cb11.setText("Vicious Claw (" + item9 + "/250)");
                }
                break;
            case 10:
                String item10 = quantityEditText.getText().toString();
                if (item10.matches(""))
                {
                    AlertDialog emptyETextError = emptyETextErrorBuilder.create();
                    emptyETextError.show();
                }
                else
                {
                    cb12.setText("Vicious Fang (" + item10 + "/250)");
                }
                break;
            }
        }
    });
}
Lisettelisha answered 23/6, 2013 at 14:42 Comment(8)
When you say a few things, have you tried to clear focus?Dyewood
yes, i tried that but I forgot to mention it in the questionLisettelisha
Wait, where should I try clearfocus? I only tried adding it to edittext in XML file.Lisettelisha
Post your onClick...maybe you are doing something in thereCalistacalisthenics
I did, check the edited postLisettelisha
Here is an answer I have given to a similar question #45075352Merge
Have you get the answer..? I Also have the same issue.Trilly
Here is a better approach to this problem https://mcmap.net/q/326928/-onclick-event-is-not-triggering-androidImmunochemistry
L
9

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
Lisettelisha answered 26/6, 2013 at 13:37 Comment(3)
This isn't working for me for some reason. I don't have two onClicks, I only add one in my code. It does change the state of the button to focussed first before working, so I am simply going to extract the content of the onClick method and call it both on the onClick event and on the onFocus event. Just like an onClick with multiple buttons, catch all onFocus events with an onFocusChangeListener and then check if the button is selected.Clew
can't believe, exactly same for me :/ grrr databinding... :) 🤦‍♂️Ibbison
worked! I made a similar mistake of defining click listener inside that another click listener 🤦🏻‍♂️Baras
A
52

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.

Aiglet answered 5/11, 2015 at 4:27 Comment(3)
Changing focusableInTouchMode was the solution for me. Thanks!Selwyn
I am using an imageview but this is not working for me?? What should i do?Cedar
Great solution! Thank you very much!Wordsmith
L
9

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
Lisettelisha answered 26/6, 2013 at 13:37 Comment(3)
This isn't working for me for some reason. I don't have two onClicks, I only add one in my code. It does change the state of the button to focussed first before working, so I am simply going to extract the content of the onClick method and call it both on the onClick event and on the onFocus event. Just like an onClick with multiple buttons, catch all onFocus events with an onFocusChangeListener and then check if the button is selected.Clew
can't believe, exactly same for me :/ grrr databinding... :) 🤦‍♂️Ibbison
worked! I made a similar mistake of defining click listener inside that another click listener 🤦🏻‍♂️Baras
C
3

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

view.setFocusable(false);

worked for me.

Carrageen answered 1/9, 2017 at 23:18 Comment(0)
N
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;
            });
Neuralgia answered 18/10, 2017 at 9:42 Comment(0)
A
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"
Afterburner answered 23/6, 2013 at 14:49 Comment(5)
try android:descendantFocusability="beforeDescendants" android:focusableInTouchMode="true" in your layout that contains button and edittextAfterburner
Added those 2 in my relative layout (I think that's what you meant) and it sadly still won't workLisettelisha
I did edittext.clearfocus() but it doesn't work. But I couldn't add layout.requestFocus() (eclipse underlined it with red)Lisettelisha
did u try to set focus on your button?Afterburner
Yes, I tried doing it in XML (requestFocus) and it didn't work. After all this time trying to focus it I started thinking maybe after all it isn't a focus problem...Lisettelisha
N
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.

Nephogram answered 7/2, 2017 at 14:38 Comment(0)
W
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 !

Wieldy answered 17/5, 2020 at 17:8 Comment(0)
G
-1

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

Greatgrandaunt answered 23/6, 2013 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.