I would like to open the soft keyboard on when the Activity starts and I found that
android:windowSoftInputMode="stateVisible"
doesn't work.
To make sure, I created a new project (the default "Hello world") and did the following:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
to the onCreate procedure.
I compiled it with Android2.3.3 and tried to run it on my Galaxy S2 device and the Android4 emulator and still - no keyboard.
My manifest file:
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk android:minSdkVersion="9" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".HelloworldActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateVisible">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
My main.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
My code:
public class HelloworldActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your edit field. This will force the keyboard to be hidden in all situations.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code when you click on the button it will hide keyboard.
The Android system shows an on-screen keyboard—known as a soft input method—when a text field in your UI receives focus.
You can force first focus on the EditText
view by running:
final EditText edit = (EditText) findViewById(R.id.editText1);
edit.post(new Runnable() {
@Override
public void run() {
edit.requestFocus();
}
});
This should open the keyboard on activity start.
Its simple thing. I have done it and it works as your requirement.
Don't do anything with the manifest, leave it as it is while you creating the new new project.
Now define inputmanager.
public static InputMethodManager imm;
imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
Now, here salary EditText is my EditText and i am showing the keyboard on the start of that activity.
salaryEditText.setHint("select Salary/Wage");
salaryEditText.setFilters(new InputFilter[] {new DecimalDigitsInputFilter(2)}); // Ignore this line
if(!(imm==null))
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,1);
That will help you to show the keyboard while activity start.
To Close keyboard on the activity finish see below code:
white this code while you finish the activity.
imm.hideSoftInputFromWindow(salaryEditText.getWindowToken(), 0);
Hope it will solve your issue. If not then let me know.
Enjoy. :)
Do you use the default android keyboard? If you do, try it on a different device, I know it has some issues
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With