I'm currently developing a calculator app where I have made a custom keypad and would like to hide the virtual keyboard. I have found solutions where I can hide it, but the cursor also gets hidden. The functionality I want is the same as the com.android.calculator2 app. I have looked at the source code of that but I still can't get it to work.
I think you are getting it wrong. There is a much easier solution(and a more obvious one).
Make the EditText uneditable.
Bind to the EditText in your code (findViewById)
In your buttons, get the text and add to the current string and then display it.
Eg.
say you pressed the '1' button.
in your one.setOnclickListener(), do this:
String S=EditText.getText()+"1";
EditText.setText(s);
Edit:
If you just want to hide the keyboard while keeping the cursor, try this code:
EditText editText = (EditText)findViewById(R.id.edit_text);
editText.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
return true;
}
});
Related
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.
I have an EditText where I want to handle the inputs myself, so I don't want the soft keyboard to show up when I click it (or when selection changes, focus changed, long clicked, etc). However, I still want to be able select the text, change cursor position, copy/past, etc.
I have tried putting android:windowSoftInputMode="stateAlwaysHidden" in the manifest, but that doesn't seems to do much. I also tried adding the following
edittext.setOnTouchListener(new OnTouchListener() {
#Override public boolean onTouch(View v, MotionEvent event) {
EditText edittext = (EditText) v;
int inType = edittext.getInputType(); // Backup the input type
edittext.setInputType(InputType.TYPE_NULL); // Disable standard keyboard
edittext.onTouchEvent(event); // Call native handler
edittext.setInputType(inType); // Restore input type
return true; // Consume touch event
}
});
which disables the keyboard, but also prevent the cursor from working.
Currently I'm basically trying to add listeners for all the situations where the keyboard might pop up to toggle it off, but this is very clunky and I can't catch all the cases. Is there a better way to disable the soft keyboard for a particular EditText or fragment?
Obviously the best solution would be if Google gave an inputType that works like this.
The following tends to work. It will sometimes flicker as the keyboard is loaded and then is instantly murdered. But, it just listens for when you click on the textview and then when that happens it murders the keyboard.
It can't account for things like if the textfield gets focus some other way, but for my purposes (I have a textfield for a barcode reader that gets a barcode read into it by a barcode reader (hardware keyboard)), so a softkeyboard makes no sense.
editView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
//imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
});
I also added the other line you might want in there and commented it out. Namely if you want to hide the Android launched soft keyboard or if a user loads the keyboard by holding menu if that might close too.
Try this code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Disable IME for this application
getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
setContentView(R.layout.activity_layout);
I want to do the following :
I have an edit text and want to be able to select text from it without showing the soft keyboard and without editing it's content, just to select text to use it in another screen
N.B. this edit text is inside dialog box that shown above the activity, the current scenario that it show up the keyboard when open the dialog
I have tried the following
android:windowSoftInputMode="stateHidden"
I have also used the following code :
editText.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
hideSoftKetboard (v);
return true;
}
});
private void hideSoftKetboard (View v){
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
can anyone help please ?
Use setInputType(0); will solve the problem
I have an activity where a text view gains focus of a keyboard.
I set an ontouchlistener to the parent view; if the click is outside the range of the textview, I am hiding my textview by using ;
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(textView.getWindowToken(), 0);
My only problem is, if the keyboard is not showing and the parent is clicked on, it keeps firing up those two lines, which ultimately causes useless transactions (I like to save as much processing as possible ..)
I have been trying to use some textview methods like hasfocus or isfocused etc but I can't quite seem to find one that only fires off the text view makes a keyboard show ...
Does anyone know if this is even possible?
THe if statement below is the place I would like to put the method ..
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (textView.*SOME METHOD HERE*?) {
Log.e(TAG, "LOSING FOCUS");
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(textView.getWindowToken(), 0);
}
}
return true;
}
Thanks
Check the configuration. The keyboardHidden value will hold whether any keyboard is out. This will work unless there's a face keyboard (think Blackberry style). If those are important you'll need some more complicated logic including the keyboard field and hardKeyboardHidden fields.
What I want to do is this: I have an EditText and when the user click on it, the user can move the caret position, same as the EditText, but without showing the keyboard.
I've tried with setInputType(0); and it hides completly the keyboard but the cursor doesn't appears.
Is there any way to do this?
Thank's
The following tricks works for me. Caret and soft keyboard both active onTouch event of editText. So Call touch event and then hide the keyboard manually.
myEditText.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final boolean ret = dialerNumber.onTouchEvent(event);
final InputMethodManager imm = ((InputMethodManager) myContext
.getSystemService(Context.INPUT_METHOD_SERVICE));
try{
imm.hideSoftInputFromWindow(myEditText.getApplicationWindowToken(), 0);
}catch(Exception e){
e.printStackTrace();
}
return ret;
});
I haven't tried suppressing the keyboard altogether, but I have hidden the soft keyboard manually before using the InputMethodManager.hideSoftInputFromWindow method.
Does this work?
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)