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 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 am currently developing an app with Samsung Galaxy Tab 2 as my device.
My Code in the XML is:
<EditText
android:id="#+id/analysis_text"
style="#style/icon_text"
android:imeOptions="actionDone"
android:inputType="textMultiLine"
android:onClick="onBtnClicked"
/>
When this code executes, the full screen data entry mode (Extract Mode) is triggered automatically only in certain situations.
I would like my users to get a full data entry screen while editing text in this control, regardless of the screen or positioning of the control.
Another rephrase of this question:
How do I force full screen data entry mode for EditText boxes in my activity no matter what the content is?
I solved this issue, not really solved, but found a valid work-around.
The workaround is that I designed a text editor (which looks similar to the fullscreen UI) and on click of each of those EditBoxes the new UI activity is triggerred (with the startActivityForResult() so that once they are completed control is handed back to the calling activity) and after completion of edit the text is transferred back into the main screen.
I also ensured that those boxes which transfer the control do not take focus so that they immediately transfer control to the new UI.
This way I have been able to implement a cancel button, which now actually allows the user to go back without saving the changes he accidentally makes.
I am open to new ideas, but this has worked for me.
Code in Activity:
public void onBtnClicked(View v) {
EditText current_text_box = (EditText) v;
Intent intent = new Intent(ObservationActivity.this,
TexteditorActivity.class);
intent.putExtra("start_text", current_text_box.getText().toString());
intent.putExtra("start_position", current_text_box.getSelectionStart());
startActivityForResult(intent, v.getId());
}
Code in XML:
<EditText
android:id="#+id/observation_text"
style="#style/icon_text"
android:focusable="false"
android:imeOptions="flagNoExtractUi"
android:inputType="textMultiLine"
android:onClick="onBtnClicked" >
</EditText>
To create the full screen UI I used code, you can use any like (http://android-richtexteditor.googlecode.com/)
You Can Try This
yourEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
yourEditText.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
}
});
Import LayoutParams of your Parent Layout. Your answer will also work well.
I am trying to implement a relatively simple DialogFragment that is supposed to contain an image and a OK button and I want to display it on demand from my activity.
I set its layout in onCreateView via inflater.inflate, but I can't figure out how to tell it that the implementation for the OK button event handler is located in my custom DialogFragment class. It seems to try to find it in the activity, which is not what I want. Would calling getDialog().dismiss() be enough to dismiss it?
Here is how I create a dialog in my activity:
ResponseDialog dialog = new ResponseDialog();
dialog.show(getFragmentManager(), "dialog_response_image");
Also, some folks say that my custom DialogFragment should set getDialog().setCanceledOnTouchOutside(true);, but where should I set this. In onActivityCreated?
How can I get access to its view from the activity, if I wish to set the source of the image contained by it?
Also, for some reason, it fills up the entire display, even if I use static width / height. Does anyone know how to fix this? - I managed to fix this by swithching to LinearLayout instead of RelativeLayout in the DialogFragment layout XML...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#color/background_color"
tools:context=".MainActivity"
android:layout_width="200dp"
android:layout_height="400dp"
android:id="#+id/dialogImageReponse" >
<Button
android:id="#+id/dialogButtonOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/ok"
android:onClick="Ok" />
</RelativeLayout>
I'll try to answer each of your questions:
1.) About the "how to tell it that the implementation for the OK button event handler is located in my custom DialogFragment class" I'm not sure why you want to do that since you haven't told us.
2.) "Would calling getDialog().dismiss() be enough to dismiss it?". If you wish to dismiss the DialogFragment from your Activity then you just need to call dialog.dismiss() ('dialog' here refers to ResponseDialog dialog = new ResponseDialog(); so obviously you can only call dialog.dismiss() once you've created the ResponseDialog object.
3.) Regarding "getDialog().setCanceledOnTouchOutside(true);" Once again you just need to call
dialog.setCanceledOnTouchOutside(true);
immediately after this:
ResponseDialog dialog = new ResponseDialog();
dialog.show(getFragmentManager(), "dialog_response_image");
4.) You can get access to its view by calling:
View v = dialog.getView();
Although if you just wish to set the source of the image to be used contained by it, and I assume you want to use a photo or a picture stored in your phone then you would have to use a parametrized constructor for ResponseDialog like this and as a parameter you would have to pass the URI OR the filepath of the picture that you would like displayed in the dialogfragment :
ResponseDialog dialog = new Response(String filepath);
and then in your custom dialog class which I understand is ResponseDialog you would have to use this filepath (received in the constructor) to create a bitmap and then set the bitmap as the source of the ImageView in that DialogFragment.
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.