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.
Related
I am trying to load an URL from the keyboard, such that when I click on "DONE" the URL loads in WebView. I tried to use KeyEvent but it is not working. How do I do it?
Assuming that you are typing something into an edittext then the following can be used to do what you want:
yourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
yourWebview.loadUrl(yourEditText.getText());
return true;
}
return false;
}
});
You can have different action depending on what the keyboard says for example "next" or "done", just check the other actions inside EditorInfo.
Let me know if you need any other help.
I am trying to respond to pressing the enter button on a standard system on screen soft keybord, in numeric mode. keyboard appears on the screen when myCustomEditText class is given focus.
tried 2 approaches:
overriding onKeyDown():
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
//do sth..
return true;
}
return super.onKeyDown(keyCode, event);
}
setting listener setOnKeyListener:
myCustomEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
onRightButtonClicked();
return true;
}
return false;
}
});
(also tried onKeyDown() in myCustomEditText as well, but with no effect)
problem is:
these aproaches above works for all the keys being pressed but for the enter key - the methods are not even being fired at all. (it's not a problem of bad KeyEvent.KEYCODE_...)
my question is:
why these methods are not being fired for enter key, but are for other keys, and how to listen to enter key being pressed properly?
You respond to ENTER differently than normal keys, try something like this:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
switch (actionId) {
case EditorInfo.IME_ACTION_DONE:
...
return true;
default:
return false;
}
}
});
You can specify which action is performed by ENTER by specifiying android:imeOptions in XML or by calling editText.setImeOptions(...) with one of the constants in EditorInfo. imeOptions also changes how the ENTER key looks! For example if you set imeOptions to actionSearch then the ENTER key will look like a search button.
You can find a list of all possible values for imeOptions along with a description here.
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
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.
How to capture when user presses "Search Soft key" in android.
Can we do it in onKeyDown() and checking the keycode?
Try this in your Activity code:
#Override
public boolean onKeyDown( int keyCode, KeyEvent event )
{
if ( event.getKeyCode() == KeyEvent.KEYCODE_SEARCH )
{
// Catch the search
// do something
// return true to consume press or false to pass it on
}
return super.onKeyDown( keyCode, event );
}
I think that you are right. You can check for the key code that is given in the following list.
And prefer to check this onKeyUp.
http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_SEARCH
I've done something like this for capturing the search softkey when progress dialog is showing and it worked fine for me:
progressDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH) {
return true;
}
return false;
}
});