aj10
aj10

Reputation: 1043

how to disable multiple clicks on menu option in android

How do you disable multiple clicks on a menu option, before the first click is processed?

Upvotes: 4

Views: 2223

Answers (3)

tiagocarvalho92
tiagocarvalho92

Reputation: 427

I know that this is an old question but I want to share a reactive approach.

Fragment/Activity onOptionsItemSelected:

    if (item.getItemId() == yourId) {
      viewModel.showTopUp()
      return true;
    }
    return super.onOptionsItemSelected(item);

In the ViewModel create a PublishSubject and throttle the requests to prevent multiple clicks:

private PublishSubject<Context> topUpClicks = PublishSubject.create();

public void showTopUp(Context context) {
    topUpClicks.onNext(context);
}

private void handleTopUpClicks() {
    disposables.add(topUpClicks.throttleFirst(1, TimeUnit.SECONDS)
        .doOnNext(transactionViewNavigator::openTopUp)
        .subscribe());
}

Upvotes: 1

Flo
Flo

Reputation: 27455

You can set the visibility or enable/disable the item by code.

MenuItem item = menu.findItem(R.id.your_item);
item.setVisible(true);
item.setEnabled(false);

Of course you have to check somewhere whether to enable oder disable the icon.

Upvotes: 4

Blundell
Blundell

Reputation: 76466

Psuedo/Android answer:

 private boolean clicked = false;

 @Override
 public onClick(View v){
   if(!clicked){
       clicked = true;


       // do your processing - one click only


       super.onClick();
   }
 }

EDIT

or even better after the first click you can call yourView.setOnClickListener(null); to remove the onClick

Upvotes: 2

Related Questions