This question already has answers here:
How to show soft-keyboard when edittext is focused
(48 answers)
Closed 9 years ago.
I have an EditText that I am passing focus to programmatically. But when I do, I want the keyboard to show up as well (and then go down when that EditText lose focus). Right now, the user has to click on the EditText to get the keyboard to show up -- even thought the EditText already has focus.
<activity android:name=".YourActivity"
android:windowSoftInputMode="stateVisible" />
Add this to manifest file...
This is how I show the ketyboard:
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
set this for your activity in your manifest to pop keyboard automatically when your screen comes containing EditText box
<activity android:windowSoftInputMode="stateAlwaysVisible" ... />
To hide keyboard on losing focus set a OnFocusChangeListener for the EditText .
In the onCreate()
EditText editText = (EditText) findViewById(R.id.textbox);
OnFocusChangeListener ofcListener = new MyFocusChangeListener();
editText.setOnFocusChangeListener(ofcListener);
Add this class
private class MyFocusChangeListener implements OnFocusChangeListener {
public void onFocusChange(View v, boolean hasFocus){
if(v.getId() == R.id.textbox && !hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
}
To show the keyboard, use the following code.
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
To hide the keyboard,, use the code below.
et is the reference to the EditText
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
In order to do it based on focus listener you should go for:
final InputMethodManager imm =(InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
}else{
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
}
imm.toggleSoftInput(0, 0);
}
});
Hope this helps.
Regards!
Related
I am creating a Fragment inside an Activity.I have an EditText in the MainActivity layout and the Fragment layout comes under that EditText.My issue is when I click the EditText the soft keyboard is not showing.
In EditText layout I create
<EditText
android:id="#+id/edt_searchContact"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textCursorDrawable="#drawable/color_cursor"
android:background="#drawable/edit_text_line_contacts"
android:focusable="true"
android:clickable="true"
android:hint="Search..."/>
<requestFocus/>
for showing the soft keyboard the code that I given in the Fragment
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edt_searchContact, InputMethodManager.SHOW_IMPLICIT);
In Manifest
android:windowSoftInputMode="adjustPan"
I have 3 Fragment in one Activity,when one Fragment comes that EditText is going to Visible in main Activity layout.
Can anyone please help me
Thanks in advance :)
Try this in your activity
editText = (EditText)findViewById(R.id.txt_yourName);
// Request focus and show soft keyboard automatically
editText.requestFocus();
getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
On your Activity's onResume() Method you can write this:
EditText et = (EditText) findViewById(R.id.et);
et.requestFocus();
InputMethodManager mngr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mngr.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
Try this one :
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
editText.post(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edt_searchContact, InputMethodManager.SHOW_IMPLICIT);
}
});
}
});
editText.requestFocus();
The below method worked for me-
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
This question already has answers here:
Android: show soft keyboard automatically when focus is on an EditText
(33 answers)
Closed 8 years ago.
In my activity there is three editText view it will randomly gets hide at some time
After entering a single text in the editText the soft-keyboard will disappears automatically
Now I want to show the soft keyboard When editText get focused
How to do this?
Thanks in advance
Through InputMethodManager you can show and hide the soft keyboard. Use toggleSoftInput() method to show soft keyboard when EditText get focus and use hideSoftInputFromWindow() to hide the keyboard when EditText lost focus as below...
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean isFocused) {
if (isFocused) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
} else {
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
}
}
});
Try with these code..
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
Am stuck with a pretty simple issue in the Android App am coding. I have a list of EditText objects, one in each row.
When the user long presses the EditText, I need to show the keyboard. When the user does a long press, then I call this method :
private void setNameAsEditable(View rowView, boolean setToEditable) {
EditText textView = (EditText) rowView
.findViewById(R.id.edittext_name);
textView.setFocusableInTouchMode(setToEditable);
textView.setFocusable(setToEditable);
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(textView, InputMethodManager.SHOW_IMPLICIT);
}
The edittext becomes editable (the underline and the cursor appears, as you can see below)
but the keyboard does not come up.
I tried various solutions from stackoverflow (like this), but in vain.
I even tried
this.getWindow().setSoftInputMode ( WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
I want the soft-keyboard to come up as soon as the user long-presses the EditText. Can someone please help?
Try it, for me it works fine.
et =(EditText) findViewById(R.id.edittext_name);
imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
et.setText("hide key board");
et.setFocusable(false);
et.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
et.setFocusableInTouchMode(true);
imm.showSoftInput(et, 0);
et.setText("show key board long pressed");
return false;
}
});
This is what worked finally ( Amit's answer + Tamilan's answer) Thanks so much!
private void setNameAsEditable (View rowView, boolean setToEditable) {
EditText textView = (EditText) rowView
.findViewById(R.id.edittext_name);
textView.setFocusableInTouchMode(setToEditable);
textView.setFocusable(setToEditable);
InputMethodManager imm = (InputMethodManager) getContext().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(textView.getWindowToken(), 0);
if (setToEditable) {
textView.requestFocus();
imm.showSoftInput(textView, 0);
}
}
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
Check if you have android:focusable="false" or android:focusableInTouchMode="false" in your xml layout.
I created a keypad from buttons (so an xml with button 1, button 2, etc., but now I want an edit text to react the same as using the soft-keyboard, is there a way to imitate the keypad that comes with the device?
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
//show your custom keypad
}
}
});
Force softkeypad to appear.
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
Hi I am using four tabs in my tabhost, named TabOne, TabTwo, TabThree, TabFour.
In TabOne I have editext to make search option, when I pressed in edit text it show the keyboard.
But without closing the keyboard When I move to TabTwo the keyboard still showing. I dont want the keyboard should enable in TabTwo,TabThree,TabFour.
Because all other three tabs are not having edittext option.
Now My question is how to hide keyboard when clicked on the other tabs.
I have tried the below methods,
Adding the below code in oncreate method of TabB
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Adding the below xml tag in Android manifest file
android:windowSoftInputMode="stateAlwaysHidden"
Please guide me to solve this issue.
Hide edit text when it lose focus.
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard();
}
}
});
void hideKeyboard() {
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
Please look at this answer
enable the soft keyboard
inputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
and disable the soft keyboard
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
This worked for me:
In the Activity which is triggered when you click on the specific Tab, I used this in onCreate:
inputSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(inputSearch.getWindowToken(), 0);
}
}});
In the AndroidManifest.xml, I added the following to my MainActivity (Where all Tabs are defined):
android:windowSoftInputMode="stateAlwaysHidden"
this.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
This single line will work for you. check this out.