When a software keyboard is opened on my virtual device, the back button changes function to hide the keyboard when pressed - it does not stay 'KEYCODE_BACK' like it does whilst the keyboard is hidden.
Is this "hide" button still defined as a KeyEvent or do I need to go a different route in order to run an activity whenever it is pressed?
Android Studio 3.0.1
//setup i was hoping to use, but keycode changes whenever keyboard is shown//
public boolean onKeyDown(int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_BACK)
{
checkEmpty();
}
return false;
}
As stated by CommonsWare, "the system does not pass that event [back button while keyboard is displayed] to the activity. It just collapses the input method editor (soft keyboard)."
So no, it does not trigger a KeyEvent.
Related
First off I am kinda new to Android, so with that being said. So I have a spinner, and every time I make a selection the phone will scroll back up to the last edit text who has focus. That is very annoying so I set the spinner as focusable, but for some reason I then have to click the spinner twice to get it to open (the first click gives the spinner focus, the second opens the spinner). So the best I have come up with so far is this:
activitySpinner = (Spinner) findViewById(R.id.acivity_dropdown);
activitySpinner.setFocusable(true);
activitySpinner.setFocusableInTouchMode(true);
activitySpinner.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
activitySpinner.requestFocus();
activitySpinner.performClick();
return true;
}
});
That takes care of the need for two clicks, but that causes problems because it will open the spinner on the slightest touch, even if all I wanted to do was scroll down. Am I looking at this problem the wrong way? Is there a way to make the spinner focusable and also allow it to open on the first click?
Things I've tried:
Setting focusable in the xml,
setting focusable and focusable in touch mode in java,
the code above
Using setFocusable(true) and setFocusableInTouchMode(true) are correct. To fix the two-touch problem created by the latter check for the ACTION_UP event in your touch handler and return false to let the event bubble up which eliminates the extra requestFocus() call:
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP && !v.hasFocus()) {
v.performClick();
}
return false;
}
The hasFocus() check makes this specific to the click-twice problem; if the control already has focus, your actual tap should bring up the list without additional work.
Try this also
spinner.requestFocusFromTouch();
This question already has answers here:
How to detect "Recent Apps" system button clicks (Honeycomb+)
(8 answers)
Closed 9 years ago.
How can I detect when I press the return application button in android? it is on the right side of the home button on samsung s3. sorry I dont really know what is the name of the button.
Not the back button of the keyboard
It is called the Back Button. You can override its method to handle click events. Something like
#Override
public void onBackPressed()
{
// do stuff
super.onBackPressed();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.i("MainActivity", "Back button pressed, exiting..");
//Your code here..
}
return super.onKeyDown(keyCode, event);
}
Like this. i checked if it is back button. you can look up for the right button constant inside 'KeyEvent' class.
I guess you mean the button for opening the Recent Apps (the one on the right in the image):
There's no way to overriding it. The only method you can intercept is the Activity's onWindowFocusChanged, which is called once the recent apps is displayed, but also is been called on a lot of other different situations.
Take a look here for more info:
How to detect "Recent Apps" system button clicks (Honeycomb+)
And here if you want to avoid opening the recent apps (but it seems it doesn't work on Jellybean and up):
https://stackoverflow.com/a/17095749/1991053
I'm developing an android application and when I press the back button from my device (normal press time for a person, 1 second or less), it skips from my activity, to the previous activity (menu) and then exits the application.
But if I tap the back button quickly, it reacts as expected, it goes to the menu.
I've tried to find a solution but no success.
I've always tried to override the back button default behavior but no success either.
Is there a way to set a reaction time for the back button to react?
Many thanks in advance!
P.S.- I have other activities that maintains the expected behavior in back button when pressed with a normal press time.
"Is there a way to set a reaction time for the back button to react?"
Yes, you can simply record the time when the button is pressed and react differently in onBackPressed by calculating the (currentTime-lastTimePressed)
To allow this to work with previous activities, you can ask activities to startActivityForResult, so that when you finish your activity you can pass on the time as well to let them know if they should exit as well.
I was developing an extra option for an application that already exists, and I found out that I should extend not from the Activity from Android but an already extended Activity called SEActivity. So in this extended version of Activity they override the method onKeyDown like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
else
return super.onKeyDown(keyCode, event);
}
By extending this SEActivity, the Back button works with the expected behavior.
Thanks anyway :)
For my application I have implemented a system that detects if the current version is an old version that requires an update. By the way the detection work as intended.
My problem is how to block the application use when that happens. By now I'm launching an activity for inform the user and grant a link to the Market. My problem is that if the user press the back button returns to the last Activity and I dont know how to change the
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
} else {
return super.onKeyDown(keyCode, event);
}
}
For exit the application when the user press backs and don't go back to the other activities.
Thanks!
You can call finish() on the other activities, but I'm not sure how you'd get a reference to them.
To my knowledge the Android system does not let the programmer end tasks (which contain multiple activities in their back stacks), which is what you're trying to do.
You could try using Fragments instead of Activities, which will let you communicate information and events between them.
Before laucnhing the new activity, make sure to call finish() on main activity (outdated version of main app) to remove from back stack.
What I want to do is change the default "Done" label that appears in the virtual keyboard. Here's what I've tried without any luck:
mSearchInput.setImeOptions(EditorInfo.IME_ACTION_DONE);
mSearchInput.setImeActionLabel(getString(R.string.search_action_label), EditorInfo.IME_ACTION_DONE);
I am able, however, to handle a click on that button, with this:
mSearchInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
performSearch();
return true;
}
return false;
}
});
I'm clueless as to how I can change the label on that button at the moment.
The imeActionLabel sets the label for the button that appears on the top right on full screen IME mode (i.e., when your phone is in landscape). If you want to change the button to the bottom right of the keyboard, you can pass certain flags to imeOptions.
As far as I know, for that button you're limited to a certain set of actions (see here for a full list of supported flags), but since you seem to want a search button, all you have to do is to slightly adjust your first line and use IME_ACTION_SEARCH:
mSearchInput.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
Mind you, the exact appearance of that button will depend on the input method. The default Android keyboard shows a magnifier for the search flag, while the Touch Input (HTC's keyboard) seems completely unaware of that flag, still showing a return button.