I have an activity with lots of edittext. whenever I load that activity, the keyboard appears and eats half of the screen which makes that activity's look bad. So is there any way to hide keyboard when I load that activity.
in your onCreate() use this..
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Add this two line in your activity's XML file in the RootLayout i.e. either relative or linear(whatever you have taken) :
android:focusableInTouchMode="true"
Add this line in activity manifests file
android:windowSoftInputMode="stateHidden"
In your AndroidManifest.xml add the attribute android:windowSoftInputMode:
<activity android:name="your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
You can do this using intputmethodmangare... using the following code..
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
Put this code on the onCrete function:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
view.clearFocus();
}}, 50);
where view is your EditText
The runnable is because the code might be executed before the editText is rendered.
I created a method which I call in all the required Activity classes in the onCreate event. Worked for me in all scenarios.
public class ClassLib {
public static void hideKeyboard(Activity activity) {
//Hide keyboard
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
}
Related
My activity has a single text field, which is editable, I want it so that when the activity is started the keyboard doesn't automatically start up, it should come up only when the user clicks on the editTiext field.
Any help?
In your manifest file add this line android:windowSoftInputMode="stateHidden"to your activity!
It seems like when your activity starts your TextView (since you said text field I suppose you have a TextView but the property exists on other views as well) receives automatically focus. Try looking at the TextView properties to find one that is about the object receiving focus.
public static void hideKeyboard(Context mContext){
//Hide a keypad write down on onCreate
((Activity) mContext).getWindow()
.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
public static void showKeyboard(Context mContext,EditText edittext){
//Show a Keyboard when you click on Edittext
InputMethodManager mgr = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(edittext, InputMethodManager.SHOW_FORCED);
}
I have this page :
But when open softWareKeyBoard I can't see other EditText. What can I do?
NOTICE : I use below code but not worked .And my class is extends Fragment!
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
there is two ways to do this either you can use this in your Manifest activity
it will work for that ,the adjust-pan will flow your surface upward and open the soft keyboard
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
<activity android:name="XXXActivity" android:windowSoftInputMode="adjustPan">
</activity>
Just include window?.setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) in onCreate method of your activity containing the Fragment (this will affect all fragments inside activity)
For example:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
window?.setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
)
}
Working Code
For Fragments:
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
For Activities:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Use can also use NestedScrollView as parent view. Set fitsSystemWindows = "false" and set android:windowSoftInputMode="adjustResize" in manifest file.
I have used like this to show the soft keyboard programatically and this is worked for me to prevent the auto resize of the screen while launching the keyboard.
EditText et = (EditText))findViewById(R.id.edit_text);
Timer timer = new Timer();
TimerTask task = new TimerTask() {
#Override
public void run() {
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(et.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
}
};
timer.schedule(task, 200);
In Manifest file use:
<activity android:name="XXXActivity" android:windowSoftInputMode="adjustPan">
</activity>
I have 2 EditTexts in the MainActivity Layout. If i run the application normally the 1st EditText gets focused but the softkeyboard is not openned.
but when i used this:
public class TestingActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText et1 = (EditText) findViewById(R.id.editText1);
EditText et2 = (EditText) findViewById(R.id.editText2);
et2.requestFocus();
InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mInputMethodManager.showSoftInput(et2, InputMethodManager.SHOW_IMPLICIT);
}
}
expecting the 2nd EditText will get focus and softkeyboard will be openned.
I only get focus, but the softkeyboard is openned only when i click on the EditText.
Thank You
Try specifying the android:windowSoftInputMode attribute in your AndroidManifest.xml file for your activity.
For example:
<activity android:name=".TestingActivity" android:windowSoftInputMode="stateVisible|adjustResize" />
You probably don't need any of the code that uses InputMethodManager in your Activity.
I notice that one reason for the keyboard not showing up is selecting an inputtype not supported by the specific Android device. For instance InputType.TYPE_NUMBER_VARIATION_NORMAL will not work on my Asus Transformer (no keyboard shows up), while InputType.TYPE_CLASS_NUMBER will work just fine.
et2.clearFocus();
et2.requestFocus();
InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mInputMethodManager.showSoftInput(et2, InputMethodManager.SHOW_IMPLICIT);
I meet the problem on Android N platform and resolve it by refocusing the editview.
I don`t know the real reason why the editview should be cleared first,but it works fine for me.
Sometimes you will need to post-delay showing keyboard command, so in my case, i did the following
editText.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
}, 300);
For getting the focus to particular edittext just add the tag inside your edit text.
<EditText
android:id="#+id/etBox"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="enter into editbox"
>
<requestFocus/>
</EditText>
I've browsed similar questions and followed the suggestions there, but for the love of god, I can't get this to work, and it's driving me crazy. So here's the deal:
I have an editText, which needs to requestFocus at program startup, and pop the soft keyboard. If I put "android:windowSoftInputMode="stateVisible" in the Manifest, the keboard shows every time the activity starts. I only want it to show once with onCreate(), and when the user specifically clicks on the editText. My code for this is below:
EditText argument;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_buttons);
argument = (EditText) findViewById(R.id.editText_argument);
InputMethodManager imm = InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(argument, InputMethodManager.SHOW_FORCED);
Q1) This code doesn't work. What am I doing wrong?
Q2) You see that I declared "EditText argument" outside of onCreate(), as I'd like to use this in the rest of the activity, not just within onCreate(). Is this good programming practice?
Q3) Then, when the user clicks done on the soft keyboard, I'd like this EditText to lose focus, i.e. the cursor should disappear. I understand that I need to have a dummy View to do this, but I still don't exactly understand how to switch focus to the dummy. How would I go about doing that?
Thanks so much in advance!
A1) You're missing a editText.requestFocus().
Refer: Soft Keyboard shows up on EditText focus ONLY once should help for dismissing soft keyboard.
A2) Yes, that's fine. Most of the UI elments should be declared at the class level scope and initialized in onCreate()
A3) A1's reference link should help you here.
Happy Coding!
EDIT:
onCreate():
EditText argument;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_buttons);
argument = (EditText) findViewById(R.id.editText_argument);
showKeyboard():
argument.requestFocus();
argument.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(argument, 0);
}
},200);
dismissKeyboard():
argument.requestFocus();
argument.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(argument.getWindowToken(), 0);
}
},200);
I want to immediately highlight give focus to a particular edit box when the activity loads and bring up the softkeyboard. how can I do this? Also should there be anything in onStart()?
You can call : requestFocus() on the View after you do findViewByID()
You can do that in onStart - I see now reason for it not to work.
Check here too :
http://developer.android.com/reference/android/view/View.html
In your layout put </requestFocus> tag inside EditText.
In onStart() call
getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Ideally this should work.
<EditText
android:id="#+id/Abc"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<requestFocus />
</EditText>
But there have been some known issues of the keyboard not popping up. See this.
You can also do this using Runnable:
public class MyActivity extends Activity {
private Handler mHandler= new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// get text
mHandler.post(new Runnable() {
public void run() {
text.requestFocus();
}
});
}
}
Using the getSystemService(...), one can obtain, in this case, the Context.INPUT_METHOD_SERVICE, have a look at this code sample below for illustration:
InputMethodManager imm = (InputMethodManager)context.getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edittext, 0, null);