I'm trying to make a To-Do app. You click on a button which brings up a pop-up where you can enter text and click on add button to have it added to the list. I'd like to have the keyboard show up at appropriate times:

1) When the user clicks on the button, he should be able to enter text without having to click on the edittext field.

2) After the user clicks on add, the keyboard should disappear rather than not going away until the user presses back.

I know this question has been asked before many times but the solutions don't seem to be working for me. Here's the code:

 button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final EditText taskEditText = new EditText(MainActivity.this);
            AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
                    .setTitle("Add a new task")
                    .setMessage("What do you want to do next?")
                    .setView(taskEditText)
                    .setNegativeButton("Add", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            taskEditText.requestFocus();
                            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.showSoftInput(taskEditText, InputMethodManager.SHOW_IMPLICIT);
                            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                            String task = String.valueOf(taskEditText.getText());
                            SQLiteDatabase db = mHelper.getWritableDatabase();
                            ContentValues values = new ContentValues();
                            values.put(TaskContract.TaskEntry.COL_TASK_TITLE, task);
                            db.insertWithOnConflict(TaskContract.TaskEntry.TABLE,
                                    null,
                                    values,
                                    SQLiteDatabase.CONFLICT_REPLACE);
                            db.close();
                            updateUI();
                        }
                    })
                    .setPositiveButton("Cancel", null)
                    .create();
            dialog.show();
        }
    });
    updateUI();
}

This question was a bit different earlier and the responses I got didn't work for me.

You can assign focus to the EditText by calling teskEditText.requestFocus(). If that doesn't bring up the keyboard you could also add the line getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);.

Bonus question:
If you want an EditText without the line underneath the text, you can add a background.

Example, I would like a white background on my EditText with no underline, so I set android:background="#ffffff" in the layout XML file. This could also be done programmatically with taskEditText.setBackgroundColor();

  • I know this is an extremely naive question but where do I put the taskEditText.requestFocus exactly? I'm fairly new to this. I searched online a bit and I have several places where I want the focus to change. For example, in the bonus question, if I use EditText and click on an item I created, the blinking cursor just won't go away! I want it to disappear if I click anywhere else on the screen. – harsh99 Aug 10 at 18:27
final EditText taskEditText = new EditText(MainActivity.this);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(taskEditText, InputMethodManager.SHOW_IMPLICIT);

Try to force your soft keyboard to spawn on your EditText maybe?

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.