I have in my setting the option which should disable the app soft keyboard as some users could have a device with physical keyboard.
The issue is that everything i'm trying is not working and the soft keyboard is shown anyway,
I've tryed:
adding android:windowSoftInputMode="stateAlwaysHidden" in my manifest, i've tryed the following code in my fragment:
fun hideKeyboard(activity: Activity) {
val imm: InputMethodManager =
activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
//Find the currently focused view, so we can grab the correct window token from it.
var view = activity.currentFocus
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = View(activity)
}
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
which i called in onViewCreated like:
hideKeyboard(requireActivity())
I've set to all EditText showSoftInputOnFocus:
val keyboard = prefs.getBoolean("keyboard", true)
txtBarcode.showSoftInputOnFocus = keyboard
txtQta.showSoftInputOnFocus = keyboard
And when showSoftInputOnFocus is set the keyboard doen't appear, but once i type something with the physical keyobard the softkeyboard is shown...
So based on my setting keyboard value (true,false) how can i disable the softkeyboard for the whole applicaiton??
I am trying to disable okButton till the edit text is empty.I couldn't find a solution to it. I tried adding text watcher but I don't know how to disable positive button in that.
val inputTxt = EditText(this)
alert("Enter your mobile number") {
customView = inputTxt
inputTxt.setInputType(InputType.TYPE_CLASS_NUMBER)
inputTxt.setFilters(arrayOf<InputFilter>(InputFilter.LengthFilter(10)))
inputTxt.setRawInputType(Configuration.KEYBOARD_12KEY);
okButton {
startActivity(intentFor<NewActivity>()
}
isCancelable = false
cancelButton { finish() }
}.show()
This is not possible (in a clean way) with the AlertDialog API itself, but you could have your own "OK" button in the custom view that you've added. The main risk is that it is not garanteed to look exactly (same margins and paddings) as the regular "OK" button.
I'm trying to switch the button in the softkey from "Go" to "Done" and vice-versa.
If I just set the imeoption
private fun showDoneOnSoftKeyboard() {
setImeOptionsOnSoftKeyboard(EditorInfo.IME_ACTION_DONE)
}
private fun showGoOnSoftKeyboard() {
setImeOptionsOnSoftKeyboard(EditorInfo.IME_ACTION_GO)
}
private fun setImeOptionsOnSoftKeyboard(imeOptions: Int) {
contractIdInput.imeOptions = imeOptions
}
the button is not changed. I've found that by doing:
private fun setImeOptionsOnSoftKeyboard(imeOptions: Int) {
val inputType = contractIdInput.inputType
contractIdInput.inputType = InputType.TYPE_NULL
contractIdInput.imeOptions = imeOptions
contractIdInput.inputType = inputType
}
the button is changed. The problem is though that the keyboard settings are reset that means that if I have for example the capslock set after I switch between states (for example from Done to Go) then the capslock is reset.
I have also tried
contractIdInput.imeOptions = imeOptions
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.restartInput(contractIdInput)
but this has the same effect.
I tried this one as well:
contractIdInput.setImeActionLabel("Custom text", KeyEvent.KEYCODE_ENTER)
but it does not work either.
Is there any other way to do the same?
It looks like this can't be done. The IME-options are thought to be set statically in the XML or programmatically but they can't be modified dynamically while the user is typing.
My question is on invoking the soft-keyboard in Android.
I am trying out the FingerPaint (API Demos), which is essentially a touch-drawing app. Attempting to give it a touch text-input interface, I added EditText inside the CustomView class, like the following:
private void inputText() {
EditText newText = new EditText(getContext());
//getContext needed as we're in a class extending View, not activity
newText.setText("Text Text Text");
//'addView' is deliberately left out for invisibility
//'requestFocus' also left out
// Show soft keyboard for the user to enter the value.
InputMethodManager imm = (InputMethodManager) ConfigKey.context.getSystemService("input_method");
imm.showSoftInput(newText, InputMethodManager.SHOW_IMPLICIT);
String newTextString = newText.getText().toString();
System.out.println(newTextString);
}
The EditText was added alright (as seen from the Logcat, "Text Text Text") but there was just no keyboard to be seen for inputing.
Any way to fix it?
Any thoughts would be appreciated!
I have an EditText and a Button. On click of the button i want to open the EditText keyboard and at the same time request focus on the EditText, So that the user directly start typing in the keyboard and text appears in the EditText.
But in my case when i click the button it open the keyboard, but it won't set focus on the EditText, because of which user has to click the EditText again to write on it. What is the issue. Any help or suggestion.
Code On click of button
m_SearchEditText.requestFocus();
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(m_SearchEditText.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
ensure that the edittext is focusable in touch mode. You can do it two way.
In xml:
android:focusableInTouchMode="true"
in Java:
view.setFocusableInTouchMode(true);
Personally I don't trust the XML definition of this param. I always request focus by these two lines of code:
view.setFocusableInTouchMode(true);
view.requestFocus();
The keyboard shoul appear on itself without the need to call InputMethodManager.
It works in most of the cases. Once it did not work for me because I have lost the reference to the object due to quite heavy processing - ensure that this is not your issue.
In my case it worked by adding a handler after you clicked to button and focus set in another view the focus can get back to your needed view.
just put this in your code:
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
lastview.getEditText().clearFocus();
m_SearchEditText.requestFocus();
InputMethodManager mgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(mLastNameET, InputMethodManager.SHOW_IMPLICIT);
}
}, 100);
I hope it was helpful
Following saleh sereshki's answer and pauminku's comment, I'm using:
m_SearchEditText.post(new Runnable() {
#Override
public void run() {
m_SearchEditText.requestFocus();
}
});
which in Kotlin is the equivalent to:
m_SearchEditText.post { m_SearchEditText.requestFocus() }
The following works for me and should help:
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
In your manifest.xml write:
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:windowSoftInputMode="stateAlwaysVisible" />
And call m_SearchEditText.requestfocus() in oncreate().
OR,
Try:
if(m_SearchEditText.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
As an extension to this answer (I didn't add it as a comment because of reputation...).
If you want to reduce the delayed time to zero, use handler.post() instead.
Full code:
final Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
lastview.getEditText().clearFocus();
m_SearchEditText.requestFocus();
InputMethodManager mgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(mLastNameET, InputMethodManager.SHOW_IMPLICIT);
}
});
Minimalist Kotlin extension version because we should pretend these sorts of obtuse calls into system services are not necessary:
fun EditText.requestKeyboardFocus() {
val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}
U need two xml attributes also to achieve this:
android:focusable="true"
android:focusableInTouchMode="true"
Add them to the EditText as well as the parent layouts(Any layout inside which these views are). By default these are false, so the focus is not given to the requested view.
Source: https://developer.android.com/reference/android/view/View.html#attr_android:focusable
After u show the EditText based on the checkbox selection, add the next and previous focus points dynamically in code.
Hope this helps.
to focus you can use this function
editText.requestFocus()
you can have two different problems with EditText. one is EditText will be focused but the keyboard is not going to open, so as other answers said, you have to open the keyboard manually. other problem is EditText not gonna focused at all means nothing happened.
for opening keyboard you can do this after requestFocus :
val manager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
manager?.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
if EditText not focused at all check for this properties. EditText has to be focusable, visible, enable, focusableInTouchMode. enable all of them before calling requestFocus.
this.isFocusable = true
this.isFocusableInTouchMode = true
this.visibility = View.VISIBLE
this.isEnabled = true
and at the end you can use this kotlin extention to do all of this for you :
fun EditText.focus() {
this.isFocusable = true
this.isFocusableInTouchMode = true
this.visibility = View.VISIBLE
this.isEnabled = true
this.isCursorVisible = true
this.post {
(this.context as? Activity)?.runOnUiThread {
this.requestFocus()
val manager =
this.context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
manager?.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
}
}
}
In my case none of the answers above worked (they don't work in Nox, for example). To set focus to EditText, you could trick it into thinking that the user actually clicked it.
So I use MotionEvent, like this:
// simulate a click, which consists of ACTION_DOWN and ACTION_UP
MotionEvent eventDown = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0);
editText.dispatchTouchEvent(eventDown);
eventDown.recycle();
MotionEvent eventUp = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_UP, 0, 0, 0);
editText.dispatchTouchEvent(eventUp);
eventUp.recycle();
// To be on the safe side, also use another method
editText.setFocusableInTouchMode(true);
editText.requestFocus();
editText.requestFocusFromTouch();
The above solutions will work if the EditText is enabled.
In my code, I have disabled the view at one point and trying to request focus later without enabling.
If none of the above worked, enable the EditText and then proceed with the above solutions.
In my case it is like,
etpalletId.isEnabled = true
etpalletId.text!!.clear()
etpalletId.requestFocus()
etpalletId.isCursorVisible = true
I was combining some answers and found the following solution:
fun Fragment.focusEditText(editText: EditText) {
Timer("Timer", false).schedule(50) {
requireActivity().runOnUiThread(java.lang.Runnable {
editText.isFocusableInTouchMode = true
editText.requestFocus()
val manager =
requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
manager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
})
}
The timer delay makes sure that the focus request is working and the keyboard is opened manually because it did not work implicitely for me.
For some special cases in my code I do this:
later {
showKeyboard()
titleEdit.requestFocus()
}
In normal code it should look like this:
view.post {
(view.context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager)
.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
InputMethodManager.HIDE_IMPLICIT_ONLY)
titleEdit.requestFocus()
}
Explanation: Looks like in some cases toggleSoftInput is the only way but somehow it steal focus and it works in some case just in code tun after things gets initialized so has to be post for later execution.
Make sure that your view is focusable if not then set it to focusable:
yourView.setFocusable(true);
yourView.setFocusableInTouchMode(true);
After that set focus as follows:
yourView.requestFocus()
If that doesn't work then use this:
yourView.post(new Runnable() {
#Override
public void run() {
yourView.requestFocus();
}
});
This will work, the reason behind this is the whole UI isn't loaded in order to set the focus on a specific UI element. In this cases, the focus can be overwritten by the system that might ignore your explicit request.
So if you request the focus using a background thread (yes, I’m meaning using Thread or Runnable class) it should do the trick.
For more info visit this link:
https://medium.com/#saishaddai/til-requestfocus-not-working-22760b417cf9
It's working for request focus when sometime request focus not working
fun EditText.focus(activity: Activity) {
this.isFocusable = true
this.isFocusableInTouchMode = true
this.visibility = View.VISIBLE
this.isEnabled = true
this.isCursorVisible = true
this.post {
activity?.runOnUiThread {
this.requestFocus()
val manager =
this.context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
manager?.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
}
}