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
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);
}
As the title says. I have tried the usual,
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(root.getWindowToken(), 0);
in OnResume(), but it does not work, presumably because the keyboard is open from the Gmail window, not my app's window.
Stepping through the code shows imm.mActive == false at this point, if that matters. I tried putting this code in OnPostResume(), but still no effect.
I have tried using InputMethodManager.HIDE_NOT_ALWAYS instead of 0, but also no effect.
The built-in gallery app does not have this problem, so I think a solution is possible.
I think you need get the applications context in order to hide the keyboard. Something like this:
View v = (Your Base Layout) findViewById(R.id....);
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
If that doesn't work you can also try this:
this.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
hope this helps :)
My solution was to make sure my EditText no longer had focus before navigating to the Gmail activity:
contactSearchView.clearFocus()
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);
}
}
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);