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);
Related
I want show keyboard when my EditText receives focus. I tried many methods but nothing not helped.
I tried:
1.
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
whith different flags.
2.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
<requestFocus />
4.
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(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
}
});
editText.requestFocus();
4 method is fork but it bad solution. Thus it is written here Show soft keyboard automatically when EditText receives focus
Before, I used the method 2 and it worked. but now no longer. and I created a blank Project and it does not work, none of the methods
UPDATE:
<style name="Theme.TransparencyDemo" parent="android:Theme.Light.NoTitleBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
You can add flags to your activity as well which will show the keyboard automatically
<activity name="package.ActivityName" android:windowSoftInputMode="stateVisible"/>
this is mostly useful if you expect the focus to be applied when the activity launches
Also you can use in Fragment:
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
or in Activity
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Use WindowManager instead of InputMehtodManager inside onFocusChange listener of edittext, As Its reliable.
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
} });
I have-
EditText &
Button
How can I open a soft keyboard when one selects the edittext view, the user can write anything and it should display in the edittext.
I used the below code but the keyboard doesn't come up-
EditText edit= (EditText) findViewById(R.id.list);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edit, InputMethodManager.SHOW_IMPLICIT);
Any idea how I can open the keyboard and close it again by using back button?
This is my function to open the keyboard :
protected void showKeyBoard(final View v){
v.post(new Runnable() {
public void run() {
v.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
}
});
}
Make sure
android:windowSoftInputMode="stateAlwaysVisible" in manifest File.
and
edittext.requestFocus();
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!
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.
Thanks for reading.
I am facing a strange problem: My app behavior is such that when the Activity starts, I requestFocus() on an EditText and show the soft keyboard.
However, when I press the back button to dismiss the keyboard and tap the EditText I don't get the keyboard to pop up ever again. Only way out is to the start the Activity again.
Here's what my code looks like:
EditText editText = (EditText) findViewById(R.id.editText);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null) {
imm.toggleSoftInput(0, 0);
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
}
And here's my XML:
<EditText android:id="#+id/editText"
android:layout_width="wrap_content"
android:imeOptions="actionSearch" android:hint="Test Hint"
android:layout_height="wrap_content" android:layout_centerHorizontal="true"
android:maxLength="30">
</EditText>
Any help would be greatly appreciated!
Thanks!
Try to open and hide inside a Runnable as,
TO OPEN
ettext.requestFocus();
ettext.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(ettext, 0);
}
},200);
TO CLOSE
ettext.requestFocus();
ettext.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(ettext.
getWindowToken(), 0);
}
},200);
You used the wrong view for showing the input window.
EditText editText = (EditText) findViewById(R.id.editText);
editText.requestFocus();
InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null) {
imm.showSoftInput(editText, 0);
}
try this:
final InputMethodManager imm = (InputMethodManager)EnterWordsActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null)
{
imm.toggleSoftInput(YOUE_EDTITE_TEXT.SHOW_FORCED,1);
}
None of above work until i clear the forces textField.clearFocus(); before requesting focus, so my final code in onResume looks like this.
#Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume: ");
resumed = true;
textField.postDelayed(new Runnable() {
#Override
public void run() {
textField.clearFocus();
textField.requestFocus();
if (!editMode)
textField.getText().clear();
inputMathodType = SharedPref.read(SharedPref.KEY_INPUT_MATHOD_SHARED_PREF, -1);
setInputMethod();
}
}, 200);
}
Used this coding in your Activity,It will hide your keyboard this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
I faced the same problems for days. Solutions provided with creating threads and delays in the code are not efficient. The framework has solution. you can check my short solution here: https://stackoverflow.com/a/74442682/6906562