-2

how can i set double tap to like a image in android.(like instagram photos)

I have following code here:

    final ImageView photo_show = (ImageView) findViewById(R.id.imgv_photo);// this is main image
    final ImageView imgv_like_photo = (ImageView) findViewById(R.id.imgv__like_photo);// this is heart image
    imgv_musics_activity_like_photo.setVisibility(View.GONE);

    photo_show.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            photo_show.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    like(imgv_musics_activity_like_photo);
                }
            });
        }
    });

public void like(View view) {
    TranslateAnimation animate = new TranslateAnimation(0, view.getWidth(), 0, 0);
    animate.setDuration(500);
    animate.setFillAfter(true);
    view.startAnimation(animate);
    view.setVisibility(View.VISIBLE);
}

And xml :

            <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center">

            <ImageView
                android:id="@+id/imgv_photo"
                android:layout_width="250dp"
                android:layout_height="250dp"
                android:layout_centerHorizontal="true"
                android:contentDescription="@string/image_photo"
                tools:src="@drawable/no_photo" />


            <ImageView
                android:id="@+id/imgv_like_photo"
                android:layout_width="250dp"
                android:layout_height="250dp"
                android:layout_centerHorizontal="true"
                android:contentDescription="@string/image_photo"
                android:src="@drawable/ic_like" />


        </RelativeLayout>

this codes just show heart on image when double taps. i want to hide after 2 sec for example. Can you guys give me a way??

emen
  • 170
  • 2
  • 19
  • 1
    this link may help you https://stackoverflow.com/questions/5191456/how-can-i-place-double-click-event-on-imageview-in-android – AskNilesh Aug 28 '17 at 08:16

2 Answers2

2

For the detection of double tap you should use a GestureDetector. See this SO question, which is a similar case as yours.

About hiding, you can add a second animation with a start delay of two seconds.

Xavier Rubio Jansana
  • 6,388
  • 1
  • 27
  • 50
2

declare global variable:

boolean isDoubleTap = false;
Handler mHandler = new Handler(Looper.getMainLooper());

Within onClick you can create a Runnable like:

@Override
public void onClick(View v) {
    photo_show.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Runnable doubleTapRunnable = new Runnable() {
                @Override
                public void run() {
                    isDoubleTap = false;
                }
            };
            if (isDoubleTap) {
                //your logic for double click action
                like(imgv_musics_activity_like_photo);
                isDoubleTap = false;
            } else {
                isDoubleTap=true;
                mHandler.postDelayed(doubleTapRunnable, 500); // you can adjust delay in oder to check double tap
            }
        }
    });
}

Note: you can also use OnDoubleTapListener.

Anshuman Jaiswal
  • 5,352
  • 1
  • 29
  • 46