Example usage for android.view KeyEvent getKeyCode

List of usage examples for android.view KeyEvent getKeyCode

Introduction

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

Prototype

public final int getKeyCode() 

Source Link

Document

Retrieve the key code of the key event.

Usage

From source file:com.concavenp.artistrymuse.fragments.SearchFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View mainView = inflater.inflate(R.layout.fragment_search, container, false);

    // The search text the user will input
    mSearchEditText = mainView.findViewById(R.id.search_editText);
    mSearchEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() {

        @Override// www  . j  a v  a2 s . c o m
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

            if ((actionId == EditorInfo.IME_ACTION_SEARCH) || (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {

                performSearch();

                return true;

            }

            return false;
        }

    });
    mSearchEditText.setOnTouchListener(new View.OnTouchListener() {

        @SuppressWarnings("unused")
        @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_UP) {

                // Check the layout direction to support LTR or RTL
                if (ViewCompat.getLayoutDirection(mSearchEditText) == ViewCompat.LAYOUT_DIRECTION_LTR) {
                    if (event.getRawX() >= (mSearchEditText.getRight()
                            - mSearchEditText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {

                        performSearch();

                        return true;
                    }
                } else {
                    if (event.getRawX() <= (mSearchEditText.getLeft()
                            + mSearchEditText.getCompoundDrawables()[DRAWABLE_LEFT].getBounds().width())) {

                        performSearch();

                        return true;
                    }
                }

            }

            return false;
        }

    });

    // Save off the flipper for use in deciding which view to show
    mFlipper = mainView.findViewById(R.id.fragment_search_ViewFlipper);

    // Get the ViewPager and set it's PagerAdapter so that it can display items
    ViewPager mViewPager = mainView.findViewById(R.id.search_results_viewpager);
    mSearchPagerAdapter = new SearchFragmentPagerAdapter(this, getChildFragmentManager());
    mViewPager.setAdapter(mSearchPagerAdapter);
    mViewPager.setOffscreenPageLimit(mSearchPagerAdapter.getCount());

    // Give the TabLayout the ViewPager
    TabLayout mTabLayout = mainView.findViewById(R.id.search_tabs);
    mTabLayout.setupWithViewPager(mViewPager);

    mFlipper.setDisplayedChild(
            mFlipper.indexOfChild(mFlipper.findViewById(R.id.fragment_search_no_search_Flipper)));

    return mainView;

}

From source file:net.olejon.mdapp.Icd10Activity.java

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

    // Input manager
    mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

    // Layout/*  ww  w  .  j  a  v a2s .c  o  m*/
    setContentView(R.layout.activity_icd10);

    // Toolbar
    final Toolbar toolbar = (Toolbar) findViewById(R.id.icd10_toolbar);
    toolbar.setTitle(getString(R.string.icd10_title));

    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mToolbarSearchLayout = (LinearLayout) findViewById(R.id.icd10_toolbar_search_layout);
    mToolbarSearchEditText = (EditText) findViewById(R.id.icd10_toolbar_search);

    mToolbarSearchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
            if (i == EditorInfo.IME_ACTION_SEARCH || keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                mInputMethodManager.hideSoftInputFromWindow(mToolbarSearchEditText.getWindowToken(), 0);

                search(mToolbarSearchEditText.getText().toString().trim());

                return true;
            }

            return false;
        }
    });

    ImageButton imageButton = (ImageButton) findViewById(R.id.icd10_toolbar_clear_search);

    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mToolbarSearchEditText.setText("");
        }
    });

    // List
    mListView = (ListView) findViewById(R.id.icd10_list);

    // Floating action button
    mFloatingActionButton = (FloatingActionButton) findViewById(R.id.icd10_fab);

    mFloatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mToolbarSearchLayout.getVisibility() == View.VISIBLE) {
                mInputMethodManager.hideSoftInputFromWindow(mToolbarSearchEditText.getWindowToken(), 0);

                search(mToolbarSearchEditText.getText().toString().trim());
            } else {
                mToolbarSearchLayout.setVisibility(View.VISIBLE);
                mToolbarSearchEditText.requestFocus();

                mInputMethodManager.showSoftInput(mToolbarSearchEditText, 0);
            }
        }
    });

    // Get chapters
    GetChaptersTask getChaptersTask = new GetChaptersTask();
    getChaptersTask.execute();
}

From source file:hku.fyp14017.blencode.ui.fragment.FormulaEditorListFragment.java

@Override
public void onListItemClick(ListView listView, View view, int position, long id) {
    if (position >= itemsIds.length) {
        SensorTagFragment sensorTagFragment;
        FragmentManager fm = getSherlockActivity().getSupportFragmentManager();
        sensorTagFragment = (SensorTagFragment) fm.findFragmentByTag(SensorTagFragment.SENSOR_TAG_FRAGMENT_TAG);
        if (sensorTagFragment == null) {
            sensorTagFragment = new SensorTagFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("Tag number", position - (itemsIds.length - 1));
            sensorTagFragment.setArguments(bundle);
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(hku.fyp14017.blencode.R.id.script_fragment_container, sensorTagFragment,
                    SensorTagFragment.SENSOR_TAG_FRAGMENT_TAG);
            ft.hide(this);
            ft.commit();/*from ww  w. j  a  v  a 2 s  . c o  m*/
        } else {
            /*Bundle bundle = new Bundle();
            bundle.putInt("Tag number", position - (itemsIds.length-1));
            sensorTagFragment.setArguments(bundle);*/
            FragmentTransaction ft = fm.beginTransaction();
            ft.hide(this).show(sensorTagFragment).commit();
        }
    } else {
        FormulaEditorFragment formulaEditor = (FormulaEditorFragment) getSherlockActivity()
                .getSupportFragmentManager()
                .findFragmentByTag(FormulaEditorFragment.FORMULA_EDITOR_FRAGMENT_TAG);
        if (formulaEditor != null) {
            formulaEditor.addResourceToActiveFormula(itemsIds[position]);
            formulaEditor.updateButtonViewOnKeyboard();
            KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
            onKey(null, keyEvent.getKeyCode(), keyEvent);
        }
    }

}

From source file:net.eledge.android.europeana.gui.activity.HomeActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    NewBlogNotification.cancel(this);

    PreferenceManager.setDefaultValues(this, R.xml.settings, false);

    searchController.suggestionPageSize = getResources().getInteger(R.integer.home_suggestions_pagesize);
    isLandscape = getResources().getBoolean(R.bool.home_support_landscape);

    mSuggestionsAdaptor = new SuggestionAdapter(this, new ArrayList<Item>());
    mGridViewSuggestions.setAdapter(mSuggestionsAdaptor);
    mGridViewSuggestions.setOnItemClickListener(this);

    mEditTextQuery.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override//  w w  w .j a va 2  s . c  o m
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((actionId == EditorInfo.IME_ACTION_SEARCH) || (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                performSearch(v.getText().toString());
                return true;
            }
            return false;
        }
    });
    mEditTextQuery.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() > 2) {
                if (mGridViewSuggestions.isShown()) {
                    mSuggestionsAdaptor.clear();
                    mSuggestionsAdaptor.notifyDataSetChanged();
                }
                searchController.suggestions(HomeActivity.this, s.toString());
            } else {
                onTaskFinished(null);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // ignore
        }

        @Override
        public void afterTextChanged(Editable s) {
            // ignore
        }
    });

    mProfilesAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item);
    mSpinnerProfiles.setAdapter(mProfilesAdapter);

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    if (mBlogFragment == null) {
        mBlogFragment = new HomeBlogFragment();
    }
    fragmentTransaction.replace(R.id.activity_home_fragment_blog, mBlogFragment);
    fragmentTransaction.commit();
}

From source file:org.chromium.chrome.browser.media.remote.ExpandedControllerActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int keyCode = event.getKeyCode();
    if ((keyCode != KeyEvent.KEYCODE_VOLUME_DOWN && keyCode != KeyEvent.KEYCODE_VOLUME_UP)
            || mVideoInfo.state == PlayerState.FINISHED) {
        return super.dispatchKeyEvent(event);
    }/*  w  w w.j  ava  2s .  c  o m*/

    return handleVolumeKeyEvent(mMediaRouteController, event);
}

From source file:org.egov.android.view.activity.SearchActivity.java

/**
 * Event triggered when pressing enter/done key,call _getSearchList() function
 *//* w  w  w .j  a  v a2  s  .c  o m*/
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))
            || (actionId == EditorInfo.IME_ACTION_DONE)) {
        _getSearchList();
    }
    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;/*from w  w  w .  j  ava  2 s.com*/
    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);
                        }
                    }
                }
            }
        }
    }
}

From source file:com.github.kanata3249.ffxieq.android.AugmentEditActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            EditText et;// ww w.j  av a2  s  .  co  m
            Equipment eq;

            eq = getDAO().instantiateEquipment(mBaseID, mAugID);
            et = (EditText) findViewById(R.id.AugmentDescription);
            if (eq != null && et != null) {
                String augment;

                augment = et.getText().toString();
                if (!eq.getAugment().equals(augment)) {
                    showDialog(R.string.QueryDiscardChanges);
                    return true;
                }
            }
        }
    }

    return super.dispatchKeyEvent(event);
}

From source file:com.xee.auth.SignInButton.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_UP && (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER
            || event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
        if (mListener != null) {
            mListener.onClick(this);
        }//from w w w. ja va  2 s . c  o  m
        if (mConnectionCallback != null) {
            signIn();
        }
    }
    return super.dispatchKeyEvent(event);
}

From source file:com.oakesville.mythling.MediaPagerActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
            if ((getCurrentFocus().getParent() instanceof View
                    && ((View) getCurrentFocus().getParent()).getId() == R.id.button_bar)
                    || getCurrentFocus().getId() == R.id.title_text) {
                findViewById(R.id.pager_position).requestFocus();
                return true;
            }/*from w  w  w. j  ava  2 s. co m*/
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
            if (getCurrentFocus().getId() == R.id.pager_position) {
                if (pager.getCurrentItem() < getListables().size() - 1) {
                    pager.setCurrentItem(pager.getCurrentItem() + 1);
                    positionBar.setProgress(pager.getCurrentItem());
                    return true;
                }
            }
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
            if (getCurrentFocus().getId() == R.id.pager_position) {
                if (pager.getCurrentItem() > 0) {
                    pager.setCurrentItem(pager.getCurrentItem() - 1);
                    positionBar.setProgress(pager.getCurrentItem());
                    return true;
                }
            }
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_MEDIA_FAST_FORWARD) {
            pager.setCurrentItem(getListables().size() - 1);
            positionBar.setProgress(getListables().size() - 1);
            return true;
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_MEDIA_REWIND) {
            pager.setCurrentItem(0);
            positionBar.setProgress(0);
            return true;
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
                || event.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PLAY) {
            Listable listable = getListables().get(pager.getCurrentItem());
            if (listable instanceof Item) {
                playItem((Item) listable);
                return true;
            }
        }
    }
    return super.dispatchKeyEvent(event);
}