1

I implemented a custom button and added a task to it with delay so it shows the animation. When I double click it, it crashes. I want to make so it's only clickable once.

i have tried setEnabled(false); i have tried setClickable(false);

i tried a variable that check if a button has been clicked and disables it.

public class Login extends AppCompatActivity {

Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    final SubmitButton LoginBtn = findViewById(R.id.login);
    handler = new Handler();
    LoginBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LoginBtn.setEnabled(false);
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    LoginBtn.setEnabled(true);
                    Intent startActivity = new Intent(Login.this, Main_page.class);
                    startActivity(startActivity);
                    finish();
                }
            }, 3200);
        }
    });

}
}

As I wrote, I want that if the button has been clicked that it becomes unclickable.

Dustaboy3
  • 43
  • 8
  • You have `LoginBtn` declared as `final`, doesn't that mean you will not be able to change the value? – Nexevis May 16 '19 at 19:55
  • When i try to use is without final android studio sends an eror that it's accesing variable within inner class and it has to be declared final. – Dustaboy3 May 16 '19 at 20:03
  • Post the stack trace. – shmosel May 16 '19 at 21:17
  • Since i implemented the if function the app doesn't crash anymore. Although now i can press on the button as many times i want and after 3200 miliseconds that many activites open up at least that many animations are seen. I am going to delete the if function and post the stack. – Dustaboy3 May 16 '19 at 21:21
  • 2019-05-16 23:28:25.582 1631-1703/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block? 2019-05-16 23:28:25.587 2642-10416/com.google.android.googlequicksearchbox:search E/IAudioFlinger: createRecord returned error -12 2019-05-16 23:28:25.587 2642-10416/com.google.android.googlequicksearchbox:search E/AudioRecord: AudioFlinger could not create record track, status: -12 2019-05-16 23:28:25.588 2642-10416/com.google.android.googlequicksearchbox:search E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12. – Dustaboy3 May 16 '19 at 21:30

2 Answers2

0

Try this:

Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    final SubmitButton LoginBtn = findViewById(R.id.login);
    handler = new Handler();
    LoginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginBtn.setEnabled(false);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        LoginBtn.setEnabled(true);
                        Intent startActivity = new Intent(Login.this, Main_page.class);
                        startActivity(startActivity);
                        finish();
                    }
                }, 3200);
            }

        });
...

Explanation: Your enable/disable block is outside of the onClick listener and it's in the onCreate method: in this way you call setEnable() method only when the activity was created.

But some time setEnable() can not work in case of very rapid click like explained in the second response here. In that case you can use a timer to check the elapsed time.

By the way, I think that your app crash because you don't handle in the right way the Handler. I suggest you also to add:

@Override
protected void onStop(){
    handler.removeCallbacksAndMessages(null);
}
FedeFonto
  • 354
  • 3
  • 14
  • using the first chunk of the code you solved the problem of a crash and now i can press it more than once but it opens only one activity i am going to add the second part of your code to see if it solves it completely – Dustaboy3 May 16 '19 at 21:34
  • should i add the onStop method inside onClick method – Dustaboy3 May 16 '19 at 21:34
  • you add this code not in the onClick, but outside, under the onCreate – FedeFonto May 16 '19 at 21:36
  • I suggest you to study the [activity lifecycle](https://developer.android.com/guide/components/activities/activity-lifecycle) that explain how an Activity work. – FedeFonto May 16 '19 at 21:38
  • 1
    Okay thank you, at least now it does not crash you have been very helpfull. i Will study it. – Dustaboy3 May 16 '19 at 21:39
0

Try...

view.setOnClickListener(null);

...after your click event.

Richard Dapice
  • 838
  • 5
  • 10