Android simulate button press - android

I am trying to execute an event mouse press within my android application. When the user enters a CARRIAGE RETURN when entering text into a text field. I would like to execute a mouse button press on an ADD button when that character is detected in my OnClickListener for that EditText ui.

Peter -
It sounds like what you want to do is override the EditorAction for the given EditText, and then programmatically perform the same action as the OnClickListener. For example:
EditText inputText; //This is either created in code or inflated via XML
Button addButton; //This is either created in code or inflated via XML
inputText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
addButton.performClick();
//Tell the system you consumed the action event
return true;
}
});
The actionId can be a useful property too, as it reports the specific action (DONE, NEXT, etc.) based on the soft keyboard method shown...but keep in mind that if the user presses enter from a hardware keyboard the action will ALWAYS be EditorInfo.IME_NULL, so it may not serve your purpose to monitor this value.
This is a safer method than overriding KeyEvent listeners, as you run less risk of consuming events you don't want and didn't know you stole.
Hope that helps!

Related

How to know when the user is done with text entry on an EditText control?

I want to be able to know when a user is done entering text in an EditText control. I'm thinking maybe it's best to know when the keyboard is closed or something similar. This is using Kotlin on Android app. I'm not sure why it's so hard to find basic answers like this. Maybe I'm searching with the wrong question (new to Android dev).
Using keyboard close as an indicator that the user finished entering text is a bad idea (the user might open the keyboard again to enter more text). A better solution would be to explicitly require for the user to indicate that he has finished entering the data.
You could use a "submit" button.
You can also set the android:imeOptions of EditText to actionDone and set a listener on the EditText.
editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(EditText v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//do your stuff
return true;
}
return false;
}
});
Update - Assuming keyboard close as the indicator is bad for a couple of reasons,
There is no 'proper' way to monitor the soft keyboard. You could try listening to the focus of the EditText or you could use the height difference to guesstimate whether the soft keyboard is open or closed(the option used in most keyboard listener libraries). But these aren't reliable and might break in production.
It's an 'unexpected' application behavior for the user. For example, the keyboard can be removed by pressing the back button. In general, the user would expect that the action would not proceed if the back button is pressed. But if you listen to keyboard close, then it would end up resulting in poorer UX.
There are no actual reasons why you would want to use keyboard close as the trigger. If you want to perform the action as the user types, then you should use TextWatcher, otherwise stick to explicit confirm options.
use onFocusChangeListener to know if the user has finished to add text and has leave the textInput focus.
Example
editText?.onFocusChangeListener =
View.OnFocusChangeListener { _,
hasFocus ->
if (!hasFocus) {
// code to execute when EditText loses focus
}
}

Block physical keyboard input in EditText

I have a EditText that should not allow the user to input anything through the keyboard (soft or hard). This EditText should only allow the user to input something through keys(buttons) displayed in the screen by the app.
I have disabled the soft keyboard, but I can't find a way to disable the hardware keyboard input. This input via hardware keyboard can be done using a emulator that is configured to allow input through the hardware keyboard.
So, my question is, How can I block the input via physical keyboard in a EditText?
Thank you!
Finally got it!
Code:
editText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
return true;
}
});
Explanation:
Returning true will tell the listener that I've already handled the hardware keyboard input. Based on Android documentation (http://developer.android.com/reference/android/view/View.OnKeyListener.html)
View.OnKeyListener: Interface definition for a callback to be invoked when a hardware key event is dispatched to this view. The callback will be invoked before the key event is given to the view. This is only useful for hardware keyboards; a software input method has no obligation to trigger this listener.
onKey(View v, int keyCode, KeyEvent event)
Called when a hardware key is dispatched to a view.
You could try these according to what you want in your xml layout
android:longClickable="false"
android:clickable="false"
android:cursorVisible="false"
android:editable="false"
Then make the edittext show what the user typed through the keyboard you made in the app.

Android: how to simulate back button to hide a keyboard?

This problem may seem trivial but I wasn't able to find any nice and simple solution.
I've got an activity with a EditText and a software 'back' Button which simply calls finish() method of activity.
When I click on the EditText, there is a soft keyboard shown to input the text.
I want to achieve the following functionality when clicking the 'back' button (exactly the same as it is with the hardware back button):
- when the Keyboard is hidden, the onClick method should call finish() to end the activity
- when the Keyboard is shown, the onClick methond should hide the keyboard.
Is there any simple way to do that?
Keyboard Pasition
finding if keyboard is hidden or not?
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Selection of Back Button
1)First you have to detect the back key in functionality :
here is code:
start changing the ‘Back’ button, behavior, you need to override the onKeyDown()
method and than check if the desired button has been pressed:
//Override the onKeyDown method
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//replaces the default 'Back' button action
if(keyCode==KeyEvent.KEYCODE_BACK)
{
//do whatever you want the 'Back' button to do
//as an example the 'Back' button is set to start a new Activity named 'NewActivity'
this.startActivity(new Intent(YourActivity.this,NewActivity.class));
}
return true;
}
at least for Android 2.0.
#Override
public void onBackPressed()
{
//do whatever you want the 'Back' button to do
//as an example the 'Back' button is set to start a new Activity named 'NewActivity'
this.startActivity(new Intent(YourActivity.this,NewActivity.class));
return;
}

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