I have a Edit Text , and after user click on button or on soft keyboard input , i do some action in background and restart this activity again . At first time the focus is on EditText , but after redirecting the focus is lost .
One more thing , I am using a bluetooth device to enter text in the EditText area .
In the starting of activity , i am getting the editText button and calling
edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
also after calling action , i am again requesting focus .
In manifest i am using :
android:windowSoftInputMode="stateAlwaysVisible"
I have also tried to show keyboard always :
InputMethodManager mgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(mLastNameET, InputMethodManager.SHOW_IMPLICIT);
I have tried many things , please suggest how can i make it work ?
There are two ways to do this,
First is to declare EditText.request focus() in on resume(), or second one is declare <request focus /> tag between <EditText> <request focus/> <\EditText>
You can also use this thing:
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();
I had similar problem and it is fixed with adding android:windowSoftInputMode="adjustPan" in Manifest like below.
I hope it work for you too.
<activity
android:name=".MyActivity"
android:windowSoftInputMode="adjustPan">
</activity>
Related
I want window of my app to be resized when soft keyboard is launched. I thus added following tag to main activity in manifest.xml file
android:windowSoftInputMode="adjustResize"
however even at time of app lunch keyboard is displayed and window is resized.
I want that at launch atleast keyboard not to be displayed automatically and window to be resized. only when some one is type should the window resizing should occur.
any guidance
just to further add I have editText which gets the control at time of launch. before adding android:windowSoftInputMode="adjustResize" , complete screen use to get displayed and when I touched to type was keyboard displayed. I want the same behavior, that is keyboard should not to be displayed by default but displayed and window resized when someone touches to type
You can set the Activity Attribute by adding this to your Manifest File here
Like so, but haven't tried it.
android:windowSoftInputMode
<activity android:windowSoftInputMode="stateHidden|adjustResize" . . . >
How the main window of the activity interacts with the window containing the on-screen soft keyboard.
You can also refer here
Do below changes to your code
To hide the keyboard on activity launch add below code
public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
To show keyboard when edittext gets focused, use below code
public void showSoftKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
view.requestFocus();
inputMethodManager.showSoftInput(view, 0);
}
Call hideSoftKeyboard() in your onCreate method.
Once edittext gets focused use below code.
EditText editText = (EditText)findViewById(R.id.editText);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
//Toast.makeText(getApplicationContext(),"Focused",Toast.LENGTH_SHORT).show();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); // Resizing window programatically
showSoftKeyboard(view);
}
});
I have an EditText with three toggle buttons beneath it.
I want to keep the focus on the EditText AND have the keyboard stay visible when I tap on any of the three toggles. i.e. I do not want the keyboard to hide when the focus is outside the EditText (I should not see the keyboard hide then reopen).
I've tried the following to no avail:
toggleButton.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
editText.requestFocus();
// This doesn't fully work.
// Focus is on editText but keyboard still hides when I
// tap on the toggle button.
}
});
The EditText and ToggleButtons are in a fragment, and the parent activity has this configuration in the AndroidManifest.
<activity
android:name=".activities.MyActivity"
android:label="#string/m_activity"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden|adjustResize" />
What is the best way to fix this issue?
I think you should do that for your yourEditText, using OnFocusChangeListener
yourEditText.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
yourEditText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
}
});
This means that, you will request focus whenever it is changed for you yourEditText and you will also show keyboard.
You can use the LayoutParms.Example is given below
1.Hide the keyboard
this.getWindow().setSoftInputMode
(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
2.Show the Keyboard
this.getWindow().setSoftInputMode
(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
If you want to Know the other options. Refer the below link.
https://developer.android.com/reference/android/view/WindowManager.LayoutParams.
html
I want to open the soft keyboard in android automatically after opening the activity . I used the code mentioned here
Close/hide the Android Soft Keyboard
but it doesn't work.
This is my code.
final EditText txtName = (EditText) findViewById(R.id.etxt_hidenprac);
txtName.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
txtName.post(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) Practice.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(txtName, InputMethodManager.SHOW_IMPLICIT);
}
});
}
});
txtName.requestFocus();
<activity
android:name="com.example.moisun01.Practice"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:windowSoftInputMode="stateVisible">
</activity>
I found it why it doesn't work . its because my layout is in landscape orientation.
and in portrate orientation it works fine.
but my App is in landscape . is there any solution for this problem?
answer is already given in comment box... it worked for me..
You dont need to do too much...just Add
android:windowSoftInputMode="stateVisible"
in your activity tag in manifest file
In this way soft keyboard in android automatically opens as activity start
..if this wasn't happening to you..
then..try to give focus to first edittext usin requestFocus() method
In the Activiy onCreate() you can write like this.
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, 0);
or ,in the Manifest file you can use
android:windowSoftInputMode="stateVisible" under activity.
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(mEditText, 0);
or ,in the Manifest file you can use android:windowSoftInputMode="stateVisible" under activity.
Maybe try this:
onCreate method
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
I have multiple EditText on my screen and one of them is focussed. The softkeyboard does not trigger as soon as the screen pops up. I want the soft keyboard to trigger as soon as the screen pops up. It works well if I don't implement the onFocusChangeListener(). However I need the onFocusChangeListener() to detect which editText is focused. I have tried setting setFocusable(true) and setFocusableInTouchMode(true). Also i don't want to modify the android:windowSoftInputMode property in AndroidMenifest.xml. I have the following criteria :
onFocusChangeListener implemented (to detect which edittext is focused)
No modifications in AndroidMenifest.xml
Here is my code snippet
final InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
mInput.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
mIsFocused = hasFocus;
if(hasFocus)
inputMethodManager.showSoftInput(mInput, InputMethodManager.SHOW_IMPLICIT);
}
});
Any suggestions ?
You can open softkeyboard programmatically
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
You can try using inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0).
Source: Stackoverflow post
I have 2 editfields in my Custom Dialog which is called from ACtivity, among them 1 is of "trxtPassword" and other of "text" type. Keyboard doesn't appear in "text" type editbox, but just comes on "textPassword" edittext, and then doesn't go only.
I tried the following, but nothing works:
InputMethodManager inputManager = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//inputManager.hideSoftInputFromWindow(txt_username.getWindowToken(), 0);
//inputManager.hideSoftInputFromWindow(txt_password.getWindowToken(), 0);
If I make txt_password.setInputType(0); then others can see the password easily, so that can't be used.
What more can be doen to achieve the goal? I did to trap the onLostFocus on txt
txt_password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus == false) {
InputMethodManager inputManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(LoginDialog.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
});
But unfortunately, once entered, if I click anywhere else or any checkbox, then also the focus is not lost from the txt_password field. That is only lost if I click another editText and then the onFocusChange event is fired and throws error and the application shuts down.
Any idea how to accomplish this?
Use that to keep keyboard hidden on activity start
<activity
android:name=".views.DrugstoreEditView"
android:windowSoftInputMode="stateHidden"></activity>
And there is one usefull answer: How to hide soft keyboard on android after clicking outside EditText?