I have an EditText view in a fragment and when showing that fragment, I need to let it get focus and the soft keyboard pops up to allow user type in directly, however, the EditText view seems can only get focus but not show the keyboard even though I manually force it to show. The user has to touch it to bring up the soft keyboard. I tried to get a minimum app to reproduce this, it's simply an activity with an EditText. Any idea?
Activity
package com.example.litaos.testeditview;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText = findViewById(R.id.edit_text);
editText.requestFocus();
final InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
}
Layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.litaos.testeditview.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit_text"
android:hint="Hello World!"
android:focusable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Have you tried adding requestFocus ?
I think it's not enough to just request focus. Please try adding following code to your onCreate method:
I hope this helps you!