Show soft keyboard when Activity starts

半城伤御伤魂 提交于 2019-11-28 18:07:21

What worked best for me is in Android Manifest for activity B adding

android:windowSoftInputMode="stateVisible"

Hope that helps for you as well.

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"

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);
}

If you're using an emulator, you have to turn the hard keyboard off in order for the soft keyboard to show.

Keyur Sureliya

File : AndroidManifest.xml

<activity android:name=".MainActivity">

Add following property :

android:windowSoftInputMode="stateVisible"

Which worked for me.

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"

paste this after setContentView

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!