How to make soft keyboard static? - android

I'm trying to force the soft keyboard not to close down after I press enter in the EditText in my activity. What do I need to do so that the EditText will always be on focus and the soft keyboard will stay put even after pressing enter?
I used already in the manifest android:windowSoftInputMode="stateAlwaysVisible"
I also tried to use methods such as:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(((EditText) findViewById(R.id.your_view)),InputMethodManager.SHOW_FORCED);
EditText.requestFocus();
but it didn't change anything...
also, how can I make the EditText focused right when the activity opens up?
Thanks a lot!

Just call this inside onCreate method of your activity. It will show the keyboard right when the activity shows up. And if you keep clicking enter , it will keep creating new line on your edit text and not close the keyboard, however the backbutton on the actual device will still close it
In Kotlin :-
window.setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
)
and if in case you have limited the edittext to one line only ... add the following code in the onCreate method below the above code
kotlin :-
editText.setOnEditorActionListener { _, actionId, event ->
(actionId == EditorInfo.IME_ACTION_DONE) || ((event.keyCode == KeyEvent.KEYCODE_ENTER) && (event.action == KeyEvent.ACTION_DOWN))
}

There is a method of EditText, named requestFocus() to get focus on the EditText
For Example, inside onCreate() of activity
EditText date_birthday = findViewById(R.id.date_birthday);
date_birthday.requestFocus();

Related

How to hide the keyboard and update EditTextPreference programmatically?

I am trying to implement the settings activity according to the guidelines. I have an EditTextPreference that I want to keep, but instead of allowing the user to type in any value, the value should come through Bluetooth (i have the Bluetooth part ready).
At the moment, when the EditTextPreference is clicked, it shows the editor popup with okay and cancel buttons. That's how I want it to be, so I can handle the OK or Cancel click events.
1) The first problem is that the keyboard also shows up - I don't want that, because the value should come from the background. I've added these properties, but nothing seems to have any effect on hiding the keyboard(even if I switch them around):
<EditTextPreference
android:selectable="true"
android:enabled="true"
android:editable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:cursorVisible="false"
android:capitalize="words"
android:inputType="none"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="#string/pref_title_id" />
2) The second problem is: how do I update the value of the EditTextPreference from the code behind, so the user doesn't have to type in anything, but just to see the value and click okay or cancel?
3) Third problem / question: is it okay to save the values in a database instead of using the shared preferences? Basically, I want to have the common settings UI, but keep the values in a database.
I hope that someone has had the same issues as me, because I was unable to find any solutions on the internet.
1.When you click the edit box, you should call this method, that hides the keyboard.
private void hideKeyboard() {
View view = getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
For setting a value to the editText use the method:
editTextPreferences.setText("Your value");
For saving just a value, you can use Shared Preference because its more flexible, but if you want to save more data and to have all in a db you can use SQLite db. Both them save local values,because when app is uninstall, they are deleted.
For updating EditTextPreference check this one
EditTextPreference.setText(value) not updating as expected
For disable Input =>
setFocusableInTouchMode(boolean)
setFocusable(boolean)
Try following code in your activity's onCreate method to make the keyboard only pops up when a user clicks an EditText.
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
or
use android:windowSoftInputMode="stateHidden" in the Android Mainfest.xml under activity tag
To Hide a Keybord call this method in your onClick event
private void hideKeyboard() {
View view = getCurrentFocus();
if (view != null) {
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).
hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}

Can't hide keyboard in MvxFragment

I am developing Xamarin Android Application.I am using edit text in MvxFragment but when I type something in EditText and then go to another fragment ,keyboard is still open and can't be closed or hidden untill I press return key on keyboard.So how can I able to close or hide that keyboard ?
You need to call something like this when closing your fragment:
public void HideSoftKeyboard()
{
InputMethodManager manager = (InputMethodManager)GetSystemService(Context.InputMethodService);
manager.HideSoftInputFromWindow(CurrentFocus.WindowToken, 0);
}
Add this to your activity, and call it from the fragment.

How to close soft keyboard in fragment

I have an EditText inside a fragment, which is in itself inside an actionbarsherlock tab. When I touch inside the EditText box a soft keyboard appears with one of the keys having a magnifying glass (search) icon. When I type some text and click on the search key I can process the typed-in-string in my onEditorAction, but the soft keyboard remains on display. How can I close it programatically?
By the way if one answer is that I could configure some setting for EditText such that it closes automatically on search, I would still like to know if the soft keyboard can be closed with a method call as I also have my own search button on screen (nothing to do with the soft keyboard) and I would like the soft keyboard to close when that's pressed too.
Note: Before anyone rushes to claim this question is a repeat of a previous question, I have seen many Q&A's about hiding the soft keyboard at various points. Many of the answers seem inordinately complicated and in many it is not clear whether the idea is to permanently hide the keyboard or just just temporarily close it till the user taps on an EditText field again. Also some answers require calls to methods not available in fragments.
In my fragments I close the keyboard simply in this way:
public static void closeKeyboard(Context c, IBinder windowToken) {
InputMethodManager mgr = (InputMethodManager) c.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(windowToken, 0);
}
closeKeyboard(getActivity(), yourEditText.getWindowToken());
This is working code to hide soft keyboard for android.
try {
InputMethodManager input = (InputMethodManager) activity
.getSystemService(Activity.INPUT_METHOD_SERVICE);
input.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}catch(Exception e) {
e.printStackTrace();
}
I'm using this code in a fragment
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(text.getWindowToken(), 0);
when I click on an action bar icon and it's working, I don't see why it shouldn't work in your case (maybe I misunderstood the question).
You can check my answer here. It was the only way that worked for me inside fragment.
A clear way to close keyboard and clearfocus of an EditText inside a fragment, is to make sure that your EditText XML has :
android:id="#+id/myEditText"
android:imeOptions="actionDone"
Then set listener to your EditText (with Kotlin, and from a fragment):
myEditText.setOnEditorActionListener({ v, actionId, event ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
myEditText.clearFocus()
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view!!.windowToken, 0)
}
false
})
Working in Fragment
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

AnySoftKeyboad stays with garbage data after I hide it

I have a problem which occurs only when I use AnySoftKeyboard.
I'm trying to show/hide the keyboard according to EditText focus.
I used the methods I found in this post
When I'm hiding the keyboard, there is a strange behavior -
When I rotate the screen, the text that was in the EditText is doubled.
I thought that it has to do with the onCreate method, but I can see the it happens also when I click "back" (Finish()). I see it for a split second before the Activity is closed.
When I start a new activity, (ActivityB from ActivityA) then clicking "Back" once doesn't do anything (probably "closing" the invisible keyboard).
When I click "back" again, ActivityB closes but I can see for a split second the text from ActivityA keyboard in a big font across the screen (like a 100 millisecond pop-up )
Does anyone has an idea how to deal with it?
Apparently it is a bug in AnySoftKeyboard.
I didn't happen when I use other keyboards.
I solved it by doing setText to the EditText view before hiding it - its probably resets some stuff on keyboard object.
Here is my code:
View view = getWindow().getCurrentFocus();
if (view==null)
return;
IBinder binder = view.getWindowToken();
if (binder == null)
return;
// I used this to fix the strange behaviour
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);
Surprisingly it works!
Try this:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

How do I prevent the software keyboard from popping up?

I have my own keypad in my application so I want to hide the software keyboard all the time(in specific activities & dialogs).
I experimented with two options:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
This code prevent the keyboard from popping up at the beginning, but when I click on the textbox the keyboard still pops.
InputMethodManager imm = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
This code hide the keyboard, but it doesn't prevent the keyboard from popping up.
PLEASE HELP!
Finally figured out!
I used
public void supressKeyboard() {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
for the activities where I want to suppress the keyboard(you can put it in a general activity where all other activities inherit from)
But this won't prevent the keyboard from popping up when you click on the EditText textbox. What I did is I consumed the onTouch event for the text box:
#Override
public boolean onTouchEvent(MotionEvent event) {
return true;
}

Categories

Resources