Reputation: 1667
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);
Upvotes: 38
Views: 71498
Reputation: 526
Well if the keyboard is not appearing even by editText.requestFocus()
that means the window is not focused, make sure that the window is focused
Example for a dialog box, make sure you didn't add this flag to the dialog object, remove these lines if you have it
cdd.getWindow().
setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
and add this
cdd.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
and then editTextName.requestFocus();
will work well.
Upvotes: 0
Reputation: 1
This code solved my problem. All you need is to write onFocusChange() of the editText with showSoftInput() inside in onCreate() like this:
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity_layout);
editText = findViewById(R.id.editText);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
}
});
}
public void buttonClick(EditText editText) {
editText.requestFocus();
}
Upvotes: 0
Reputation: 444
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)
}
}
Upvotes: 0
Reputation: 3189
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
Upvotes: 0
Reputation: 6373
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.
Upvotes: 0
Reputation: 1921
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)
}
}
}
Upvotes: 3
Reputation: 87
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.
Upvotes: 0
Reputation: 547
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
Upvotes: 0
Reputation: 2615
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();
Upvotes: 0
Reputation: 9508
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() }
Upvotes: 13
Reputation: 4392
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)
}
Upvotes: 5
Reputation: 509
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);
}
});
Upvotes: 5
Reputation: 374
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.
Upvotes: 4
Reputation: 2532
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
Upvotes: 21
Reputation: 3304
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.
Upvotes: 57
Reputation: 6332
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);
}
Upvotes: 9
Reputation: 1873
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);
Upvotes: 8