Event catcher, for user hiding keyboard with back button - android

I have an app that uses the keyboard, when the keyboard is up if the user presses back button the keyboard dissappears, however I would like to change one or two other things while it changes, so I need an event listener.
I tried
#Override
public void onBackPressed()
However this doesnt seem to catch the backbutton while the keyboard is up, if the user presses back twice, then this catches the second click only

Register this on your root view to detect keyboard appearance/disappearance :
root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
public void onGlobalLayout(){
int heightDiff = root.getRootView().getHeight()- root.getHeight();
/*If height difference is more then 150, consider keyboard as visible. -150 for disappearing */
}
});
Also, make sure to use adjustResize for keyboard.

Related

Disable SoftInput Keyboard from Closing OnBackPressed

Just wondering if it's possible to prevent the keyboard from closing when the back button is pressed.
AKA, jump to the previous activity on one tap of the back button.
You can override onBackPressed() so that if the keyboard is showing you just call finish() on your Activity:
#Override
public void onBackPressed()
{
boolean keyboardIsShowing = // determine if keyboard is showing somehow.
if (keyboardIsShowing )
{
finish();
}
else
{
super.onBackPressed();
}
}
I am not sure an exact way to know if a keyboard is showing, but this link can point you in the right way:
How to check visibility of software keyboard in Android?
On a side note, users probably don't expect the Activity to close when the back button is pressed, they probably expect the keyboard to close. I would carefully consider your use case before implementing something like this.

delay when hiding soft keyboard

In my Activity I have an AutoCompleteTextView with a dropdown list. When the user selects an item, a new Activity is started. Since I have a lot of stuff in the next Activity, there's a delay of about 0.5-1s before it starts. I'm trying to hide the soft keyboard immediately after an item is selected:
actvActionSearch.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View v,
int position, long id) {
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(actvActionSearch.getWindowToken(), 0);
//do stuff to prepare and start next Activity
}
});
However, the soft keyboard gets hidden appr. at the same time the next Activity starts. Where does this delay come from? Hiding the keyboard is the 1st thing I perform
Note how you are getting InputMethodManager as a system service?
That means your call to hideSoftInputFromWindow is performed on a system Service, which means its always running in the background along side of your app, which means when you hide keyboard, its actually running in parallel as your app which is performing the activity create.

Android - Activities and navigation?

I navigate from Activity1 to Activity2
On Activity 2 I have a keyboard and this keyboard stays on the screen after selecting the back button and going to Activity 1.
This is how I fixed this issue
// This code is in Activity 2
#Override
public void onBackPressed() {
startActivity(intentForActivity1);
finish();
}
Is this a wrong solution to my problem?
Is it a bad idea to handle the back button manually?
Since you're capturing the back button press, most probably the soft keyboard does not receive the press and thus it does not hide.
Try hiding it yourself:
#Override
public void onBackPressed() {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
startActivity(intentForActivity1);
finish();
}
See Reto Meier's answer for more details on this method to hide the keyboard: Close/hide the Android Soft Keyboard
There's nothing inherently wrong with overriding the back button. Just make sure the behavior isn't confusing to the user.
Also, if you ever just want to hide the soft keyboard (say, you're switching between tabs or something like that), you can use InputMethodManager. Here's a thread where people discussed ways to do this.

Suppress appearance of the soft keyboard on EditText focus

I am trying to define a set of buttons that allow the user to enter data into an EditText box. I want all the default functionality of an EditText box except for the pop up of the soft keyboard. the only data to be allowed to enter into the EditText box should be the data from the buttons I have defined. I am trying to suppress the soft keyboard by catching the touch event and returning true. (per the conversation found on this thread)
private OnTouchListener txtTouchListener = new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
//Return true to suppress the passing of the event on to the OS
return true;
}
};
The problem is that this method then blocks the long click event from firing. To solve this I can return false and then handle the long click event. However, this then makes the short click bring up the soft keyboard. Also, upon long click not only is the soft keyboard suppressed, but so is the context menu. I am looking for a way to stop the keyboard from appearing on a short (or long) click but keep all other functionality (updating the cursor position on short click, on long click show the EditText context menu, etc.)
Any ideas on this is greatly appreciated!
This thread suggests:
EditText.setInputType(InputType.TYPE_NULL);
Or you set the inputType in the XML Attribute to none.
Not tried it myself though.

Android: Detect user inactivity / Detect (softkeyboard) keyboard input

I want to detect "user inactivity" in my Android app. To be more precise: I want to detect if the user has NOT done any interaction with my app (touching the screen, scrolling, input texts ...) for a specific time. Technically I use a timer that is reseted on each (user) interaction.
In my activity, I override the onUserInteraction method to detect interactions like scrolling, touching the screen ...
#Override
public void onUserInteraction(){
resetInactiveTimer();
}
Unfortunately, onUserInteraction is not called when the user interacts with the soft keyboard. I think the reason is, that the soft keyboard is not part of my Activity.
For the edit texts in my app I use TextWatcher and the onTextChanged method which works fine. But my app also contain a WebView that loads arbitrary web pages. Of course some web pages could contain input fields and I do not know how to detect that the user interacts with the soft keyboard to edit those text fields.
Still interested in this?
Your activity implements KeyEvent.Callback, so you can override onKeyDown:
#Override
public boolean onKeyDown (int keyCode, KeyEvent event) {
resetInactiveTimer();
return false;
}
Alternatively, (in the most common circumstance) if the key is pressed with the cursor in an EditText or similar, you will need implement an OnKeyListener and use the onKey method to call resetInactiveTimer();

Categories

Resources