Example usage for android.view MotionEvent getAction

List of usage examples for android.view MotionEvent getAction

Introduction

In this page you can find the example usage for android.view MotionEvent getAction.

Prototype

public final int getAction() 

Source Link

Document

Return the kind of action being performed.

Usage

From source file:com.android.hareime.accessibility.AccessibleKeyboardViewProxy.java

/**
 * Handles a hover event on a key. If {@link Key} extended View, this would
 * be analogous to calling View.onHoverEvent(MotionEvent).
 *
 * @param key The currently hovered key.
 * @param event The hover event./*from   w w w .ja v a2  s .  c om*/
 * @return {@code true} if the event was handled.
 */
private boolean onHoverKey(Key key, MotionEvent event) {
    // Null keys can't receive events.
    if (key == null) {
        return false;
    }

    final AccessibilityEntityProvider provider = getAccessibilityNodeProvider();

    switch (event.getAction()) {
    case MotionEvent.ACTION_HOVER_ENTER:
        provider.sendAccessibilityEventForKey(key, AccessibilityEventCompat.TYPE_VIEW_HOVER_ENTER);
        provider.performActionForKey(key, AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS, null);
        break;
    case MotionEvent.ACTION_HOVER_EXIT:
        provider.sendAccessibilityEventForKey(key, AccessibilityEventCompat.TYPE_VIEW_HOVER_EXIT);
        break;
    }

    return true;
}

From source file:com.appsummary.luoxf.myappsummary.recyclerView.fastscroll.Views.FastScroller.java

/**
 * Handles the touch event and determines whether to show the fast scroller (or updates it if
 * it is already showing).//from w  w  w . j  a v  a 2 s . co m
 */
public void handleTouchEvent(MotionEvent ev, int downX, int downY, int lastY) {
    ViewConfiguration config = ViewConfiguration.get(mRecyclerView.getContext());

    int action = ev.getAction();
    int y = (int) ev.getY();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        if (isNearPoint(downX, downY)) {
            mTouchOffset = downY - mThumbPosition.y;
        }
        break;
    case MotionEvent.ACTION_MOVE:
        // Check if we should start scrolling
        if (!mIsDragging && isNearPoint(downX, downY) && Math.abs(y - downY) > config.getScaledTouchSlop()) {
            mRecyclerView.getParent().requestDisallowInterceptTouchEvent(true);
            mIsDragging = true;
            mTouchOffset += (lastY - downY);
            mPopup.animateVisibility(true);
        }
        if (mIsDragging) {
            // Update the fastscroller section name at this touch position
            int top = 0;
            int bottom = mRecyclerView.getHeight() - mThumbHeight;
            float boundedY = (float) Math.max(top, Math.min(bottom, y - mTouchOffset));
            String sectionName = mRecyclerView.scrollToPositionAtProgress((boundedY - top) / (bottom - top));
            mPopup.setSectionName(sectionName);
            mPopup.animateVisibility(!sectionName.isEmpty());
            mRecyclerView.invalidate(mPopup.updateFastScrollerBounds(mRecyclerView, mThumbPosition.y));
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        mTouchOffset = 0;
        if (mIsDragging) {
            mIsDragging = false;
            mPopup.animateVisibility(false);
        }
        break;
    }
}

From source file:android.support.designox.widget.HeaderBehavior.java

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent ev) {
    if (mTouchSlop < 0) {
        mTouchSlop = ViewConfiguration.get(parent.getContext()).getScaledTouchSlop();
    }//from w w w. j ava2s  .  co  m

    final int action = ev.getAction();

    // Shortcut since we're being dragged
    if (action == MotionEvent.ACTION_MOVE && mIsBeingDragged) {
        return true;
    }

    switch (MotionEventCompat.getActionMasked(ev)) {
    case MotionEvent.ACTION_DOWN: {
        mIsBeingDragged = false;
        final int x = (int) ev.getX();
        final int y = (int) ev.getY();
        if (canDragView(child) && parent.isPointInChildBounds(child, x, y)) {
            mLastMotionY = y;
            mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
            ensureVelocityTracker();
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final int activePointerId = mActivePointerId;
        if (activePointerId == INVALID_POINTER) {
            // If we don't have a valid id, the touch down wasn't on content.
            break;
        }
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
        if (pointerIndex == -1) {
            break;
        }

        final int y = (int) MotionEventCompat.getY(ev, pointerIndex);
        final int yDiff = Math.abs(y - mLastMotionY);
        if (yDiff > mTouchSlop) {
            mIsBeingDragged = true;
            mLastMotionY = y;
        }
        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP: {
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        break;
    }
    }

    if (mVelocityTracker != null) {
        mVelocityTracker.addMovement(ev);
    }

    return mIsBeingDragged;
}

From source file:android.support.design.widget.HeaderBehavior.java

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent ev) {
    if (mTouchSlop < 0) {
        mTouchSlop = ViewConfiguration.get(parent.getContext()).getScaledTouchSlop();
    }//from  w w  w. j a  v a2  s  .  c o m

    final int action = ev.getAction();

    // Shortcut since we're being dragged
    if (action == MotionEvent.ACTION_MOVE && mIsBeingDragged) {
        return true;
    }

    switch (MotionEventCompat.getActionMasked(ev)) {
    case MotionEvent.ACTION_DOWN: {
        mIsBeingDragged = false;
        final int x = (int) ev.getX();
        final int y = (int) ev.getY();
        if (canDragView(child) && parent.isPointInChildBounds(child, x, y)) {
            mLastMotionY = y;
            mActivePointerId = ev.getPointerId(0);
            ensureVelocityTracker();
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final int activePointerId = mActivePointerId;
        if (activePointerId == INVALID_POINTER) {
            // If we don't have a valid id, the touch down wasn't on content.
            break;
        }
        final int pointerIndex = ev.findPointerIndex(activePointerId);
        if (pointerIndex == -1) {
            break;
        }

        final int y = (int) ev.getY(pointerIndex);
        final int yDiff = Math.abs(y - mLastMotionY);
        if (yDiff > mTouchSlop) {
            mIsBeingDragged = true;
            mLastMotionY = y;
        }
        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP: {
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        break;
    }
    }

    if (mVelocityTracker != null) {
        mVelocityTracker.addMovement(ev);
    }

    return mIsBeingDragged;
}

From source file:ca.shoaib.ping.PingListFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_ping_list, container, false);

    mListView = (ListView) rootView.findViewById(R.id.list_ping);

    pingListAdapter = new PingListAdapter(getActivity(), R.layout.ping_row, pingList);
    mListView.setAdapter(pingListAdapter);
    //mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override/* w  w w .jav  a  2 s.c  om*/
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mPingListCallback.onPingSelected(pingList.get(position));
            mActivatedPosition = position;
            setActivatedPosition(position);
            //Log.d(TAG, "ArtistId: " + artistList.get(position).getArtistId());
        }
    });

    pb = (ProgressBar) rootView.findViewById(R.id.progress_bar);
    et = (EditText) rootView.findViewById(R.id.ping_destination);
    tv = (TextView) rootView.findViewById(R.id.ping_result);
    btn = (Button) rootView.findViewById(R.id.ping_start);

    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            String stringUrl = et.getText().toString();

            ConnectivityManager connMgr = (ConnectivityManager) getActivity()
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

            if (networkInfo != null && networkInfo.isConnected()) {
                AsyncTask pingTask = new PingTask(getActivity(), pingList, pingListAdapter).execute(stringUrl);

            } else {
                tv.setTextSize(20);
                tv.setTextColor(Color.RED);
                tv.setText(R.string.no_internet);
            }

            InputMethodManager inputManager = (InputMethodManager) getActivity()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);

            inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
                    InputMethodManager.RESULT_UNCHANGED_SHOWN);
        }
    });

    et.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            final int DRAWABLE_LEFT = 0;
            final int DRAWABLE_TOP = 1;
            final int DRAWABLE_RIGHT = 2;
            final int DRAWABLE_BOTTOM = 3;

            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                if (event.getRawX() >= (et.getRight()
                        - et.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                    et.setText("");
                    et.selectAll();

                    return true;
                }
            }
            return false;
        }
    });
    return rootView;
}

From source file:geeshang.nasaimage.MainActivity.java

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        v.setAlpha(0);/*from  w  w w .  ja  v  a 2  s  . c o  m*/
    }
    if (event.getAction() == MotionEvent.ACTION_UP) {
        v.setAlpha(255);
    }
    return false;
}

From source file:com.beyondar.example.CameraWithTouchEventsActivity.java

@Override
public void onTouchBeyondarView(MotionEvent event, BeyondarGLSurfaceView beyondarView) {

    float x = event.getX();
    float y = event.getY();

    ArrayList<BeyondarObject> geoObjects = new ArrayList<BeyondarObject>();

    // This method call is better to don't do it in the UI thread!
    beyondarView.getBeyondarObjectsOnScreenCoordinates(x, y, geoObjects);

    String textEvent = "";

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        textEvent = "Event type ACTION_DOWN: ";
        break;//  ww w. ja v  a2s. c om
    case MotionEvent.ACTION_UP:
        textEvent = "Event type ACTION_UP: ";
        break;
    case MotionEvent.ACTION_MOVE:
        textEvent = "Event type ACTION_MOVE: ";
        break;
    default:
        break;
    }

    Iterator<BeyondarObject> iterator = geoObjects.iterator();
    while (iterator.hasNext()) {
        BeyondarObject geoObject = iterator.next();
        textEvent = textEvent + " " + geoObject.getName();

    }
    mLabelText.setText("Event: " + textEvent);
}

From source file:com.chess.genesis.activity.GameListOnlineFrag.java

@Override
public boolean onTouch(final View v, final MotionEvent event) {
    if (v.getId() == R.id.game_search)
        v.setBackgroundColor(//from w ww.  j av  a2s. c  o  m
                (event.getAction() == MotionEvent.ACTION_DOWN) ? MColors.BLUE_NEON : MColors.CLEAR);
    return false;
}

From source file:co.taqat.call.CallIncomingActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getResources().getBoolean(R.bool.orientation_portrait_only)) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }/*  w w  w .  ja v a  2s  . c om*/

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.call_incoming);

    name = (TextView) findViewById(R.id.contact_name);
    number = (TextView) findViewById(R.id.contact_number);
    contactPicture = (ImageView) findViewById(R.id.contact_picture);

    // set this flag so this activity will stay in front of the keyguard
    int flags = WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
    getWindow().addFlags(flags);

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

    final int screenWidth = getResources().getDisplayMetrics().widthPixels;

    acceptUnlock = (LinearLayout) findViewById(R.id.acceptUnlock);
    declineUnlock = (LinearLayout) findViewById(R.id.declineUnlock);

    accept = (ImageView) findViewById(R.id.accept);
    decline = (ImageView) findViewById(R.id.decline);
    accept.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            decline.setVisibility(View.GONE);
            acceptUnlock.setVisibility(View.VISIBLE);

        }
    });

    accept.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            float curX;
            switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                acceptUnlock.setVisibility(View.VISIBLE);
                decline.setVisibility(View.GONE);
                answerX = motionEvent.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                curX = motionEvent.getX();
                if ((answerX - curX) >= 0)
                    view.scrollBy((int) (answerX - curX), view.getScrollY());
                answerX = curX;
                if (curX < screenWidth / 4) {
                    answer();
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:
                view.scrollTo(0, view.getScrollY());
                decline.setVisibility(View.VISIBLE);
                acceptUnlock.setVisibility(View.GONE);
                break;
            }
            return true;
        }
    });

    decline.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            float curX;
            switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                declineUnlock.setVisibility(View.VISIBLE);
                accept.setVisibility(View.GONE);
                declineX = motionEvent.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                curX = motionEvent.getX();
                view.scrollBy((int) (declineX - curX), view.getScrollY());
                declineX = curX;
                Log.w(curX);
                if (curX > (screenWidth / 2)) {
                    decline();
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:
                view.scrollTo(0, view.getScrollY());
                accept.setVisibility(View.VISIBLE);
                declineUnlock.setVisibility(View.GONE);
                break;

            }
            return true;
        }
    });

    decline.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            accept.setVisibility(View.GONE);
            acceptUnlock.setVisibility(View.VISIBLE);
        }
    });

    mListener = new LinphoneCoreListenerBase() {
        @Override
        public void callState(LinphoneCore lc, LinphoneCall call, State state, String message) {
            if (call == mCall && State.CallEnd == state) {
                finish();
            }
            if (state == State.StreamsRunning) {
                // The following should not be needed except some devices need it (e.g. Galaxy S).
                LinphoneManager.getLc().enableSpeaker(LinphoneManager.getLc().isSpeakerEnabled());
            }
        }
    };

    super.onCreate(savedInstanceState);
    instance = this;
}

From source file:com.echlabsw.android.drawertab.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(TAG, "onCreate");

    setContentView(R.layout.activity_main);

    createStubTab();//  www.  j  a va2 s.c  om

    mActionBar = getActionBar();

    mHorizontalTabScrollView = (HorizontalScrollView) findViewById(R.id.hscroll_tab_host);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerListView = (ListView) findViewById(R.id.left_drawer);
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

    mActionBar.setHomeButtonEnabled(true);
    mActionBar.setDisplayHomeAsUpEnabled(true);

    mDrawerToggle = createDrawerToggleListener();

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup();

    for (String tag : mTabMap.keySet()) {
        Tab tab = mTabMap.get(tag);
        TabHost.TabSpec spec = mTabHost.newTabSpec(tag);
        spec.setContent(this);
        spec.setIndicator(tab.title);
        mTabHost.addTab(spec);
    }

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager(), mTabMap.size());
    mViewPager.setAdapter(mAdapter);

    mTabHost.setOnTabChangedListener(this);
    mViewPager.setOnPageChangeListener(this);

    if (!TAB_SCROLL_ENABLED) {
        mHorizontalTabScrollView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    break;
                case MotionEvent.ACTION_UP:
                    v.performClick();
                    break;
                }
                return true;
            }
        });
    }

    mArrayNavigationDrawerAdapter = new ArrayAdapter<Tab>(this, android.R.layout.simple_list_item_1,
            mTabMap.values().toArray(new Tab[0]));
    mDrawerListView.setAdapter(mArrayNavigationDrawerAdapter);
    mDrawerListView.setOnItemClickListener(this);

    /* Create AsyncTaskLoader */
    getSupportLoaderManager().initLoader(0, null, this);
}