Example usage for android.view KeyEvent getAction

List of usage examples for android.view KeyEvent getAction

Introduction

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

Prototype

public final int getAction() 

Source Link

Document

Retrieve the action of this key event.

Usage

From source file:org.alfresco.mobile.android.application.fragments.site.search.SearchSitesFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (container == null) {
        return null;
    }//from w  ww. j  a  v  a2s  .c  o m
    setRootView(inflater.inflate(R.layout.fr_site_search, container, false));
    init(getRootView(), emptyListMessageId);

    searchText = (MaterialAutoCompleteTextView) viewById(R.id.search_query);
    searchAdapter = new HistorySearchInlineCursorAdapter(this, null, R.layout.row_two_lines_search);
    getLoaderManager().initLoader(0, null, this);
    searchText.setAdapter(searchAdapter);
    searchText.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TwoLinesViewHolder vh = (TwoLinesViewHolder) view.getTag();
            HistorySearch tmphistorySearchItem = HistorySearchManager.retrieveHistorySearch(getActivity(),
                    (Long) vh.choose.getTag());
            searchText.setText(tmphistorySearchItem.getQuery());
            historySearchItem = tmphistorySearchItem;
            search();
        }
    });

    bAdd = (ImageButton) viewById(R.id.search_action);
    bAdd.setEnabled(false);
    activateSend();

    bAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            search();
        }
    });

    searchText.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            historySearchItem = null;
            activateSend();
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
    });

    searchText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
    searchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (event != null && (event.getAction() == KeyEvent.ACTION_DOWN)
                    && ((actionId == EditorInfo.IME_ACTION_SEARCH)
                            || (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))) {
                search();
                return true;
            }
            return false;
        }
    });

    return getRootView();
}

From source file:ar.rulosoft.custompref.SeekBarCustomPreference.java

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (isEnabled()) {
        if (event.getAction() != KeyEvent.ACTION_UP) {
            int increment = mKeyProgressIncrement;

            switch (keyCode) {
            case KeyEvent.KEYCODE_PLUS:
            case KeyEvent.KEYCODE_EQUALS:
                setProgress(getProgress() + increment);
                return true;
            case KeyEvent.KEYCODE_MINUS:
                setProgress(getProgress() - increment);
                return true;
            case KeyEvent.KEYCODE_DPAD_LEFT:
                increment = -increment;/*from   w  w  w .j  a  va 2  s .c o m*/
                // fallthrough
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                increment = ViewCompat.getLayoutDirection(v) == ViewCompat.LAYOUT_DIRECTION_RTL ? -increment
                        : increment;
                setProgress(getProgress() + increment);
                return true;
            }
        }
    }
    return false;
}

From source file:org.mklab.mikity.android.SettingsFragment.java

/**
 * {@inheritDoc}//  w ww  .j a  v  a2s.com
 */
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
        final InputMethodManager inputMethodManager = (InputMethodManager) getActivity()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
        saveEnvironment();
        return true;
    }

    return false;
}

From source file:io.github.calvinmikael.anymanga.WebViewFragment.java

@SuppressLint("SetJavaScriptEnabled")
@Override/*from   ww w.  ja  v a 2 s  .  c  o m*/
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_web_view, container, false);
    mProgressBar = (ProgressBar) view.findViewById(R.id.progressBar);
    mProgressBar.setVisibility(View.GONE);

    mWebView = (ObservableWebView) view.findViewById(R.id.webView);
    mWebView.setScrollViewCallbacks(this);
    WebSettings settings = mWebView.getSettings();

    settings.setLoadsImagesAutomatically(true);
    settings.setJavaScriptEnabled(true);
    settings.setDomStorageEnabled(true);
    mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    settings.setBuiltInZoomControls(true);
    settings.setDisplayZoomControls(false);
    mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            if (!mProgressBar.isShown()) {
                mProgressBar.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (mProgressBar.isShown()) {
                mProgressBar.setVisibility(View.GONE);
            }
        }
    });
    mWebView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            // if a view already exists then immediately terminate the new one
            if (mCustomView != null) {
                onHideCustomView();
                return;
            }

            // Save the current state
            mCustomView = view;
            mOriginalSystemUiVisibility = getActivity().getWindow().getDecorView().getSystemUiVisibility();

            // Save the custom view callback
            mCustomViewCallback = callback;

            // Add the custom view to the view hierarchy
            FrameLayout decorView = (FrameLayout) getActivity().getWindow().getDecorView();
            decorView.addView(mCustomView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));

            mTabStrip = (PagerSlidingTabStrip) getActivity().findViewById(R.id.tabs);
            mTabStrip.setVisibility(View.GONE);

            // Go fullscreen
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                getActivity().getWindow().getDecorView()
                        .setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
            } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT
                    && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getActivity().getWindow().getDecorView()
                        .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                                | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
            }
        }

        @Override
        public void onHideCustomView() {
            // Remove the custom view
            FrameLayout decorView = (FrameLayout) getActivity().getWindow().getDecorView();
            decorView.removeView(mCustomView);
            mCustomView = null;

            mTabStrip.setVisibility(View.VISIBLE);
            // Restore the original form
            getActivity().getWindow().getDecorView().setSystemUiVisibility(mOriginalSystemUiVisibility);

            // Call the custom view callback
            mCustomViewCallback.onCustomViewHidden();
            mCustomViewCallback = null;
        }
    });
    // The back button must be handled within the mWebView for the
    // mWebView to have back behavior based on the current mPage
    // if back behavior is not handled with this listener then
    // back behavior will be entirely dependent on the first mPage
    mWebView.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
            if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()
                    && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
                mWebView.goBack();
                return true;
            }

            return false;
        }
    });

    if (savedInstanceState != null) {
        mWebView.restoreState(savedInstanceState);
    } else {
        if (mPage == 1) {
            mWebView.loadUrl(getString(R.string.website_kissmanga));
        } else if (mPage == 2) {
            mWebView.loadUrl(getString(R.string.website_mangapark));
        } else if (mPage == 3) {
            mWebView.loadUrl(getString(R.string.website_line_webtoon));
        }
    }

    return view;
}

From source file:com.kaku.weac.activities.MainActivity.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            ToastUtil.showShortToast(this, getString(R.string.press_again_exit));
            mExitTime = System.currentTimeMillis();
        } else {/*from  ww w.  ja v  a2 s. c  o  m*/
            MobclickAgent.onKillProcess(this);
            finish();
            System.exit(0);
        }
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

From source file:com.cianmcgovern.android.ShopAndShare.Share.java

@Override
public void onCreate(Bundle savedInstanceState) {

    mContext = this;
    super.onCreate(savedInstanceState);

    setContentView(R.layout.share);//from w  ww. ja v a2  s .  c om

    Button upload = (Button) findViewById(R.id.uploadButton);
    upload.setText(R.string.upload);
    upload.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            String userLoc = mLocationEdit.getText().toString();
            String userStore = mStore.getText().toString();
            // Don't allow empty text fields
            if (userLoc.length() > 1 && userStore.length() > 1) {
                runUpload(Results.getInstance(), userLoc, userStore);
            } else
                new AlertDialog.Builder(mContext).setTitle(R.string.invalidInput)
                        .setMessage(R.string.invalidFields).show();
        }

    });

    Button cancel = (Button) findViewById(R.id.cancelShareButton);
    cancel.setText(R.string.cancelButton);
    cancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (mLocationListener != null)
                mLocationManager.removeUpdates(mLocationListener);
            finish();
        }

    });

    mLocationEdit = (EditText) findViewById(R.id.enterLocation);
    mLocationEdit.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((event.getAction()) == KeyEvent.ACTION_DOWN && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                in.hideSoftInputFromWindow(mLocationEdit.getApplicationWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
                return true;
            }
            return false;
        }

    });
    mStore = (EditText) findViewById(R.id.enterStore);
    mStore.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((event.getAction()) == KeyEvent.ACTION_DOWN && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                in.hideSoftInputFromWindow(mStore.getApplicationWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
                return true;
            }
            return false;
        }

    });

    mSearch = (Button) findViewById(R.id.locationButton);
    mSearch.setBackgroundResource(R.drawable.search);
    mSearch.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
            if (!CheckFeatures.haveGPS()) {
                new AlertDialog.Builder(mContext).setTitle("GPS Required")
                        .setMessage("You must have GPS to use this feature").show();
            }
            // Only use GPS if it is enabled
            else if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                mSearch.setEnabled(false);
                mSearch.setBackgroundResource(R.drawable.world);
                getLocation();
            } else {
                mGpsDialog = new AlertDialog.Builder(mContext).create();
                mGpsDialog.setTitle(R.string.gpsDisabled);
                mGpsDialog.setMessage(mContext.getText(R.string.gpsDisabledMessage));
                mGpsDialog.show();
            }
        }

    });
}

From source file:com.androzic.MapList.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.list_with_empty_view_and_progressbar, container, false);

    progressBar = (ProgressBar) view.findViewById(R.id.progressbar);

    view.setFocusableInTouchMode(true);/*w w w.  j ava  2 s  . co m*/
    view.requestFocus();
    view.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
                if (currentTree != mapsTree) {
                    currentTree = currentTree.parent;
                    adapter.notifyDataSetChanged();
                    return true;
                }
            }
            return false;
        }
    });

    return view;
}

From source file:com.android2.calculator3.EventListener.java

@Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
    int action = keyEvent.getAction();

    // Work-around for spurious key event from IME, bug #1639445
    if (action == KeyEvent.ACTION_MULTIPLE && keyCode == KeyEvent.KEYCODE_UNKNOWN) {
        return true; // eat it
    }/*from w ww.j a va2 s  .co m*/

    if (keyEvent.getUnicodeChar() == '=') {
        if (action == KeyEvent.ACTION_UP) {
            mHandler.onEnter();
        }
        return true;
    }

    if (keyCode != KeyEvent.KEYCODE_DPAD_CENTER && keyCode != KeyEvent.KEYCODE_DPAD_UP
            && keyCode != KeyEvent.KEYCODE_DPAD_DOWN && keyCode != KeyEvent.KEYCODE_ENTER) {
        if (keyEvent.isPrintingKey() && action == KeyEvent.ACTION_UP) {
            // Tell the handler that text was updated.
            mHandler.onTextChanged();
        }
        return false;
    }

    /*
     * We should act on KeyEvent.ACTION_DOWN, but strangely sometimes the DOWN event isn't received, only the UP. So the workaround is to act on UP... http://b/issue?id=1022478
     */

    if (action == KeyEvent.ACTION_UP) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_ENTER:
        case KeyEvent.KEYCODE_DPAD_CENTER:
            mHandler.onEnter();
            break;

        case KeyEvent.KEYCODE_DPAD_UP:
            mHandler.onUp();
            break;

        case KeyEvent.KEYCODE_DPAD_DOWN:
            mHandler.onDown();
            break;
        }
    }
    return true;
}

From source file:org.dmfs.tasks.InputTextDialogFragment.java

@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
    // if the user clicks the back button
    if ((keyCode == android.view.KeyEvent.KEYCODE_BACK)) {
        // filter only the touch down event
        if (event.getAction() != KeyEvent.ACTION_DOWN) {
            // we assume that he don't want to save his input
            handleCancel();/*  w ww . j a  va  2 s.com*/
        } else {
            return false;
        }
    }
    return false;
}

From source file:com.rks.musicx.services.MediaButtonReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    String intentAction = intent.getAction();
    String command = null;//  www .j av a2 s .co  m
    if (intent.getAction() != null) {
        if (intentAction.equals(Intent.ACTION_MEDIA_BUTTON)) {
            KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

            if (event == null) {
                return;
            }

            int keycode = event.getKeyCode();
            final int action = event.getAction();
            final long eventTime = event.getEventTime();

            switch (keycode) {
            case KeyEvent.KEYCODE_MEDIA_STOP:
                Log.d(TAG, "stop");
                command = Constants.ACTION_STOP;
                break;
            case KeyEvent.KEYCODE_HEADSETHOOK:
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                Log.d(TAG, "toggle");
                command = Constants.ACTION_TOGGLE;
                break;
            case KeyEvent.KEYCODE_MEDIA_NEXT:
                Log.d(TAG, "next");
                command = Constants.ACTION_NEXT;
                break;
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                Log.d(TAG, "prev");
                command = Constants.ACTION_PREVIOUS;
                break;
            case KeyEvent.KEYCODE_MEDIA_PAUSE:
                Log.d(TAG, "pause");
                command = Constants.ACTION_PAUSE;
                break;
            case KeyEvent.KEYCODE_MEDIA_PLAY:
                Log.d(TAG, "play");
                command = Constants.ACTION_PLAY;
                break;
            }
            // startServices(context, command);
            if (command != null) {
                if (action == KeyEvent.ACTION_DOWN) {
                    if (event.getRepeatCount() == 0) {
                        if (keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
                            if (eventTime - mLastClickTime >= DOUBLE_CLICK) {
                                mClickCounter = 0;
                            }

                            mClickCounter++;
                            Log.e("MediaButton", "Got headset click, count = " + mClickCounter);
                            mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT);

                            Message msg = mHandler.obtainMessage(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT,
                                    mClickCounter, 0, context);

                            long delay = mClickCounter < 3 ? DOUBLE_CLICK : 0;
                            if (mClickCounter >= 3) {
                                mClickCounter = 0;
                            }
                            mLastClickTime = eventTime;
                            acquireWakeLockAndSendMessage(context, msg, delay);
                        } else {
                            startServices(context, command);
                        }
                    }
                }
            }
        }
    }
}