Example usage for android.view KeyEvent ACTION_DOWN

List of usage examples for android.view KeyEvent ACTION_DOWN

Introduction

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

Prototype

int ACTION_DOWN

To view the source code for android.view KeyEvent ACTION_DOWN.

Click Source Link

Document

#getAction value: the key has been pressed down.

Usage

From source file:fr.ippon.android.opendata.android.AbstractParkingListFragment.java

private void configureSearchField(RelativeLayout r) {
    final AbstractParkingListFragment loaderCallBack = this;
    searchField = (EditText) r.findViewById(R.id.searchField);

    if (TextUtils.isEmpty(searchField.getText()))
        searchField.setText(searchHint);

    searchField.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // If the event is a key-down event on the "enter" button
            if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                // hide the keyboard
                InputMethodManager imm = (InputMethodManager) getActivity()
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(searchField.getWindowToken(), 0);

                // Perform action on key press
                boolean okSearch = !TextUtils.isEmpty(searchField.getText())
                        && !searchHint.equals(searchField.getText());
                queryText = okSearch ? searchField.getText().toString() : null;
                Log.d(TAG, "query: " + queryText);
                hasSearched = true;//from  w ww .j a  v a  2s  .com
                getLoaderManager().restartLoader(0, null, loaderCallBack);
                if (queryText == null) {
                    searchField.setText(searchHint);
                }
                return true;
            }
            return false;
        }
    });
    // afichage d'une croix dans l'edit text pour effacer le texte saisie
    final Drawable x = getResources().getDrawable(R.drawable.ic_cancel);
    x.setBounds(0, 0, x.getIntrinsicWidth(), x.getIntrinsicHeight());
    searchField.setCompoundDrawables(null, null, x, null);
    searchField.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN
                    && searchHint.equals(searchField.getText().toString())) {
                searchField.setText("");
            }
            if (searchField.getCompoundDrawables()[2] == null) {
                // cross is not being shown so no need to handle
                return false;
            }
            if (event.getAction() != MotionEvent.ACTION_DOWN) {
                // only respond to the down type
                return false;
            }
            if (event.getX() > searchField.getMeasuredWidth() - searchField.getPaddingRight()
                    - x.getIntrinsicWidth()) {
                searchField.setText("");
                return true;
            } else {
                return false;
            }
        }
    });
}

From source file:com.stoutner.privacybrowser.CreateHomeScreenShortcut.java

@Override
@NonNull/*  ww  w  . j ava  2s .  c  o m*/
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Create a drawable version of the favorite icon.
    Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIcon);

    // Use AlertDialog.Builder to create the AlertDialog
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
    LayoutInflater customDialogInflater = getActivity().getLayoutInflater();
    alertDialogBuilder.setTitle(R.string.shortcut_name);
    alertDialogBuilder.setIcon(favoriteIconDrawable);
    alertDialogBuilder.setView(customDialogInflater.inflate(R.layout.create_home_screen_shortcut_dialog, null));
    alertDialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            buttonListener.onCreateHomeScreenShortcutCancel(CreateHomeScreenShortcut.this);
        }
    });
    alertDialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            buttonListener.onCreateHomeScreenShortcutCreate(CreateHomeScreenShortcut.this);
        }
    });

    // Assign the alertDialogBuilder to an AlertDialog.
    final AlertDialog alertDialog = alertDialogBuilder.create();

    // Show the keyboard when the dialog is displayed on the screen.
    alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

    // We need to show alertDialog before we can setOnKeyListener below.
    alertDialog.show();

    EditText shortcutNameEditText = (EditText) alertDialog.findViewById(R.id.shortcutNameEditText);

    // Allow the "enter" key on the keyboard to create the shortcut.
    shortcutNameEditText.setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // If the event is a key-down event on the "enter" button, select the PositiveButton "Create".
            if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                // Trigger the create listener.
                buttonListener.onCreateHomeScreenShortcutCreate(CreateHomeScreenShortcut.this);

                // Manually dismiss alertDialog.
                alertDialog.dismiss();

                // Consume the event.
                return true;
            } else {
                // If any other key was pressed, do not consume the event.
                return false;
            }
        }
    });

    // onCreateDialog requires the return of an AlertDialog.
    return alertDialog;
}

From source file:io.github.hidroh.materialistic.KeyDelegate.java

/**
 * Calls from {@link Activity#onKeyDown(int, KeyEvent)} to delegate
 * @param keyCode    event key code/*w  ww .j a  v a2  s. co  m*/
 * @param event      key event
 * @return  true if is intercepted as navigation, false otherwise
 */
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
        return mBackInterceptor != null && mBackInterceptor.onBackPressed();
    }
    if (!mEnabled) {
        return false;
    }
    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        event.startTracking();
        return true;
    }
    return false;
}

From source file:com.luke.lukef.lukeapp.NewUserActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_user);
    this.lukeNetUtils = new LukeNetUtils(this);
    this.userNameEditText = (EditText) findViewById(R.id.newUserName);
    this.ageCheckBox = (CheckBox) findViewById(R.id.age_check);
    checkBundle();/*from   www  .  j av  a2s  .c  o m*/
    this.userImageViewCamera = (ImageView) findViewById(R.id.newUserCameraImageView);
    this.userImageViewAuth0 = (ImageView) findViewById(R.id.newUserSocialMediaImageView);
    this.userImageViewDefault = (ImageView) findViewById(R.id.newUserDefaultImageView);
    if (this.isEditing) {
        this.userImageViewDefault.setImageBitmap(SessionSingleton.getInstance().getUserImage());
    }
    ImageButton confirmButton = (ImageButton) findViewById(R.id.newUserConfirmButton);
    confirmButton.setOnClickListener(this);
    this.userNameEditText.setImeActionLabel("Done", KeyEvent.ACTION_DOWN);
    /*Action when pressing enter on keyboard. Since editing ones name is disabled when editing
    and existing profile, this should never be triggered unless a new user is being created*/
    this.userNameEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            endEditing();
            return false;
        }
    });
    this.radioGroupPicture = (RadioGroup) findViewById(R.id.radioGroupPicture);
    this.radioGroupPicture.setOnCheckedChangeListener(this);
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}

From source file:com.android.screenspeak.KeyboardSearchManager.java

@Override
public boolean onKeyEvent(KeyEvent event) {
    // Only handle single-key events here. The KeyComboManager will pass us combos.
    if (event.getModifiers() != 0 || !mNodeSearch.isActive()) {
        return false;
    }//from ww  w. ja  va2s . com

    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_ENTER:
            if (mHasNavigated || mNodeSearch.hasMatch()) {
                finishSearch();
                mContext.getCursorController().clickCurrent();
            } else {
                cancelSearch();
            }
            return true;
        case KeyEvent.KEYCODE_DEL:
            resetHintTime();
            final String queryText = mNodeSearch.getCurrentQuery();
            if (queryText.isEmpty()) {
                cancelSearch();
            } else {
                final String lastChar = queryText.substring(queryText.length() - 1);
                mNodeSearch.backspaceQueryText();
                mSpeechController.speak(mContext.getString(R.string.template_text_removed, lastChar),
                        SpeechController.QUEUE_MODE_FLUSH_ALL, FeedbackItem.FLAG_NO_HISTORY, null);
            }
            return true;
        case KeyEvent.KEYCODE_DPAD_UP:
            moveToEnd(NodeFocusFinder.SEARCH_BACKWARD);
            return true;
        case KeyEvent.KEYCODE_DPAD_LEFT:
            moveToNext(NodeFocusFinder.SEARCH_BACKWARD);
            return true;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            moveToEnd(NodeFocusFinder.SEARCH_FORWARD);
            return true;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            moveToNext(NodeFocusFinder.SEARCH_FORWARD);
            return true;
        case KeyEvent.KEYCODE_SPACE:
            resetHintTime();
            if (mNodeSearch.tryAddQueryText(" ")) {
                mSpeechController.speak(mContext.getString(R.string.symbol_space),
                        SpeechController.QUEUE_MODE_FLUSH_ALL, FeedbackItem.FLAG_NO_HISTORY, null);
            } else {
                mContext.getFeedbackController().playAuditory(R.raw.complete);
            }
            return true;
        default:
            if (event.isPrintingKey()) {
                resetHintTime();
                final String key = String.valueOf(event.getDisplayLabel());
                if (mNodeSearch.tryAddQueryText(key)) {
                    mSpeechController.speak(key.toLowerCase(), SpeechController.QUEUE_MODE_FLUSH_ALL,
                            FeedbackItem.FLAG_NO_HISTORY, null);
                } else {
                    mContext.getFeedbackController().playAuditory(R.raw.complete);
                }
                return true;
            }
            break;
        }
    }

    return false;
}

From source file:org.alfresco.mobile.android.application.fragments.node.comment.CommentsFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    setRetainInstance(false);//ww w.  j  a  v  a  2s.c  o  m
    setRootView(inflater.inflate(R.layout.app_comments, container, false));

    init(getRootView(), R.string.empty_comment);

    commentText = (EditText) viewById(R.id.comment_value);
    bAdd = (ImageButton) viewById(R.id.send_comment);
    bAdd.setEnabled(false);

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

    commentText.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            activateSend();
        }

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

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

    commentText.setImeOptions(EditorInfo.IME_ACTION_SEND);
    commentText.setOnEditorActionListener(new OnEditorActionListener() {

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

    gv.setSelector(android.R.color.transparent);
    gv.setCacheColorHint(getResources().getColor(android.R.color.transparent));

    return getRootView();
}

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 ww w .  ja va 2  s  .  com*/

    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.app.blockydemo.ui.fragment.FormulaEditorScriptListFragment.java

@Override
public void onListItemClick(int position) {
    Log.d("catroid", "onListItemClick");
    if (!inContextMode) {
        FormulaEditorFragment formulaEditor = (FormulaEditorFragment) getActivity().getSupportFragmentManager()
                .findFragmentByTag(FormulaEditorFragment.FORMULA_EDITOR_FRAGMENT_TAG);
        if (formulaEditor != null) {
            formulaEditor.addUserVariableToActiveFormula(adapter.getItem(position).getName());
            formulaEditor.updateButtonViewOnKeyboard();
        }//from   w w w  . j a v a  2 s  . c  om
        KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
        onKey(null, keyEvent.getKeyCode(), keyEvent);
    }

}

From source file:com.ruesga.rview.EditorActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG);
        if (fragment != null && fragment instanceof KeyEventBindable) {
            if (((KeyEventBindable) fragment).onKeyDown(event.getKeyCode(), event)) {
                return true;
            }//w w w.  j a v a2  s.c o  m
        }
    }
    return super.dispatchKeyEvent(event);
}

From source file:com.example.android.leanback.PlaybackTransportControlGlueSample.java

@Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
    if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
        Action action = getControlsRow().getActionForKeyCode(keyEvent.getKeyCode());
        if (shouldDispatchAction(action)) {
            dispatchAction(action);/* w  w  w .  j  a v  a  2s  . c  om*/
            return true;
        }
    }
    return super.onKey(view, keyCode, keyEvent);
}