Touch Events in Fragment

Hi friends, Have a great day. Today we're going to look for touch events in fragment. Taps - If you gonna multiple tap to view or do something, this will be more helpful.

Just implement TouchListener in your fragment like this,

public class FirstFragment extends Fragment implements View.OnTouchListener


Then

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int eventaction = event.getAction();
        if (eventaction == MotionEvent.ACTION_UP) {

            //get system current milliseconds
            long time = System.currentTimeMillis();


            //if it is the first time, or if it has been more than 3 seconds since the first tap ( so it is like a new try), we reset everything
            if (startMillis == 0 || (time - startMillis > 3000)) {
                startMillis = time;
                count = 1;
            }
            //it is not the first, and it has been  less than 3 seconds since the first
            else { //  time-startMillis< 3000
                count++;
            }
            Log.d("count", String.valueOf(count));
            if (count == 5) {
                //do whatever you need
                getActivity().startActivity(new Intent(getActivity(), ErrorLogActivity.class));
            }
            return true;
        }
        return true;
    }

Then simply just mention your view.

view.setOnTouchListener(this);



Comments

Popular posts from this blog

Flutter Bloc - Clean Architecture

What's new in android 14?

Dependencies vs Dev Dependencies in Flutter