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);
Related
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 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);
}
I have a Fragment (the compatibility version) with an EditText in its layout. I'm using a ViewFlipper to flip between fragments. When I get to this particular Fragment, the soft keyboard opens up automatically. This is not what I want. Here is what I've tried to stop it or hide it.
Tried:
android:descendantFocusability="beforeDescendants"
on the fragment's main view
Tried:
android:windowSoftInputMode="stateHidden"
and
android:windowSoftInputMode="stateAlwaysHidden"
in the manifest for the activity
Tried:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mViewPager.getChildAt(position).getWindowToken(), 0);
on the OnPageChangeListener of my ViewPager
Tried:
InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(voucherView.findViewById(R.id.redeem_mobile_number).getWindowToken(), 0);
in onCreateView in my Fragment
Tried:
InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getView().findViewById(R.id.redeem_mobile_number).getWindowToken(), 0);
in onStart in my Fragment
Tried:
voucherView.findViewById(R.id.redeem_mobile_number).clearFocus();
in onCreateView in my Fragment
It seems to me like onPageChangeListener is the place to do this because the other calls happen before the soft keyboard is actually open. Any help would be great.
This post has a solution to the problem.
The answer was to add android:focusableInTouchMode="true" to the LinearLayout containing the EditText. Now it doesn't bring up the soft keyboard automatically.
<LinearLayout
android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
>
<EditText
android:id="#+id/retailer_search_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Just follow it . And EditText should be within a LinearLayout.
Have you tried this?
<activity
android:name=".YourActivityName"
android:configChanges="keyboardHidden|orientation"
android:windowSoftInputMode="stateHidden" />
EDIT:
try this (I now it is a bad one but give a try to this) :)
Thread splashTread = new Thread() {
#Override
public void run() {
try {
sleep(1);
} catch (InterruptedException e) {
// do nothing
} finally {
runOnUiThread(new Runnable() {
public void run() {
InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(youreditText.getWindowToken(), 0);
}
});
}
}
};
splashTread.start();
I had a soft keyboard disturbingly popup and pushing all views up when I click on an edit text in a fragment view (my app is a app of nested fragments - fragment in fragment)
I tried a dozen solutions here and on the internet, but nothing helped except this, which is edit the EditText itself in XML (not the view above/not the manifest/not overwride on create activities/not any other)
by adding android:focusableInTouchMode="false" line to your EditText's xml.
I borrowed the solution from ACengiz on this thread
how to block virtual keyboard while clicking on edittext in android?
Only 2 people voted for him? although for me it was a saver after hours of a headache
Add this to your activity tag in AndroidManifest.xml
keyboardHidden: will not let it open soft input keyboard automatically.
android:configChanges="orientation|keyboardHidden|screenSize"
android:windowSoftInputMode="stateAlwaysHidden|adjustResize|stateHidden"
This worked for me.
edittext.setInputType(InputType.TYPE_NULL);
if (android.os.Build.VERSION.SDK_INT >= 11)
{
edittext.setRawInputType(InputType.TYPE_CLASS_TEXT);
edittext.setTextIsSelectable(true);
}
Try This
exitText.setFocusableInTouchMode(true);
I got a relative simple question. I have an activity with a lot of EditText's in them. When I open the activity it automatically focusses to the first EditText and displays the virtual keyboard.
How can I prevent this?
Use this attributes in your layout tag in XML file:
android:focusable="true"
android:focusableInTouchMode="true"
As reported by other members in comments it doesn't works on ScrollView therefore you need to add these attributes to the main child of ScrollView.
You can add this to your Android Manifest activity:
android:windowSoftInputMode="stateHidden|adjustResize"
I have several implementations described here, but now i have added into the AndroidManifest.xml for my Activity the property:
android:windowSoftInputMode="stateAlwaysHidden"
I think this is the easy way even if you are using fragments.
"stateAlwaysHidden" The soft keyboard is always hidden when the
activity's main window has input focus.
If you have another view on your activity like a ListView, you can also do:
ListView.requestFocus();
in your onResume() to grab focus from the editText.
I know this question has been answered but just providing an alternative solution that worked for me :)
Use this in your Activity's code:
#Override
public void onCreate(Bundle savedInstanceState) {
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
https://stackoverflow.com/a/11627976/5217837 This is almost correct:
#Override
public void onCreate(Bundle savedInstanceState) {
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
But it should be SOFT_INPUT_STATE_HIDDEN rather than SOFT_INPUT_STATE_ALWAYS_VISIBLE
I had a simular problem, even when switching tabs the keyboard popped up automatically and stayed up, with Android 3.2.1 on a Tablet. Use the following method:
public void setEditTextFocus(EditText searchEditText, boolean isFocused)
{
searchEditText.setCursorVisible(isFocused);
searchEditText.setFocusable(isFocused);
searchEditText.setFocusableInTouchMode(isFocused);
if (isFocused) {
searchEditText.requestFocus();
} else {
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(searchEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS );
}
}
In the onCreate() and in the onPause() of the activity for each EditText:
setEditTextFocus(myEditText, false);
For each EditText an OnTouchListener:
myEditText.setOnTouchListener(new EditText.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
setEditTextFocus(myEditText, true);
return false;
}
});
For each EditText in the OnEditorActionListener:
myEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
.......
setEditTextFocus(myEditText, false);
return false;
}
});
And for each EditText in the layout xml:
android:imeOptions="actionDone"
android:inputType="numberDecimal|numberSigned" // Or something else
There is probably more code optimizing possible.
((InputMethodManager)getActivity().getSystemService("input_method")).hideSoftInputFromWindow(this.edittxt.getWindowToken(), 0);
I have found this simple solution that worked for me.Set these attributes in your parent layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
And now, when the activity starts this main layout will get focus by default.
Also, we can remove focus from child views at runtime by giving the focus to the main layout again, like this:
findViewById(R.id.mainLayout).requestFocus();
Hope it will work for you .
this is the solution I am using, is not the best solution but it's working well for me
editComment.setFocusableInTouchMode(false);
editComment.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event)
{
// TODO Auto-generated method stub
editComment.setFocusableInTouchMode(true);
editComment.requestFocus() ;
return false;
}});
Interestingly, this documentation https://developer.android.com/training/keyboard-input/visibility.html states that when an activity starts and focus is given to a text field, the soft keyboard is not shown (and then goes on to show you how to have the keyboard shown if you want to with some code).
On my Samsung Galaxy S5, this is how my app (with no manifest entry or specific code) works -- no soft keyboard. However on a Lollipop AVD, a soft keyboard is shown -- contravening the doc given above.
If you get this behavior when testing in an AVD, you might want to test on a real device to see what happens.
This has some good answers at the following post : Stop EditText from gaining focus at Activity startup. The one I regularly use is the following code by Morgan :
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="#+id/autotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:nextFocusUp="#id/autotext"
android:nextFocusLeft="#id/autotext"/>
NOTE : The dummy item has to be PLACED RIGHT BEFORE the focusable element.
And I think it should work perfectly even with ScrollView and haven't had any problems with accessibility either for this.
This occurs when your EditText automatically gets Focus as when you activity starts. So one easy and stable way to fix this, is simply to set the initial focus to any other view, such as a Button etc.
You can do this in your layout XML, no code required..
Accepted answer is not working for me, that's why give answer working solution, may be it is helpful !
EditText edt = (EditText) findViewById(R.id.edt);
edt.requestFocus();
edt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
edt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
Now keyboard is open enjoy :)
android:windowSoftInputMode="stateHidden|adjustResize"
Working fine
Add below code to your top of the activity XML and make sure the View is above EditText
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:focusableInTouchMode="true"/>
android:focusableInTouchMode="true"
Add the above line to xml of EditText or TextInputLayout which has focus and is causing the softInputKeyboard to pop up.
This solved the problem for us and now the keyboard doesn't popup
search_edit_text = (EditText) findViewById(R.id.search_edit_text);
search_edit_text.requestFocus();
search_edit_text.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
search_edit_text.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
This works for me guys fragment may have some different syntax . THIS WORKS FOR ACTIVITY
Use this in your Activity's code:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
If your view has EditText and Listview then Keyboard will open up by default.
To hide keyboard from popping up by default do the following
this.listView.requestFocus();
Make sure you are requesting focus on listview after getting view for editText.
For e.g.
this.listView = (ListView) this.findViewById(R.id.list);
this.editTextSearch = (EditText) this.findViewById(R.id.editTextSearch);
this.listView.requestFocus();
If you do it, then editText will get focus and keyboard will pop up.
I have 2 activities, A and B. When A starts, it checks for a condition and if true, it calls startActivityForResult() to start B. B only takes text input so it makes sense for the soft keyboard to automatically pop up when B start. When the activity starts, the EditText already has focus and it ready for input.
The problem is that the keyboard never shows up, even with windowSoftInputMode="stateAlwaysVisible" set in the manifest under the <activity> tag for B. I also tried with the value set to stateVisible. Since it doesn't show up automatically, I have to tap the EditText to make it show.
Anyone know what the solution might be?
What worked best for me is in Android Manifest for activity B adding
android:windowSoftInputMode="stateVisible"
Easiest solution: Put
android:windowSoftInputMode = "stateVisible"
in Activity section of AndroidManifest.xml
If requestFocus on an EditText isn't showing it, maybe this'll do it:
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, 0);
Look here for more information.
For me worked only this solutions:
add in manifest for that activity:
android:windowSoftInputMode="stateVisible|adjustPan"
I have got two way.
Method 1.
Use the following code inside the OnCreate method
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
It will prevent popping up keyboard unless you click.
or
Method 2 You can move away the focus on other view like TextView by using "requestfocus" in the xml.
<TextView
android:id="#+id/year_birth_day"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1991">
<requestFocus />
</TextView>
Method 3 ( I think it should be avoidable) Using the following code in the manifest-
android:windowSoftInputMode="stateVisible"
Try showing the keyboard with some delay. Something similar to this:
public void onResume() {
super.onResume();
TimerTask tt = new TimerTask() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourTextBox, InputMethodManager.SHOW_IMPLICIT);
}
};
final Timer timer = new Timer();
timer.schedule(tt, 200);
}
Major Attention Required!
android:windowSoftInputMode="stateVisible|adjustPan" This alone won't work to show keyboard on activity start.
You also need to explicitly add this into your class
editTextXYZ.requestFocus()
val imm: InputMethodManager =
getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(editTextXYZ, InputMethodManager.SHOW_IMPLICIT)
If you're using an emulator, you have to turn the hard keyboard off in order for the soft keyboard to show.
File : AndroidManifest.xml
<activity android:name=".MainActivity">
Add following property :
android:windowSoftInputMode="stateVisible"
Which worked for me.
paste this after setContentView
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);