Show keyboard automatically
Asked Answered
M

10

52

Please explain me the issue about soft keyboard. For example, I have an EditText on my activity or dialogfragment or fragmentactivity, whatever. here it is:

<EditText
    android:id="@+id/edPswrd"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPassword" >

    <requestFocus />
</EditText>

When it shows for the first time I do not see the soft keyboard and have to press editText for it to receive focus and the keyboard appears. Another activities are different, when it appears on screen the keyboard are loaded without any help. I thought that

< requestFocus />

means that EditText will be focused and keyboard will appear, but I am wrong.

How should I manage what component will receive focus and keyboard will automatically appear.

Moralize answered 7/2, 2013 at 19:21 Comment(2)
I find it is better to NOT check Focusable on the TextEdit.Ratan
Possible duplicate of How to show soft-keyboard when edittext is focusedHardnett
T
79

I think it's a bug or a feature which tries to present the whole activity to you without obscuring it with the soft keyboard at first. I've searched once for information regarding that but unfortunately found nothing coming from a really reliable source.

Anyway, to show the soft keyboard you can do this:

EditText editText = (EditText)findViewById(R.id.edit_text_id);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

I've also seen this code that should force the soft keyboard to become visible just after activity start, but never tried it:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

And if you want to hide soft keyboard you can do this:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

Edit:

For a DialogFragment this should work: in the onCreateView() method do this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_id, container);
    EditText editText = (EditText)view.findViewById(R.id.edit_text_id);

    // show soft keyboard
    editText.requestFocus();
    getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);

    return view;
}
Torque answered 7/2, 2013 at 19:37 Comment(6)
I tried, but not one of method here doesn't work with my dialogfragment. I did this: InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(edPswrd, InputMethodManager.SHOW_IMPLICIT); And it doesn't work :(Moralize
thank you. I imported android.view.ViewGroup.LayoutParams but still there is a error SOFT_INPUT_STATE_VISIBLE cannot be resolved or is not a fieldMoralize
I also did this: getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); like in advise below but keyboard still doesn't appearMoralize
also I just tried you "hide keyboard" on other activity, wrote this InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); in onCreate() but keyboard appears :(Moralize
I got it to work in onCreateDialog also. See my responsePrather
How do you get the keyboard to disappear after you dismiss the dialog? The keyboard stays up even after dismissing the dialog if you use getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);.Underslung
H
34

Open the Android Manifest file.

Look for the activity tag like this

<activity  
        android:name="com.example.framework.MainActivity"
        android:label="@string/app_name" 
        android:windowSoftInputMode="stateVisible"       //Add this line    
         >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Add the line android:windowSoftInputMode="stateVisible" as shown above

Hummock answered 7/2, 2013 at 19:33 Comment(2)
ok, thank you, it worked for the activity, and it didn't suit for dialogs.Moralize
what about viewpager?!Bibb
P
19

I know this has already been answered, but I found a way to do the accepted answer in onCreateDialog instead of just in onCreateView. When you finish with the builder, before you return do the following:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());  
// blah blah blah do builder stuff here like setTitle, setView, etc

Dialog d = builder.create();

Here's the important part:

d.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return d;
Prather answered 28/4, 2015 at 15:33 Comment(3)
I had to use SOFT_INPUT_STATE_ALWAYS_VISIBLE to show keyboard (Nexus 5). Anyway, thanks for the solution in onCreateView() method.Renzo
How do you dismiss the keyboard if you use this method of showing the keyboard?Underslung
same as always. Check this and thisPrather
M
11

add to onCreate or onStart();

myView.requestFocus();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Mendez answered 7/2, 2013 at 19:32 Comment(4)
I have got a dialogfragment and did this: edPswrd.requestFocus(); getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); It doesn't work :(Moralize
which part, the focus on the dialogfrag or the keyboad comming up ?Mendez
there is still no keyboard on screen when dialog appearsMoralize
It works like this: getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); Thank you!Moralize
Z
5

Try this

@Override
public void onResume() {
   super.onResume();
   final View v = getDialog().findViewById(R.id.edit_text_id);
   v.post(new Runnable() {
      @Override
      public void run() {
        v.requestFocus();
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
      }
   });
 }
Ziegfeld answered 12/5, 2014 at 12:53 Comment(0)
G
1

for me by adding this line android:windowSoftInputMode="stateVisible" in your manifest like this

**<activity
            android:name=".mmmmm.zzzzzzz.yyyyyy.xxxxxx"
            android:windowSoftInputMode="stateVisible"
            android:label="@string/title_activity_print_recharge"></activity>**

this is how I solved it

Gwendagwendolen answered 9/11, 2017 at 7:21 Comment(0)
E
1

First I add requestFocus tag in my EditText

<androidx.appcompat.widget.AppCompatEditText
                    android:id="@+id/mobile_number_edit_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@null"
                    android:hint="@string/give_mobile_number"
                    android:inputType="number"
                    android:maxLength="11"
                    android:maxLines="1"
                    android:textColor="@color/body"
                    android:textSize="@dimen/_13ssp"
                    app:fontFamily="@font/hkgrotesk_regular"
                    >

                    <requestFocus />

</androidx.appcompat.widget.AppCompatEditText>

then I create an extension function to open Keyboard. Here it is

fun Activity.showKeyboard() {
    val imm = this.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager

    var view = this.currentFocus
    if (view == null) view = View(this)

    imm.showSoftInput(view, 0)
}

After that, call showKeyboard() in my Fragment

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)


        activity?.showKeyboard()

        ... ... ... Your other code ... ... ...
}
Eh answered 23/5, 2022 at 7:44 Comment(0)
I
0

This is what worked for me -

Create your own EditText class and override following method -

public class FocussableEditText extends EditText {

    public FocussableEditText(Context context) {
        super(context);
    }

    public FocussableEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FocussableEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        if (hasWindowFocus) {
            InputMethodManager imm = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(this, InputMethodManager.SHOW_FORCED);
        }
    }
}

http://debuggingisfun.blogspot.in/2014/08/android-show-soft-keyboard.html

Indelicate answered 23/9, 2016 at 10:32 Comment(0)
L
0

I Recommend the following link, i was followed the steps which is mentioned below link, perfectly working, it's pretty good answer, done in very simple steps. credit goes to answered person. hope it will helps the person who mess-up with keyboard popup on dialog activity.

DialogFragment and force to show keyboard

Logjam answered 11/1, 2017 at 13:23 Comment(0)
S
0

This is what I did and it works nice...

You can use the

Utilities.showSoftKeyboard(...)

anywhere across your code

public final class Utilities {

  // Shows the soft keyboard. V on which view (typically EditText) to focus the keyboard input
  public static void showSoftKeyboard(Activity activity, EditText editText) 
  {
      InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
      imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
  }
}

/**
 * Called when user clicks/touches the search button
 * @param v The clicked/touched view (EditText)
 */
protected void onSearchButtonClick(View v)
{
    EditText seekTopic = (EditText) findViewById(R.id.SeekTopicBox);
    setActivityContent(seekTopic.getText().toString());
}

The

onSearchButtonClick()

method belongs to the activity, which hold the EditView which I want to edit and therefore I need the soft keyboard..

HTH

Snowdrop answered 8/3, 2018 at 16:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.