I am trying to get the keyboard to come up when an editText view gets or has focus. I am getting the error getOnFocusChangeListener in view cannot be applied to anonymous android.view.View.OnFocusChangeListener
The error starts on new View.OnFocusChangeListener() and goes through the whole class. I can't figure out why or how to get this working.
Here is my code:
final EditText measurement = (EditText)dialog.findViewById(R.id.measurement);
measurement.getOnFocusChangeListener(new View.OnFocusChangeListener(){
#Override
public void onFocusChange(View v, boolean hasFocus){
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(hasFocus){
imm.showSoftInput(measurement, InputMethodManager.SHOW_IMPLICIT);
}else{
imm.showSoftInput(measurement, InputMethodManager.HIDE);
}
}
});
Please help me fill in the gaps in my knowledge about why this isn't working
instead of
measurement.getOnFocusChangeListener
// you have to use
measurement.setOnFocusChangeListener
also you dont need to set a listener for edit text. whenever a edit text is clicked the softkeyboard will show up by itself, unless you are modifying certain behavior.
Related
I have an EditText with three toggle buttons beneath it.
I want to keep the focus on the EditText AND have the keyboard stay visible when I tap on any of the three toggles. i.e. I do not want the keyboard to hide when the focus is outside the EditText (I should not see the keyboard hide then reopen).
I've tried the following to no avail:
toggleButton.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
editText.requestFocus();
// This doesn't fully work.
// Focus is on editText but keyboard still hides when I
// tap on the toggle button.
}
});
The EditText and ToggleButtons are in a fragment, and the parent activity has this configuration in the AndroidManifest.
<activity
android:name=".activities.MyActivity"
android:label="#string/m_activity"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden|adjustResize" />
What is the best way to fix this issue?
I think you should do that for your yourEditText, using OnFocusChangeListener
yourEditText.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
yourEditText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
}
});
This means that, you will request focus whenever it is changed for you yourEditText and you will also show keyboard.
You can use the LayoutParms.Example is given below
1.Hide the keyboard
this.getWindow().setSoftInputMode
(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
2.Show the Keyboard
this.getWindow().setSoftInputMode
(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
If you want to Know the other options. Refer the below link.
https://developer.android.com/reference/android/view/WindowManager.LayoutParams.
html
I have multiple EditText on my screen and one of them is focussed. The softkeyboard does not trigger as soon as the screen pops up. I want the soft keyboard to trigger as soon as the screen pops up. It works well if I don't implement the onFocusChangeListener(). However I need the onFocusChangeListener() to detect which editText is focused. I have tried setting setFocusable(true) and setFocusableInTouchMode(true). Also i don't want to modify the android:windowSoftInputMode property in AndroidMenifest.xml. I have the following criteria :
onFocusChangeListener implemented (to detect which edittext is focused)
No modifications in AndroidMenifest.xml
Here is my code snippet
final InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
mInput.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
mIsFocused = hasFocus;
if(hasFocus)
inputMethodManager.showSoftInput(mInput, InputMethodManager.SHOW_IMPLICIT);
}
});
Any suggestions ?
You can open softkeyboard programmatically
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
You can try using inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0).
Source: Stackoverflow post
Ok - I've bonked my head quite a bit with this. I have a custom dialog which has multiple edittexts. I've set my layout so that it does not automatically focus on the first as both are initially filled with default values (thus the user may just press 'accept')
I want any edittext to clear itself when touched and open a keyboard for numeric input. They may change one or both fields. If they touch and do not input, the field should change back to a default value.
I have implemented this with setOnFocusChangedListeners in addition to the addTextChangedListeners.
My first problem occured with the realization that the keyboard was toggling itself open/closed when a user touched both edittexts. I resolved this by using SHOW_FORCED,HIDE_NOT_ALWAYS as parameters to toggleSoftInput. Note that this was the only set of parameters which kept the keyboard open when a second field was touched.
Unfortunately, this has created a second problem which I do not understand - on exiting the dialog, I can no longer close the keyboard (ie, it remains visible on the following view). Previously, when I did not make an effort to clear the input, keyboards closed out ok. Using SHOW_IMPLICIT (ignoring the toggle issue) also has no problem with an open keyboard being closed on exit.
So.. how the * do I get this to work?
Below are some relevant sections of code:
edQuantity.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
Log.i(TAG,"edQuantity focus changed");
if (hasFocus) {
Log.i(TAG,"edQuantity HAS FOCUS");
edQuantity.setHint("");
edQuantity.setText("");
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_NOT_ALWAYS);
}
// was the other field left empty after a change attempt?
if (String.valueOf(edPrice.getText()).length()==0) edPrice.setText("0.01");
}
});
edPrice.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
Log.i(TAG,"edPrice focus changed");
if (hasFocus) {
Log.i(TAG,"edPrice HAS FOCUS");
edPrice.setHint("");
edPrice.setText("");
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_NOT_ALWAYS);
}
// was the other field left empty after a change attempt?
if (String.valueOf(edQuantity.getText()).length()==0) edQuantity.setText("1");
}
});
protected static void dismissCustomDialog(Dialog dialog, Context context) {
if (dialog != null) {
// hide the soft keyboard
if (dialog.getCurrentFocus() != null) {
Log.i(TAG,"trying to hide a keyboard");
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(dialog.getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
imm.hideSoftInputFromWindow(dialog.getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
if(dialog.isShowing()) dialog.dismiss();
}
}
As per my comment above, used a toggle to see if a keyboard was already opened.
I stuck with one issue in my current application. I have one editText and i have set it Enables = false in XML file. I have to buttons also named as EDIT and DONE.
Problem.
When application launches it works fine. EditText is not having focus and when i click on editext keyboard appears and i can edit text in edit text.
if(v==Edit)
{
//PurchaseAddressdata.setFocusable(true);
Edit.setVisibility(Button.INVISIBLE);
done.setVisibility(Button.VISIBLE);
PurchaseAddressdata.setClickable(true);
PurchaseAddressdata.setEnabled(true);
PurchaseAddressdata.setCursorVisible(true);
PurchaseAddressdata.setSelection(PurchaseAddressdata.getText().length());
}
if(v==done)
{
Edit.setVisibility(Button.VISIBLE);
done.setVisibility(Button.INVISIBLE);
PurchaseAddressdata.setCursorVisible(false);
PurchaseAddressdata.setClickable(false);
}
But the problem is when i click on editext keyboard is appearing. I want when i press Edit button then only keyboard will appear.
Thanks
Set the onFocusChangeListener listener of the edit text field to this so that it hides the keyboard when focused.
boolean display_keyboard = false;
edittext.setOnFocusChangeListener(new OnFocusChangeListener()
{
public void onFocusChange(View view, boolean arg1)
{
if (!display_keyboard)
{
InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
});
Then when the Edit button is clicked, set displaykeyboard = true; and move the focus back to the edittext
try this code it works for me.
editText.setInputType(0);
and in onClick()
editText.setInputtype(InputType.TYPE_CLASS_TEXT);
I have 2 editfields in my Custom Dialog which is called from ACtivity, among them 1 is of "trxtPassword" and other of "text" type. Keyboard doesn't appear in "text" type editbox, but just comes on "textPassword" edittext, and then doesn't go only.
I tried the following, but nothing works:
InputMethodManager inputManager = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//inputManager.hideSoftInputFromWindow(txt_username.getWindowToken(), 0);
//inputManager.hideSoftInputFromWindow(txt_password.getWindowToken(), 0);
If I make txt_password.setInputType(0); then others can see the password easily, so that can't be used.
What more can be doen to achieve the goal? I did to trap the onLostFocus on txt
txt_password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus == false) {
InputMethodManager inputManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(LoginDialog.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
});
But unfortunately, once entered, if I click anywhere else or any checkbox, then also the focus is not lost from the txt_password field. That is only lost if I click another editText and then the onFocusChange event is fired and throws error and the application shuts down.
Any idea how to accomplish this?
Use that to keep keyboard hidden on activity start
<activity
android:name=".views.DrugstoreEditView"
android:windowSoftInputMode="stateHidden"></activity>
And there is one usefull answer: How to hide soft keyboard on android after clicking outside EditText?