How can I catch the event when the user, after he entered the text on an EditText, select Back and the keyboard disappear?
Override onBackPressed at your Activity class:
#Override
public void onBackPressed() {
//If keyboard is on, do what you want
super.onBackPressed();
}
If you want to catch the back key when user has finished entering text in EditText and he presses back key then you should use:
EditText edit = (EditText)findViewById(R.id.yourId);
edit.setOnKeyListener(new EditText.OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
System.out.println("******back key caught in edit.setOnKeyListener");
}
return false;
}
});
I had the same problem. Vineet Shukla's answer was not working, until I made sure that the delegate was a EditText.OnKeyListener. Prior to that it was a View.OnKeyListener and I was not seeing KeyEvent.KEYCODE_BACK == keyCode, for was I even seeing onKey ever called. I hope this is helpful to someone having a similar problem, although this post is a year old. Cheers.
Related
how to know when I press the delete key from the soft keyboard in android, an event the content of the edittext is empty. I know when the content is not empty, can use TextChangeListener. Only the empty content how to listen the pressed key.
KeyEvent provides keyboard events
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Do Code here
}
return super.onKeyDown(keyCode, event);
}
setListener is not worked in same android system. Finally I got a smart idea, which can use a space replace by ImageSpan,and set picture size is 0*0. So there always will not be empty.
I have an edit box which popup an android keyboard. What i want is on click of done button in the keyboard i want to display a message in the and if the user click rest of the buttons in-spite of done button the message should be clear. I have used this code but it doesn't work. Please help me to solve this out.
Code i have used
m_passwrdEditText.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_ENTER)
{
m_passwrdErrorText.setText("Test Project");
}
else
{
m_passwrdErrorText.setText("");
}
m_passwrdEditText.setTypeface(face);
return false;
}
});
return true from onKey() method.
I am new to android, I have a small doubt regarding how to handle the hardware Keyboard and if I click the search button in any part of my application it should be handled means I need to pass the intent of search activity?
How I can reach this goal.
Try this,
#Override
public boolean onSearchRequested() {
// your stuff here
return false;
}
It will also trigger onKeyDown with a keyCode of KeyEvent.KEYCODE_SEARCH before calling onSearchRequested as stated above
Add a listener for your EditText to listen on the Search button as following:
myEditText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH || keyCode == KeyEvent.KEYCODE_ENTER) {
//Do some stuff
}
//Leave the return value false to hide the SoftKeyboard if it is shown
return false;
}
});
For me, it happened before that the softkey search button is detected as Enter button not search. That's why I'm ORing them
I have an Activity with two EditText fields. So when I press enter in the first one the second one becomes focused.
However I want to disable Enter key in the first EditText sometimes (for example when I think that the user haven't made a proper input into the first EditText).
I' ve overwriten onKeyDown for the 1st EditText returning true when key event is KEYCODE_ENTER but that doesn't help.
What shall I do?
How about adding an OnFocusChangeListener? When your EditText looses focus, you can do validation and can do a requestFocus for the "right" field.
Just by adding an editorActionListener:
editText1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if(isValidContent(editText1.getText().toString()){
editText2.requestFocus();
return true;
} else {
....
}
}
return false;
}
});
when i click on softkeyboard my keyboard getdown or hide but i want to go to next activity when i click on "done " button on keyboard of android.so how to do it?
and my next qus is if i have 2 edit box in my layout when i click on first edit box then in my soft keyboard "next " would appear for going to next text box and when i go to second text box it change to "done".
thanks in advance....
x
Its is better to add some button in the layout as all android phones dont provide a consistent behaviour when using imeoptions.
This seems to be a bug. Different manufacturers make a customized keyboard for their phone which may not completely behave as the android standard keyboard. This issue has been raised before. Most people overcome this issue by either overiding the onKey event or using a TextWatcher class. A bug has been filed about this
http://code.google.com/p/android/issues/detail?id=2882
You can use a listener to check for imeoptions
incomeInput.setOnEditorActionListener(new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE){
//Do your stuff here
return true; // mark the event as consumed
}
return false;
}
}
You need to implement OnEditorActionListener interface.
Code looks like :
public class Main extends Activity implements OnEditorActionListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
}
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
startActivity(new Intent());
return true;
}
return false;
}}
Use setOnKeyListener with your second EditText and in onKey method do something like this.
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN)
performYourAction();
return false;
}
I hope it will be helpfull for you.