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);
Related
I have an Android app which has just one screen and there is NO edit text field in this screen( infact there is no UI elements on this screen).
When I try to search this app in Apps search by giving app name as keyword (its obvious keyboard comes out in order to type app name to search in apps tray) and after search, I clicked on my app to open/launch still the keyboard comes out and it wont go away until I press back button. When I launch my app the keyboard should not appear over there because this keyboard came from apps search. I does not look UI friendly, how can we handle this. Please share your thoughts on this.
You can set in your manifest this value under your activity:
<activity
android:name=".YourActivity"
....
android:windowSoftInputMode="stateAlwaysHidden">
Or try to hide the keyboard manually in onCreate like this:
//kotlin
fun hideKeyboard(context:Context, view:View){
val inputMethodManager =
context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken,
InputMethodManager.HIDE_IMPLICIT_ONLY)
}
An call the code like this
hideKeyboard(this, findViewById<View>(android.R.id.content));
implement this in your first activity it will hide the keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
View view = activity.getCurrentFocus();
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
<activity
android:name=".Activity"
android:windowSoftInputMode="adjustPan|stateAlwaysHidden">
</activity>
put this line into your manifest file in your activity tag
The best Solution for your requirement is:-
Create a BaseActivity ,
then extend all your activities to BaseActivity rather than
AppcompactActivity
Then in manifest do this
<activity
android:name=".BaseActivity"
android:windowSoftInputMode="stateAlwaysHidden">
This will not Show keyboard when any of the activity loads
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 have hidden soft keypad because I have custom keypad on the app. When the edittext is clicked, soft keypad shouldn't pop up. So, I have tried so many ways from the sources, but nothing worked except the editText.setFocusable(false); . But now the problem is edittext is not getting highlighted when I clicked it and even cursor is not visible. I have tried using InputManager, android:windowSoftInputMode="stateAlwaysHidden in the manifest and referred many like link 1 , link 2 etc., but these techniques atleast don't even hide the soft keypad on my app. Finally I got this through setFocusable, but there is a highlighting problem and cursor invisible problem and even requestFocus() in the onClickListener didn't work. Can someone give exact solution for this problem? Code snippet is appreciated.
Try this one in activity class
getwindow().setsoftInputMode(winowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
This one is avoiding of soft key pad
please use this in manifest:
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="stateHidden"
You ddont need to add any method in menifist. just add this code.. It will automaticlly hide when you click on button to get value.
Want to hide softkeyboard use this code in your click listener method.
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFomWindow(edittext.getWindowToken(),0);
i hope this code work fine.
Try this:
InputMethodManager imm = (InputMethodManager)getSystemService( Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFomWindow( edittext.getWindowToken(), 0);
how about if you editText.setOnTouchListener and when you create the new OnTouchListener do nothing something like:
editText.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
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);