I'm trying to authenticate users in my app by their keystrokes on the soft keyboard, I want to get:
The key release and press.
the pressure on the key.
the finger area on the key.
I used the onKeyUp and onKeyDown but It doesn"t work.
So how can I get these events from the soft keyboard.
Instead of using the onKeyDown from the view, override dispatchKeyEvent at the activity level. This will handle your key event before it gets to the window manager, so make sure to call super on any key events you do not explicitly handle. Example using the ACTION_DOWN (as every event has both ACTION_UP and ACTION_DOWN)
#Override
public boolean dispatchKeyEvent(KeyEvent event){
if(event.getAction() == KeyEvent.ACTION_UP) {
return super.dispatchKeyEvent(event);
}
if (keyCode == KeyEvent.KEYCODE_DEL) {
} else if (keyCode == KeyEvent.KEYCODE_BACK) {
} else if (keyCode == KeyEvent.KEYCODE_ENTER) {
} else {
return super.dispatchKeyEvent(event);
}
}
I have a custom device with a custom button and I need to handle an hardware Button Events/Intents:
every time I press the button it generates a PTT Press Action and I need to open my custom application, is there a way to do this?
If by custom device, you mean custom AOSP. Then make sure, it's button bound to events, this should be done with low level device driver configuration. And it's complicated work. Next, make sure you receive your click in next events.
Try to either use dispatch key event on Activity work:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if (event.getAction() == KeyEvent.ACTION_UP){
enter();
return true;
}}
return super.dispatchKeyEvent(event);
};
On on key event with Android View which is in focus.
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
/* This is a sample for handling the Enter button */
return true;
}
return false;
}
I am getting the users information from an edit text. I do have a listener that gets their entered information after clicking submit, but I want to also get the entered info after clicking back or clicking somewhere else:
For example, if the users clicks on the black space, I want to get the text they entered. If they type "hello", and click back rather than "enter", I still want to get the text hello. If, however, they don't type anything, I don't care about their input. How can I achieve this?
Thanks,
Ruchir
First add these as a class variables
private String inputText;
private EditText yourEditText;
Get the instance of your EditText View
yourEditText = (EditText)findViewById(R.id.your_editText);
When a button is clicked, you can get the content of the EditText field like this
Button mButton = (Button)findViewById(R.id.m_button);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
inputText = yourEditText.getText().toString();
}
});
If a user press the Back button, you can get the input if any like this
#Override
public void onBackPressed() {
inputText = yourEditText.getText().toString();
super.onBackPressed();
}
Then check if there is any value assigned to your String variable
if(inputText.equals("") || inputText == null){
// there is no value
}else{
// there is value entered.
}
To extend my solution for clicking some where else
add a class variable
private boolean isEditTextHasFocus;
then create a focus listener which will check if the Edittext has focus
private View.OnFocusChangeListener focusListener = new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){
isEditTextHasFocus = true;
} else {
isEditTextHasFocus = false;
}
}
}
Add this line in onCreate(Bundle savedInstanceState) method
yourEditText.setOnFocusChangeListener(focusListener);
Then override to onTouchEvent(MotionEvent event) listener and access the Edittext input when the key up action is called
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if(!isEditTextHasFocus){
inputText = yourEditText.getText().toString();
}
}
return true;
}
I hope this will give you a further idea to find your unique solution.
Overriding what happens when the back button is pressed is bad practice and is unnecessary for what you want to do.
You need to use a special listener called onFocusChangedListener. This function is called anytime an element gains or loses focus. In this case for your editText it will be called whenever someone clicks on it or away. Pressing the back button or leaving the editText in any way will call this function. In the following code I check if
if(!username.hasFocus())
which makes it so the value is only saved when focus from the editText is lost rather than everytime focus is changed.
You haven't added any of your own code so I am just going to use obvious placeholder variables in my code example.
Edittext username = (EditText findViewById(R.id.YOUR_EDITTEXTS_ID);
String previousValue = ""; // to keep track of value change
String usernameValue = "";
username.setOnFocusChangeListener(new View.OnFocusChangeListener(
{
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (username.hasFocus()){
//take note of value for comparison when clicking away
previousValue = username.getText().toString();
} else if (!username.hasFocus()){
// check if value has changed
if (!previousValue.equals(username.getText().toString()){
usernameValue = username.getText().toString();
}
}
}
});
I want my test data goes to another window after Enter key press, like a click on the send button.
I'm using this code:
text1 = ((TextView) findViewById(R.id.textMsg));
text1.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)) {
// Perform action on key press
Toast.makeText(IMSendData.this, text1.getText(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
Using this code after I press the Enter key, its goes on send button, and after second Enter key press the test data goes to the window.
I want to press Enter button only once and then the text go to the window.
you go through this..
sendData = (EditText) findViewById(R.id.message);
sendData.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)) {
// Perform action on key press
adapter.add(new OneComment(false,
sendData.getText().toString()));
sendData.setText("");
return true;
}
return false;
}
});
For example, send a backspace key to the edit text control to remove a character or send a char code like 112 to append a character in the edittext control programmatically.
Actually, I need a method like
void onKeyReceived(int keyCode)
{
// here I would like to append the keyCode to EditText, I know how to add a visible character, but what about some special keys, like arrow key, backspace key.
}
To send a simulated backspace key press to an EditText you have to send both key press and release events. Like this:
mEditText.dispatchKeyEvent(new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_DEL, 0));
mEditText.dispatchKeyEvent(new KeyEvent(0, 0, KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_DEL, 0));
This can be used to send any other key code, not just delete.
Your question is not all that clear, but I think you want to modify/append text to a TextView when certain buttons are pressed. If so, you want a combination of some of the existing answers.
#Override
public void onCreate(Bundle savedInstanceState) {
...
(TextView) textView = (TextView) findViewById(R.id.myTextView);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch(keyCode) {
case KeyEvent.KEYCODE_BACK:
// user pressed the "BACK" key. Append "_back" to the text
textView.append("_back");
return true;
case KeyEvent.KEYCODE_DEL:
// user pressed the "BACKSPACE" key. Append "_del" to the text
textView.append("_del");
return true;
default:
return super.onKeyDown(keyCode, event);
}
}
Whether to return true for each case you have handled (as above) or to always return super.onKeyDown(keyCode, event); after your switch statement will depend on your exact requirements. Check the documentation for the behaviour of onKeyDown
If, instead of appending text in each case you want to delete a character, or move the cursor, you could do that in each case statement. Have a look at the TextView documentation for the different methods you can call. Also look at the KeyEvent documentation for a list of the keys you can check for.
I think you need use addTextChangedListener to EditText.
Refer the answer of EditText input with pattern android and Live editing of users input
virsir , I suppose you are looking for dispatching hard keys programmatically.
For that you may try dispatch (KeyEvent.Callback receiver, KeyEvent.DispatcherState state, Object target) with an example at Back and other hard keys: three stories
Hope that helps.
Check for key events in your activity. for example, this code listens for back keypress:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
finish();
}
return super.onKeyDown(keyCode, event);
}
just use the setText method to do this. If you are wanting to simulate a backspace you could do something like this.
String curText = mEditText.getText();
if(!curText.equals("")){
mEditText.setText(curText.subString(0, curText.length - 1));
}
to simulate backspace key, just ad code
editText.setText(editText.getText().substring(0,editText.getText().length()-1))
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
to simulate adding a character, put the code
editText.setText(editText.getText() + (char) charCode)
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Take a look at this article: creating-input-method.html. Basically, you can either manually send KeyEvents or you can manually edit and commit text around the cursor in the application's Input View.These are all done via your IME's InputConnection.
Hope this helps,
if you want a click listener, the best way to do it is this:
View textfield = findViewById(R.id.textfield);
textfield .setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
/*your code for the click event here*/ }});
if you want a backspace button, do this:
public void backSpace() {
EditText textfield = (EditText)findViewById(R.id.textfield);
try {
textfield.getText().delete(textfield.getSelectionEnd() - 1, textfield.getSelectionStart());
} catch (Exception e) {
try {
textfield.getText().delete(textfield.length() - 1, textfield.length());
} catch (Exception myException) {
//textfield.getText().delete(textfield.length(), textfield.length() - 1);
}
}
}
if you want to append a character in the EditText, do this:
EditText textfield = (EditText)findViewById(R.id.textfield);
textfield.setText(textfield.getText().concat("112"));
try implementing TextWatcher interface.
it has 3 methods which you need to override.
public void afterTextChanged(Editable s) {
Log.v("afterTextChanged","here");
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.v("beforeTextChanged","here");
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
I think this will work.