Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid multiple button click at same time in android?

I'm using two button in view. While clicking two button simultaneously it will goes to different activity at a time. How to avoid this?

I have tried like this, But its not working please save....

public class MenuPricipalScreen extends Activity {   @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.menu_principal_layout);       findViewById(R.id.imageView2).setOnClickListener(new OnClickListener() {          @Override         public void onClick(View arg0) {             // TODO Auto-generated method stub              disable(findViewById(R.id.imageView3));              Intent intent = new Intent(MenuPricipalScreen.this,                     SelectYourLanguageVideo.class);             startActivity(intent);         }     });     findViewById(R.id.imageView3).setOnClickListener(new OnClickListener() {          @Override         public void onClick(View arg0) {             // TODO Auto-generated method stub              disable(findViewById(R.id.imageView2));              Intent intent = new Intent(MenuPricipalScreen.this,                     CategoryScreen.class);             intent.putExtra("request", "false");             startActivity(intent);         }     });  }   @Override protected void onResume() {     // TODO Auto-generated method stub     super.onResume();     ((ImageView) findViewById(R.id.imageView3)).setEnabled(true);     ((ImageView) findViewById(R.id.imageView2)).setEnabled(true);     ((ImageView) findViewById(R.id.imageView3)).setClickable(true);     ((ImageView) findViewById(R.id.imageView2)).setClickable(true);     ((ImageView) findViewById(R.id.imageView3)).setFocusable(true);     ((ImageView) findViewById(R.id.imageView2)).setFocusable(true); }   private void disable(View v) {     Log.d("TAG", "TAG" + v.getId());     v.setEnabled(false);     v.setClickable(false);     v.setFocusable(false); } } 

Thanks,

like image 859
Murali Ganesan Avatar asked Jan 07 '14 12:01

Murali Ganesan


People also ask

How do I turn off multiple clicks on Android?

The actual solution to this problem is to use setEnabled(false) which greys out the button, and setClickable(false) which makes it so the second click can not be received I have tested this and it seem to be very effective.

Which are the ways to set OnClickListener for button in Android?

To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.


2 Answers

The standard way to avoid multiple clicks is to save the last clicked time and avoid the other button clicks within 1 second (or any time span). Example:

// Make your activity class to implement View.OnClickListener public class MenuPricipalScreen extends Activity implements View.OnClickListener{      @Override     protected void onCreate(Bundle savedInstanceState) {         // setup listeners.         findViewById(R.id.imageView2).setOnClickListener(MenuPricipalScreen.this);         findViewById(R.id.imageView3).setOnClickListener(MenuPricipalScreen.this);         ...      }      .     .     .      // variable to track event time     private long mLastClickTime = 0;      // View.OnClickListener.onClick method defination      @Override     public void onClick(View v) {         // Preventing multiple clicks, using threshold of 1 second         if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {             return;         }         mLastClickTime = SystemClock.elapsedRealtime();          // Handle button clicks         if (v == R.id.imageView2) {             // Do your stuff.         } else if (v == R.id.imageView3) {             // Do your stuff.         }         ...     }      .     .     .   } 
like image 157
Shivanand Darur Avatar answered Sep 16 '22 16:09

Shivanand Darur


you can disable the multi-touch on your app by using this android:splitMotionEvents="false" and android:windowEnableSplitTouch="false" in your theme.

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">     ...     <item name="android:splitMotionEvents">false</item>     <item name="android:windowEnableSplitTouch">false</item> </style> 
like image 24
demir Avatar answered Sep 19 '22 16:09

demir