5

I am making an app where I am using a ArrayAdapter<String> and I am programmatically making an editText view inside of a Relative Layout which is all inside of a AppCompatDialogFragment. Each of the editTexts are showing up and I can click on them, but if it doesn't open the keyboard to type. I have tried things like:

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

Even if I try to manually open the keyboard, it still doesn't open.

        RelativeLayout listLayout = new RelativeLayout(this.getContext());
        listLayout.setLayoutParams(new AbsListView.LayoutParams(
                AbsListView.LayoutParams.WRAP_CONTENT,
                75));

        if(super.getItem(position) == ParameterTypes.String.toString()) {
            EditText editText = new EditText(this.getContext());
            editText.setWidth(500);
            editText.setHint(parameterNames[position]);
            editText.setInputType(InputType.TYPE_CLASS_TEXT);
            listLayout.addView(editText);
        } else if(super.getItem(position) == ParameterTypes.Integer.toString()) {
            EditText editText = new EditText(this.getContext());
            editText.setWidth(500);
            editText.setHint(parameterNames[position]);
            editText.setInputType(InputType.TYPE_CLASS_NUMBER);
            listLayout.addView(editText);
        } else if(super.getItem(position) == ParameterTypes.Double.toString()) {
            EditText editText = new EditText(this.getContext());
            editText.setWidth(500);
            editText.setHint(parameterNames[position]);
            editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
            listLayout.addView(editText);
        return listLayout;
James Z
  • 12,209
  • 10
  • 24
  • 44
Mr. Ace
  • 335
  • 3
  • 14

3 Answers3

4

try this

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

in xml

android:focusableInTouchMode="true"
s.mhmd
  • 187
  • 10
  • Thanks a lot, s.mhmd for your answer! I can't put it in the xml though, because I am creating this Edit Text from code only. Would it still work? – Mr. Ace Jun 11 '20 at 00:13
  • try add >> android:windowSoftInputMode="stateAlwaysVisible" -> in manifest File. – s.mhmd Jun 11 '20 at 00:22
1

You may try to call imm.showSoftInput [...] somewhat delayed with new Handler().postDelayed(new Runnable(){...}, 100L); since it takes time until the requestFocus() is effective, especially when your application is just starting up / your views. What you can also try is to call the requestFocus() delayed the same way.

larsaars
  • 2,065
  • 3
  • 21
  • 32
  • Thanks, Lurzapps for the quick response. I didn't quite get what you meant above. Could you please explain? – Mr. Ace Jun 11 '20 at 18:07
  • 1
    I once had the problem that I called the requestFocus too early, and it took me till I finally realized I have to call it delayed – larsaars Jun 11 '20 at 18:08
  • Oh, that makes a whole lot more sense! Thanks so much, Lurzapps!! – Mr. Ace Jun 11 '20 at 18:17
0

You can set focusable programmatically:

editText.setFocusableInTouchMode(true);

Akinyemi Jamiu
  • 431
  • 3
  • 10