I have an activity with a numeric EditText sitting above a button.
I have noticed that if you click in the edit text and use the hard keyboard to type some numbers then press the hard enter, that it shifts the focus to the button below the edit text and a QWERTY keyboard appears. How do I stop this from happening?
I have tried to control the enter key with the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activityEnterAmt);
final EditText editText = (EditText) findViewById(R.id.editTextEnterAmt);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
Log.i("Tag", "Enter pressed or IME Action Submit pressed");
saveButtonClick(editText);
}
return false;
}
});
}
But I really want to stop it from opening up the keyboard when you press enter. I don't really understand why it is doing this?
Thanks
I have fixed this issue by closing the soft keyboard when the EditText loses focus:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (v == editText) {
if (hasFocus) {
((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(editText,
InputMethodManager.SHOW_FORCED); // open keyboard
} else {
((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
editText.getWindowToken(), 0); // close keyboard
}
}
}
});
Related
I made a back button for my webview, but I got a problem.
When I click it more time (for example 5/6) it's back to the main page, but later button work like on delay, so I'm on some page and my app goes back. It's possible to limit the click of the button to only 1 click per page?
Thanks.
Button przycisk_powrot = (Button) findViewById(R.id.przycisk_powrot);
przycisk_powrot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
You can set the button state to enabled/disabled - and can make a check like this:
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (webView.canGoBack()) {
webView.goBack();
// Now check is it still can go back, ?
backButton.setEnabled(webView.canGoBack());
backButton.setAlpha((float)1.0);
}
else {
backButton.setEnabled(false);
// Also make the button little dim, so will show to user that it is currently in disabled state
backButton.setAlpha((float)0.5);
}
}
});
Enjoy..!
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
This is my code for an AlertDialog.Builder that has a custom view with an EditText. After entering the value inside the EditText, I want the press of Enter on the keyboard to act the same way as the PositiveButton of the AlertDialog.Builder. I have included the necessary 'imeOptions' part in the XML file. I manage to execute the code when pressing Enter, but the AlertDialog.Builder is still on screen and does not dismiss like when the PositiveButton on the AlertDialog.Builder is clicked.
//AlertDialog to set weekly income
incomeAlert = new AlertDialog.Builder(this);
incomeInflater = this.getLayoutInflater();
incomeDialogView = incomeInflater.inflate(R.layout.activity_weekincome, null);
incomeAlert.setView(incomeDialogView);
et_WeekIncome = incomeDialogView.findViewById(R.id.ls_WeekIncome);
et_WeekIncome.setOnEditorActionListener(new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
submitIncome();
return true;
}
return false;
}
});
incomeAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
submitIncome();
}
});
Thanks in advance for any help.
UPDATE: I managed to dismiss the AlertDialog.Builder by adding another piece of code as shown below
AlertDialog incomeDialog = incomeAlert.create();
incomeDialog.show();
Then when needing to dismiss, I use
incomeDialog.dismiss();
Since dismiss() is not available with AlertDialog.Builder, I had to create the Builder through an AlertDialog. Then I call dismiss() on the AlertDialog.
Thank you all for your input.
You can use OnKeyListener with your edit text to handle a specific keypress.
mEditTV.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
// do action
return true;
}
return false;
}
});
You can use the above setOnKeyListener this way.
et_WeekIncome.setOnKeyListener(new OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_ENTER:
submitIncome();
return true;
default:
break;
}
}return false;
}
});
Since dismiss() is not available with AlertDialog.Builder, I had to create the Builder through an AlertDialog. Then I call dismiss() on the AlertDialog.
There is another way to deal with this problem: use the setOnShowListener callback to set the key listener. This gives you access to the dialog's dismiss() method.
incomeAlert.setOnShowListener((DialogInterface d) -> {
et_WeekIncome.setOnKeyListener((v, keyCode, event) -> {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_ENTER) {
onClick(dialog, BUTTON_POSITIVE);
d.dismiss();
return true;
}
return false;
});
I want to detect the back button.
However my current implementation does not even detect the back button.
CODE:
#Override
public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
//... OTHER CODE ...
else if(e.getAction() == MotionEvent.BUTTON_BACK){
System.out.println("BACK BUTTON PRESSED");
setCurrentState(new MenuState());
}
return true;
}
}
You can use onBackPressed() inside your Activity:
#Override
public void onBackPressed() {
//Do something
}
It's written in the documentation:
public static final int BUTTON_BACK
Button constant: Back button pressed (mouse back button). The system may send a KEYCODE_BACK key press to the application when this button is pressed.
You need to override onKeyUp function from the Activity (not from the View):
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//todo
}
}
Use Intent inside onBackPressed()like this:
#Override
public void onBackPressed()
{
Intent BackIntent = new Intent(getApplicationContext(), NewActivity.class);
startActivityForResult(BackIntent);
finish();
}
I have an EditText which has the singleLine attribute set to true. When I press Enter on the keyboard, the keyboard is hidden. Is it possible to prevent this?
I've been using OnKeyListener which caused this problem. Switching to OnEditorActionListener stops the Keyboard from closing when pressing Enter and allows me to have full control of it.
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE) {
//DO THINGS HERE
return true;
}
return false;
}
});
this should help you
youredittext.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int keyCode, KeyEvent event) {
if ( (event.getAction() == KeyEvent.ACTION_DOWN ) &&
(keyCode == KeyEvent.KEYCODE_ENTER) )
{
// hide virtual keyboard
InputMethodManager imm =
(InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInputFromInputMethod(edittext.getWindowToken(), 0);
return true;
}
return false;
}
});
when you press the enter key the inputMethodManager will show the keyboard, if needed.
hope this will solve your problem :)
edit:
if this will not work try use the event.getKeyCode() in the secend part of the if statment
edit II: sorry, i read it wrong, i fixed it now try this one.
I change android soft keyboard to show search icon rather than Enter icon. My question is: how i can detect that user click on that search button?
In the edittext you might have used your input method options to search.
<EditText
android:imeOptions="actionSearch"
android:inputType="text"/>
Now use the Editor listener on the EditText to handle the search event as shown below:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// Your piece of code on keyboard search click
return true;
}
return false;
}
});
You can use OnQuerySubmit listener to get keyboard Search button click event.
Here's how,
searchView.setOnQueryTextListener(queryTextListener);
SearchView.OnQueryTextListener queryTextListener
= new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
showLog("searchClickListener onQueryTextSubmit");
getPhotos(searchView.getQuery().toString());
return true;
}
#Override
public boolean onQueryTextChange(String s) {
return false;
}
};