Having Trouble with Soft Keyboard - android

Hi Everyone iam new to Android and stuck in really silly problem in my project i have one EditText which is defined in Header View of a List View whenever users touches the EditText soft keyboard will be displayed. i have something called clear button which clears the EditText After clearing soft keyboard is not displaying in 2.3 devices.Whenever i press lock/power button lock the device and unlock then soft keyboard is displaying
<activity
android:name=".pl.Mobile.AddJobNew"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape"
android:windowSoftInputMode="stateVisible|adjustResize|adjustPan"
>
Here is My Activity Declaration in Android for android:windowSoftInputMode : 'adjustResize' option working perfectly for Android 2.3 devices but failed in 4.0 devices where soft keyboard overlapping over edit text . option 'adjustPan' is workin good in 4.0 devices but failed in 2.3 devices
Please help me get out of this problem.
Thanks in Advance

Try this in the activity with the edit text:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){
// Do something for 4.0 and above versions
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}
else{
// do something for phones running an SDK before 4.0
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
Also remove,
android:windowSoftInputMode="stateVisible|adjustResize|adjustPan"
From your activity in the manifest file.
Build.Version

I think that the problem is that when you click the clear button, the editText looses its focus so the keyboard hides itself.
I would try 2 things to solve it;
after you clean the text, do:
yourEditTextField.requestFocus()
manually control the keyboard visibility:
public void showKeyboard(){
View view = getWindow().getCurrentFocus();
if (view==null)
return;
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
}
public void hideKeyboard(){
View view = getWindow().getCurrentFocus();
if (view==null)
return;
IBinder binder = view.getWindowToken();
if (binder == null)
return;
// prevent a bug in some keyboards that makes the text hang on the screen
if (view instanceof EditText)
((EditText)view).setText(((EditText)view).getText().toString());
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(binder, InputMethodManager.HIDE_NOT_ALWAYS);
}

I have below line in My AndroidManifest.xml file and it works great. Please try this code. Dont put any extra code. And still if you not get any success then let me know. I will like to help you out.
<activity android:name=".pl.Mobile.AddJobNew" android:screenOrientation="portrait" android:configChanges="keyboard|orientation" android:windowSoftInputMode="adjustPan"/>
Try with above code for your activity.

Try something like that
editext1.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
if (v instanceof EditText)
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
v.requestFocusFromTouch();
return true;
}
return false;
}
});
If it not work for you try to add
editext1.setOnFocusChangeListener(new OnFocusChangeListener()
{
public void onFocusChange(View v, boolean hasFocus)
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
});
Also is good to remove request focus from layout. and maybe set this android:windowSoftInputMode="stateHidden|stateAlwaysHidden" in manifest on activity

Related

how to bring back soft keyboard after hiding using InputMethodManager

I'm new to Android Programming so was just making a simple app.
In the app I have an EditText component which slides up as the keyboard pops up and i don't want it to slide up so i searched for it and got an work around of using,
In OnCreate method
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
But the issue was after using this line the EditText was in place after clicking on enter the keyboard didn't went away.So, I search for it and got this method for hiding the keyboard
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
activity.getCurrentFocus().getWindowToken(), 0);
But now the issue is the keyboard is hiding successfully but is not visible after i re click on EditText.So, I was searching on the net but no luck and started started searching methods in Android Studio for making the keyboard visible and figured out some what this
public static void showSoftKeyboard(Activity activity){
InputMethodManager inputMethodManager1 =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager1.showSoftInputFromInputMethod(
activity.getCurrentFocus().getWindowToken(), 0);
But it's not working is throwing NullPointerException.
So please can anyone help me on this.
And also is it there any alternative for keeping the EditText on its position without sliding up. So that there is no need of applying this hide and show method and if not can you please tell me how to bring back the keyboard.
Comment your logic, and please try with below approach,
Write below line in your activity tag in Android Manifest like below
<activity android:name=".yourActivityName"
android:windowSoftInputMode="adjustPan">
</activity>
Then add this line to your edittext xml in your layout
android:imeOptions="actionDone"
like below
<EditText
android:id="#+id/edt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edittext"
android:singleLine="true"
android:maxLines="1"
android:imeOptions="actionDone"/>
Or set it from your code
yourEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);
On click on done button in soft keyboard, it will be automatically close.
And below is the code of click event of Soft Keyboard done button.
yourEditText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
// write your code here
}
return false;
}
});
Try this
public void showSoftKeyboard(Context context, View view){
if(view.requestFocus()){
InputMethodManager imm =(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
or
public void showSoftKeyboard(Context context){
if(context == null) return;
InputMethodManager imm =(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}

EditText Loses Focus Second Time

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>

showing softkeyboard in android

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);

How to hide Soft Keyboard when activity starts

I have an Edittext with android:windowSoftInputMode="stateVisible" in Manifest. Now the keyboard will be shown when I start the activity. How to hide it? I cannot use android:windowSoftInputMode="stateHidden because when keyboard is visible then minimize the app and resume it the keyboard should be visible.
I tried with
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
but it did not work.
In the AndroidManifest.xml:
<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
or try
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)‌​;
Please check this also
Use the following functions to show/hide the keyboard:
/**
* Hides the soft keyboard
*/
public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
/**
* Shows the soft keyboard
*/
public void showSoftKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
view.requestFocus();
inputMethodManager.showSoftInput(view, 0);
}
Just add two attributes to the parent view of editText.
android:focusable="true"
android:focusableInTouchMode="true"
Put this in the manifest inside the Activity tag
android:windowSoftInputMode="stateHidden"
Try this:
<activity
...
android:windowSoftInputMode="stateHidden|adjustResize"
...
>
Look at this one for more details.
To hide the softkeyboard at the time of New Activity start or onCreate(),onStart() etc. you can use the code below:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Using AndroidManifest.xml
<activity android:name=".YourActivityName"
android:windowSoftInputMode="stateHidden"
/>
Using Java
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
using the above solution keyboard hide but edittext from taking focus when activiy is created, but grab it when you touch them using:
add in your EditText
<EditText
android:focusable="false" />
also add listener of your EditText
youredittext.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.setFocusable(true);
v.setFocusableInTouchMode(true);
return false;
}});
Add the following text to your xml file.
<!--Dummy layout that gain focus -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical" >
</LinearLayout>
If you don't want to use xml, make a Kotlin Extension to hide keyboard
// In onResume, call this
myView.hideKeyboard()
fun View.hideKeyboard() {
val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}
Alternatives based on use case:
fun Fragment.hideKeyboard() {
view?.let { activity?.hideKeyboard(it) }
}
fun Activity.hideKeyboard() {
// Calls Context.hideKeyboard
hideKeyboard(currentFocus ?: View(this))
}
fun Context.hideKeyboard(view: View) {
view.hideKeyboard()
}
How to show soft keyboard
fun Context.showKeyboard() { // Or View.showKeyboard()
val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.toggleSoftInput(SHOW_FORCED, HIDE_IMPLICIT_ONLY)
}
Simpler method when simultaneously requesting focus on an edittext
myEdittext.focus()
fun View.focus() {
requestFocus()
showKeyboard()
}
Bonus simplification:
Remove requirement for ever using getSystemService: Splitties Library
// Simplifies above solution to just
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
I hope this will work, I tried a lot of methods but this one worked for me in fragments. just put this line in onCreateview/init.
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
add in your activity in manifasts
this property
android:windowSoftInputMode="stateHidden"
Put this code your java file and pass the argument for object on edittext,
private void setHideSoftKeyboard(EditText editText){
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
To hide the softkeyboard at the time of New Activity start or onCreate(),onStart() method etc. use the code below:
getActivity().getWindow().setSoftInputMode(WindowManager.
LayoutParams.SOFT_INPUT_STATE_HIDDEN);
To hide softkeyboard at the time of Button is click in activity:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
assert imm != null;
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Use SOFT_INPUT_STATE_ALWAYS_HIDDEN instead of SOFT_INPUT_STATE_HIDDEN
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Above answers are also correct. I just want to give a brief that there's two ways to hide the keyboard when starting the activity, from manifest.xml.
eg:
<activity
..........
android:windowSoftInputMode="stateHidden"
..........
/>
The above way always hide it when entering the activity.
or
<activity
..........
android:windowSoftInputMode="stateUnchanged"
..........
/>
This one says don't change it (e.g. don't show it if it isn't already shown, but if it was open when entering the activity, leave it open).
You can set config on AndroidManifest.xml
Example:
<activity
android:name="Activity"
android:configChanges="orientation|keyboardHidden"
android:theme="#*android:style/Theme.NoTitleBar"
android:launchMode="singleTop"
android:windowSoftInputMode="stateHidden"/>
Use the following code to Hide the softkeyboard first time when you start the Activity
getActivity().getWindow().setSoftInputMode(WindowManager.
LayoutParams.SOFT_INPUT_STATE_HIDDEN);
This is what I did:
yourEditText.setCursorVisible(false); //This code is used when you do not want the cursor to be visible at startup
yourEditText.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event); // handle the event first
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0); // hide the soft keyboard
yourEditText.setCursorVisible(true); //This is to display cursor when upon onTouch of Edittext
}
return true;
}
});
Try this one also
Ed_Cat_Search = (EditText) findViewById(R.id.editText_Searc_Categories);
Ed_Cat_Search.setInputType(InputType.TYPE_NULL);
Ed_Cat_Search.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Ed_Cat_Search.setInputType(InputType.TYPE_CLASS_TEXT);
Ed_Cat_Search.onTouchEvent(event); // call native handler
return true; // consume touch even
}
});
If your application is targeting Android API Level 21 or more than there is a default method available.
editTextObj.setShowSoftInputOnFocus(false);
Make sure you have set below code in EditText XML tag.
<EditText
....
android:enabled="true"
android:focusable="true" />
Try this.
First in your searchable xml the fields (name and hint etc) put #string and not literal strings.
Then method onCreateOptionsMenu, it must have a ComponentName object with your package name and your completed class name (with package name) - In case activity which has the SearchView component is the same as the show search results use getComponentName(), as the google android developer says.
I tried a lot of solutions and after much,much work this solution works for me.
Ed_Cat_Search = (EditText) findViewById(R.id.editText_Searc_Categories);
Ed_Cat_Search.setInputType(InputType.TYPE_NULL);
Ed_Cat_Search.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Ed_Cat_Search.setInputType(InputType.TYPE_CLASS_TEXT);
Ed_Cat_Search.onTouchEvent(event); // call native handler
return true; // consume touch even
}
});
this one worked for me
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
it will works

how to hide soft key pad after changing one viewpager to another view pager

hi guys how to hide soft keypad when am changing the one view pager to another view pager
the problem is in view pager having four tabs,first tab having search option when i click search edit text then after i click another tab.soft key pad is visible in next tab.
how to reslove this problem, i tryed this way it's not working
((InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE))
.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
help
Just add this attribute in your AndroidManifest under activity tag :
<activity
android:name="com.example.myApp.MyActivity"
android:windowSoftInputMode="stateHidden" >
</activity>
Try following code.
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(any_focusable_view_reference.getWindowToken(), 0);
Here any_focusable_view_reference means EditText or such which has focus.
just added setOnFocusChangeListener to Edittext , then it working fine .
EditText editTextProfileName = (EditText) view
.findViewById(R.id.nameEditText);
editTextProfileName.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard();
}
}
private void hideKeyboard() {
if (editTextProfileName != null) {
InputMethodManager imanager = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imanager.hideSoftInputFromWindow(editTextProfileName.getWindowToken(), 0);
}
}
});
Thanks

Categories

Resources