Ad

After Calling SetEnabled(false) Button Still Can Be Pressed One More Time

When clicking a button I immediately call setEnabled to false however this does not disable the button and I have to press again to disable.

I have tried to put setEnabled in different places but with the same result.

chooseLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseLeft.setEnabled(false);
                chooseRight.setEnabled(true);
                docRefPosts.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        if (task.isSuccessful()) {
                            if(!chooseRight.isEnabled())
                                docRefPosts.update("votesForRight", FieldValue.increment(-1));
                            docRefPosts.update("votesForLeft", FieldValue.increment(1));
                        }
                    }
                });
            }
        });

        chooseRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                chooseRight.setEnabled(false);
                docRefPosts.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        if (task.isSuccessful()) {
                            if(!chooseLeft.isEnabled())
                                docRefPosts.update("votesForLeft", FieldValue.increment(-1));
                            docRefPosts.update("votesForRight", FieldValue.increment(1));
                        }
                    }
                });
                chooseLeft.setEnabled(true);
            }
        });

It should function as a radio button where once selected only one can be chosen at a time.

Ad

Answer

I found the answer and I only have a guess as to why it is working. What I did was move all the code for the OnCompleteListener into a separate method. I think that the listener was running before the app could register setEnabled(false). If anyone has any thoughts on this reasoning I would love to hear.

Ad
source: stackoverflow.com
Ad