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,
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.
}
...
}
.
.
.
}
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>
Cheap Solution:
You don’t have a correct separation of concerns (MVP or any flavor) so you put your code in your Activity/Fragment
If you can’t handle this the correct way, at least do not use non-deterministic solutions (like a timer).
Use the tools you already have, say you have this code:
//Somewhere in your onCreate()
Button myButton = findViewById…
myButton.setOnClickListener(this);
// Down below…
@Override
public void onClick(View view) {
if (myButton.isEnabled()) {
myButton.setEnabled(false);
// Now do something like…
startActivity(…);
}
}
Now… in a completely different place in your logic, like… for example, your onCreate or your onResume or anywhere where you know you want your button working again…
myButton.setEnabled(true);
“More Modern” Approach:
- Do the same, but put the logic in your Presenter.
- Your presenter will decide if the “button” action has been triggered.
- Your presenter will tell its “view”:
enableMyButton();
or disableMyButton()
depending.
- Your View will do the right thing.
- you know… basic separation of concerns.
WHY “enabled(true/false)”?
Because it’s built in.
Because the button will respect its state (and if you have a correct state list, it will change appearance for you, and because it will always be what you expect). Also, because it’s easier to test a presenter full of mocks, than a full activity that can grow forever in code.
You can try my tiny library, it provides what exactly you want using same approach as Shivanand' solution.
https://github.com/RexLKW/SClick
A "Better" Practice is to use the onClickListener like this
->
OnClickListener switchableListener = new OnClickListener(@Override
public void onClick(View arg0) {
arg0.setOnClickListener(null);
(or) use the view directly as a final variable in case of errors
ex :
textView.setOnClickListener(null);
// Some processing done here
Completed , enable the onclicklistener arg0.setOnClickListener(switchableListener);
});
This should fix the problem and its quite a simple way of handling things