Skip to content

Instantly share code, notes, and snippets.

@Pavneet-Sing
Last active September 20, 2016 05:29
Show Gist options
  • Save Pavneet-Sing/e247c31781ee8ffa4996681a45ce1713 to your computer and use it in GitHub Desktop.
Save Pavneet-Sing/e247c31781ee8ffa4996681a45ce1713 to your computer and use it in GitHub Desktop.
Code will trigger an Action when ImageView is tapped three times within Specific time
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sms_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/img_android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
package com.pavneet.myapplication;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.Point;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
/**
* Created by Pavneet Singh on 9/20/2016.
* Email pavneet.edu@gmail.com
*/
public class MainActivity extends AppCompatActivity {
private int i = 0;
private ImageView imageView;
// Handler will keep the track of specific time limit
// it is acting like a timer which will wipe old count if time limit exceeds
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 100) {
i = 0;
}
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.img_android);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (i == 0) {
++i; // first tap recorded , set the counter to 3 sec
handler.sendEmptyMessageDelayed(100, 3000); // 3000 equal 3sec , you can set your own limit of secs
} else if (i == 2) { // third tap recorded so display a single tap and set count to 0
Toast.makeText(MainActivity.this, "Three Touch Clicked", Toast.LENGTH_SHORT).show();
i = 0;
handler.removeMessages(100);
} else // for second tap
++i;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment