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);
}
Related
In my application when i go from one activity to another soft keyboard automatically pops up.
i have one activity(Say A) on which i have set
android:configChanges="keyboardHidden"
because i don't want keyboard on this activity but when i move from this activity to another activity(say B) which contains Map and AutoComompleteTextView, keyboard first automatically pops up and then close down.
what i have tried on activity B:
In manifest i have set
android:windowSoftInputMode="stateHidden|adjustResize"
in oncreate
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
i also tried putting this in OnCreate
try{
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}catch (Exception e)
{
Log.e(TAG, "onCreate: keyboard crash");
e.printStackTrace();
}
i also tried to set focus on another view in activity like(View v1)
v1.requestFoucs();
i even tried putting
android:focusableInTouchMode="true"
on each and every component on activity B.
but nothing worked for me.
please help me to solve this problem
i have already tried all the accepted ans that belongs to list of links below:
OnScreen keyboard opens automatically when Activity starts
Automatic popping up keyboard on start Activity
How to avoid automatically appear android keyboard when activity start
this is my AutoComompleteTextView
<AutoCompleteTextView
android:id="#+id/auto_serviceArea"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_weight=".5"
android:background="#android:color/transparent"
android:cursorVisible="false"
android:hint="#string/serviceArea"
android:padding="5dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"/>
Edit 1: I tried to check that which view is getting focus so i can shift that focus, and while debugging i removed focus from AutoCompleteTextView but still keyboard appears and gone when activity starts.
So this is not an Autocomplete focus problem.
If you have tried everything that comes as an accepted ans according to your links for the ques, then why don't you try debugging your start activity, i mean on which you have put intent to start the respective activity.
While debugging one of my application i found that android soft keyboard has that problem of not going down even after finishing the activity that calls it, it stays on screen for few seconds but this doesn't happen frequently.
So i suggest you to debug your calling activity too just try putting "focusable=false" on the component from which you called the respective activity.
Simply what you need to do is to give
android:windowSoftInputMode="stateHidden"
in Manifest file of your Activity.
Write below line inside your main xml tag
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
just as below
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
Use these lines in java file:
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
I am creating an app which contains multiple activities. A number of my activities have an 'EditText' field. As soon as I enter these activities, the keyboard instantly pops up assuming I want to type something straight away.
Does anyone have a simple code I can add into my java file that will prevent the keyboard to pop up by default because there is an 'EditText' field.
If you can also specify where to place the line of code such as whether it goes in the onCreate method etc will be appreciated.
I'm assuming the following will work, but where do I need to place it?
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
);
The above code can be placed in the onCreate method.
p.s I figured this out after some trial and error, hope it helps others
There are multiple answers for this.
You can add this to your menifest file.
<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
OR
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
OR
You can call this method in your onCreate
/**
* Hides the soft keyboard
*/
public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
I am calling a Dialog in an OnClick function in the OnCreate. In that dialog the user of the app can add something to a external database.
Now my problem is that when the dialog is gone ( myDialog.dismiss() ) and it is returning to the actual activity, an EditText is getting focussed. My whole screen contains Spinners and TextViews, except for only 1 EditText.
The weird thing is that when the activity is first called, it stats on top of the activity (showing the first Spinner and not foucssing the EditText), but when the dialog is dismissed, EditText is focussed.
I've got android:windowSoftInputMode="adjustResize|stateHidden" in the manifest within the activity tags
And i have tryed to put android:focusableInTouchMode="true" in the EditText in the XML too, but that didn't work.
Can anyone help me find what i'm looking for?
Thanks in advance!
Android will focus the first focusable View from the top so you can try set something else focusable :)
Android focus is somewhat random, you can't really control it. You could try to disable the textview and enable it again when the dialog is gone, but I would advice against it.
To be honest, just let Android do it's thing. We have tried to work against the focus logic in a really big app and gave up after a lot of tries and convinced the customer it's not worth the hassle.
I've been having this same problem. Try:
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
hideSoftKeyboard();
}
});
private void hideSoftKeyboard() {
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
This worked for everything except for a button in my dialog that has only an
onDismiss() method. Adding focusable=true and focusableInTouchMode=true to a TextView above my EditText (as Jeppe Andersen mentioned) solved this issue. Hope this helps.
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);
Hi I wrapped edittext control onto a control that is being displayed on the screen at users request. It overlays the whole screen until user presses 'done' button on the keyboard.
I am not able to explicitly show the control on the screen. only when user taps into control only then its shown. Am I missing something?
I even try this and it does not brin it up when I launch the overlay that Edit Text exists on:
customCOntrol.showKeyboard();
public void showKeyboard()
{
InputMethodManager imm = (InputMethodManager)_context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(this._textView.getWindowToken(), InputMethodManager.SHOW_IMPLICIT);
}
here is the settig I have on the screen itself in the config file android:windowSoftInputMode="stateHidden|adjustPan"
Thank you in advance
In your showKeyboard function you are calling:
imm.hideSoftInputFromWindow(this._textView.getWindowToken(), InputMethodManager.SHOW_IMPLICIT);
This will hide the softInput keyboard from the window!
Do you want to show the keyboard? If yes then would you use:
imm.showSoftInput(view, flags, resultReceiver);
EDIT: I think you can also toggle the keyboard from the InputMethodManager, try:
imm.toggleSoftInput(0, 0);
#dropsOfJupiter
You can do: editText.requestFocus() as you launch the Activity or Fragment containing your EditText reference. This will give the focus to the EditText and will bring uo the SoftKeyboard.
I hope this helps.
PROBLEM:
I faced with this keyboard not showing up problem. I wrote the following solution inspired by this answer but not their solution! It works fine. In short the reason for this mess is that the request focus and the IMM provided service can only run on a view that is created and active. When you do all these on the creation phase onCreate(Bundle savedInstance).. or onCreateView(LayoutInflater inflater... and the view is still in initializing state, you won't get an active view to act on! I have seen many solutions using delays and checks to wait for that view to get active then do the show keyboard but here is my solution based on the android frame work design:
SOLUTION:
in your activity or fragment override the following make sure your view has the access (define it in the top of the activity/fragment):
#Override
public void onStart() {
yourView.requestFocus();
showSoftKeyboard(yourView);
super.onStart();
}
public void showSoftKeyboard(View view) {
if(view.requestFocus()){
InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view,InputMethodManager.SHOW_IMPLICIT);
}
}