Reputation: 2326
Mockup of my Application :
Problem :
When click on button1 it just call Intent of ActivitySecond
button1.setOnClickListener(this);
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId())
{
case R.id.button1:
Intent intent = new Intent(getApplicationContext(), ActivitySecond.class);
startActivity(intent);
break;
default:
break;
}
}
But, on Double tap it open twice ActivitySecond.
HOW TO RESOLVE IT.
PLEASE IF ANY SOLUTION THEN SHARE IT.
Thank you.
Upvotes: 11
Views: 18561
Reputation: 7577
I will just submit a third solution for this. I solved it by adding a boolean which was used to check if the activity (which the button starts) has been started or not.
Upvotes: 0
Reputation: 1803
When I use selector drawable in Buttons and set
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
it performs the onClick() event on double click. I found it accidentally to be working on android emulator api level 10, Android 2.3.3 Didn't tested on real device. Here is the Complete code.
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="Discover"
android:id="@+id/Button1"
android:layout_weight=".5"
android:layout_margin="0dp"
android:background="@drawable/btn_nearby"
android:contentDescription="gjhfjhkjhgkvkjh"
android:drawableLeft="@drawable/ic_follow"
android:paddingLeft="20dp"
android:paddingRight="0dp"
android:drawablePadding="-10dp"
android:textSize="16sp"
android:paddingTop="2.5dp"
android:paddingBottom="2.5dp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"/>
And Java Code
@Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.Button1:
onButton1Click();
break;
case R.id.Button2:
onButton2Click();
break;
}
}
Upvotes: -1
Reputation: 1277
As Gabe Sechan sad:
This can be done via timer (get the time they click on it, save it, and if they click it again within say 100ms ignore the 2nd click)
Here is an implementation that i used in my project:
public abstract class OnOneClickListener implements View.OnClickListener {
private static final long MIN_CLICK_INTERVAL = 1000; //in millis
private long lastClickTime = 0;
@Override
public final void onClick(View v) {
long currentTime = SystemClock.elapsedRealtime();
if (currentTime - lastClickTime > MIN_CLICK_INTERVAL) {
lastClickTime = currentTime;
onOneClick(v);
}
}
public abstract void onOneClick(View v);
}
Just use OnOneClickListener
instead of OnClickListener
and execute your code in onOneClick()
method.
The solution with disabling button in onClick()
will not work. Two clicks on a button can be scheduled for execution even before your first onClick()
will execute and disable the button.
Upvotes: 12
Reputation: 2223
btn.setOnclickListener(new View.onClickListener(){
public void onClick(View v) {
btn.setEnabled(false);
}
});
you have to make the setEnabled(false) in onlclick event.
Upvotes: 3
Reputation: 12819
You can set launchMode of ActivitySecond
to singleTop
<activity android:name=".ActivitySecond"
android:launchMode="singleTop"
>
...
</activity>
Upvotes: 6
Reputation: 93688
This is called debouncing- its a classical problem in hardware and in software. There's a couple of tricks you can do, but they all boil down to disabling the button temporarily and re-enabling it later. This can be done via timer (get the time they click on it, save it, and if they click it again within say 100ms ignore the 2nd click). Another way would be to disable the button after onClick and re-enable it when the new Activity finishes via onActivityResult. Or there's a dozen other ways, pick the easiest for you.
Upvotes: 9