Android: How to make the keypad always visible?
Add android:windowSoftInputMode="stateAlwaysVisible" to your activity in the AndroidManifest.xml file:
<activity android:name=".MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateAlwaysVisible" />
In my test app this shows the keyboard on starting of the application although it isn't fixed there but can be dismissed by pressing the back button.
To make sure the keyboard is always visible you might have to create your own keyboard as part of the UI of your application. Here is a tutorial to show you how to do this with KeyboardView: http://www.fampennings.nl/maarten/android/09keyboard/index.htm
How to always show soft keyboard and do not let it be closed?
Use android:windowSoftInputMode="stateAlwaysVisible"
in to AndroidManifest.xml
file
Like this:
<activity android:name=".YourActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateAlwaysVisible" /> // OR stateVisible
If that activity having
EditText
so when ever Activity will start
your Keyboard automatically open
If you want to still open Keyboad
after use done any operation then do this via programetically
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInputFromWindow(
linearLayout.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
OR
To Show Soft Keyboard
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(EDITABLE_VIEW,
InputMethodManager.SHOW_IMPLICIT);
OR
EDITABLE_VIEW
can be any view which has focus on screen like
mEditText = (EditText) findViewById(R.id.editText);
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText ,
InputMethodManager.SHOW_IMPLICIT);
OR
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInputFromInputMethod(editText.getWindowToken(), 0);
Documentation
Make button stay visible when keyboard becomes visible
In your AndroidMenifest.xml file inside your tag use
windowsSoftInputMode = "adjustResize"
Or take whole layout inside ScrollView
windowsSoftInputMode = "adjustPan"
Example code snippet:
<activity
android:name=".activityname"
android:label="@string/app_name"
android:windowSoftInputMode="adjustPan|adjustResize" >
</activity>
Use android:fillViewport="true"
tag in ScrollView. And to avoid layout size bugs, use margin in Top ScrollView and don't use it in ScrollView child.
And add this in OnCreate
scrollView = (ScrollView) this.findViewById(R.id.scrollView);
scrollView.setVerticalScrollBarEnabled(false);
imageView = (ImageView) findViewById(R.id.imageView);
this.findViewById(android.R.id.content).addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
scrollView.scrollTo(0, imageView.getHeight() + ((ViewGroup.MarginLayoutParams) imageView.getLayoutParams()).topMargin);
}
});
See the comments..
Android Studio - Always show keyboard?
Below is the sample code which you could re-use,
In the AndroidManifest.xml use this..
android:windowSoftInputMode="stateAlwaysVisible"
like below
<activity android:name=".MainActivity"
android:windowSoftInputMode="stateAlwaysVisible">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
In your layout you can use
<EditText
android:id="@+id/editText"
android:singleLine="true"
android:imeOptions="actionDone"
...
/>
In OnCreate() method of your activity, you could implement something like this...
final EditText editText = ((EditText) findViewById(R.id.editText));
editText.setOnEditorActionListener(new EditText.OnEditorActionListener(){
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
String value = editText.getText().toString();
//TODO .. write your respective logic to add data to your textView
editText.setText(""); // clear the text in your editText
return true;
}
});
Android: How do I prevent the soft keyboard from pushing my view up?
You can simply switch your Activity's windowSoftInputMode
flag to adjustPan
in your AndroidMainfest.xml
file inside your activity tag.
Check the official documentation for more info.
<activity
...
android:windowSoftInputMode="adjustPan">
</activity>
If your container is not changing size, then you likely have the height set to "match parent". If possible, set the parent to "Wrap Content", or a constraint layout with constraingts to top and bottom of parent.
The parent container will shrink to fit the available space, so it is likely that your content should be inside of a scolling view to prevent (depending on the phone manufacturer and the layout choosen...)
- Content being smashed together
- Content hanging off the screen
- Content being inacccessable due to it being underneath the keyboard
even if the layout it is in is a relative or constraint layout, the content could exhibit problems 1-3.
Android: show soft keyboard automatically when focus is on an EditText
You can create a focus listener on the EditText
on the AlertDialog
, then get the AlertDialog
's Window
. From there you can make the soft keyboard show by calling setSoftInputMode
.
final AlertDialog dialog = ...;
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});
How to close/hide the Android soft keyboard programmatically?
To help clarify this madness, I'd like to begin by apologizing on behalf of all Android users for Google's downright ridiculous treatment of the soft keyboard. The reason there are so many answers, each different, for the same simple question is that this API, like many others in Android, is horribly designed. I can think of no polite way to state it.
I want to hide the keyboard. I expect to provide Android with the following statement: Keyboard.hide()
. The end. Thank you very much. But Android has a problem. You must use the InputMethodManager
to hide the keyboard. OK, fine, this is Android's API to the keyboard. BUT! You are required to have a Context
in order to get access to the IMM. Now we have a problem. I may want to hide the keyboard from a static or utility class that has no use or need for any Context
. or And FAR worse, the IMM requires that you specify what View
(or even worse, what Window
) you want to hide the keyboard FROM.
This is what makes hiding the keyboard so challenging. Dear Google: When I'm looking up the recipe for a cake, there is no RecipeProvider
on Earth that would refuse to provide me with the recipe unless I first answer WHO the cake will be eaten by AND where it will be eaten!!
This sad story ends with the ugly truth: to hide the Android keyboard, you will be required to provide 2 forms of identification: a Context
and either a View
or a Window
.
I have created a static utility method that can do the job VERY solidly, provided you call it from an Activity
.
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Be aware that this utility method ONLY works when called from an Activity
! The above method calls getCurrentFocus
of the target Activity
to fetch the proper window token.
But suppose you want to hide the keyboard from an EditText
hosted in a DialogFragment
? You can't use the method above for that:
hideKeyboard(getActivity()); //won't work
This won't work because you'll be passing a reference to the Fragment
's host Activity
, which will have no focused control while the Fragment
is shown! Wow! So, for hiding the keyboard from fragments, I resort to the lower-level, more common, and uglier:
public static void hideKeyboardFrom(Context context, View view) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Below is some additional information gleaned from more time wasted chasing this solution:
About windowSoftInputMode
There's yet another point of contention to be aware of. By default, Android will automatically assign initial focus to the first EditText
or focusable control in your Activity
. It naturally follows that the InputMethod (typically the soft keyboard) will respond to the focus event by showing itself. The windowSoftInputMode
attribute in AndroidManifest.xml
, when set to stateAlwaysHidden
, instructs the keyboard to ignore this automatically-assigned initial focus.
<activity
android:name=".MyActivity"
android:windowSoftInputMode="stateAlwaysHidden"/>
Almost unbelievably, it appears to do nothing to prevent the keyboard from opening when you touch the control (unless focusable="false"
and/or focusableInTouchMode="false"
are assigned to the control). Apparently, the windowSoftInputMode setting applies only to automatic focus events, not to focus events triggered by touch events.
Therefore, stateAlwaysHidden
is VERY poorly named indeed. It should perhaps be called ignoreInitialFocus
instead.
UPDATE: More ways to get a window token
If there is no focused view (e.g. can happen if you just changed fragments), there are other views that will supply a useful window token.
These are alternatives for the above code if (view == null) view = new View(activity);
These don't refer explicitly to your activity.
Inside a fragment class:
view = getView().getRootView().getWindowToken();
Given a fragment fragment
as a parameter:
view = fragment.getView().getRootView().getWindowToken();
Starting from your content body:
view = findViewById(android.R.id.content).getRootView().getWindowToken();
UPDATE 2: Clear focus to avoid showing keyboard again if you open the app from the background
Add this line to the end of the method:
view.clearFocus();
Related Topics
Android: Android.Content.Res.Resources$Notfoundexception: String Resource Id #0X5
How to Transfer an Image from Its Url to the Sd Card
How to Solve Execution Failed for Task ':App:Compileflutterbuilddebug'
How to Verify That an Android APK Is Signed with a Release Certificate
Passing Argument to Dialogfragment
Android How to Apply Mask on Imageview
Appcompatv7 - V21 Navigation Drawer Not Showing Hamburger Icon
Multiple Instances of Widget Only Updating Last Widget
Android - Volume Buttons Used in My Application
Change the Text Color of Numberpicker
How Can Make My Viewpager Load Only One Page at a Time Ie Setoffscreenpagelimit(0);
Gradle Dsl Method Not Found: 'Runproguard'
Android Onclick in Xml VS. Onclicklistener
How to Get an Uri of an Image Resource in Android
Android Studio Not Deploying Changes to App
How to Set Font Custom Font to Spinner Text Programmatically