import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
-// If we don't use `android.support.v7.app.AlertDialog` instead of `android.app.AlertDialog` then the dialog will be covered by the keyboard.
-import android.support.v7.app.AlertDialog;
-// We have to use `AppCompatDialogFragment` instead of `DialogFragment` or an error is produced on API <=22.
+// We have to use `AppCompatDialogFragment` instead of `DialogFragment` or an error is produced on API <= 22.
import android.support.v7.app.AppCompatDialogFragment;
import android.view.KeyEvent;
import android.view.View;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.KeyEvent;
import android.view.View;
// Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
assert alertDialog.getWindow() != null;
// Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
assert alertDialog.getWindow() != null;
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
// We need to show the `AlertDialog` before we can call `setOnKeyListener()` below.
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
// We need to show the `AlertDialog` before we can call `setOnKeyListener()` below.
EditText createBookmarkNameEditText = (EditText) alertDialog.findViewById(R.id.create_bookmark_name_edittext);
assert createBookmarkNameEditText != null; // Remove the warning below that `createBookmarkNameEditText` might be `null`.
createBookmarkNameEditText.setOnKeyListener(new View.OnKeyListener() {
EditText createBookmarkNameEditText = (EditText) alertDialog.findViewById(R.id.create_bookmark_name_edittext);
assert createBookmarkNameEditText != null; // Remove the warning below that `createBookmarkNameEditText` might be `null`.
createBookmarkNameEditText.setOnKeyListener(new View.OnKeyListener() {
- public boolean onKey(View v, int keyCode, KeyEvent event) {
- // If the event is a key-down on the `enter` button, select the `PositiveButton` `Create`.
- if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
+ public boolean onKey(View view, int keyCode, KeyEvent event) {
+ // If the event is a key-down on the `enter` key, select the `PositiveButton` `Create`.
+ if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) {
// Trigger `createBookmarkListener` and return the `DialogFragment` to the parent activity.
createBookmarkListener.onCreateBookmark(CreateBookmark.this);
// Manually dismiss the `AlertDialog`.
// Trigger `createBookmarkListener` and return the `DialogFragment` to the parent activity.
createBookmarkListener.onCreateBookmark(CreateBookmark.this);
// Manually dismiss the `AlertDialog`.
// Allow the `enter` key on the keyboard to create the bookmark from `create_bookmark_url_edittext`.
createBookmarkUrlEditText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// Allow the `enter` key on the keyboard to create the bookmark from `create_bookmark_url_edittext`.
createBookmarkUrlEditText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
- // If the event is a key-down on the "enter" button, select the PositiveButton "Create".
- if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
+ // If the event is a key-down on the "enter" key, select the PositiveButton "Create".
+ if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) {
// Trigger `createBookmarkListener` and return the DialogFragment to the parent activity.
createBookmarkListener.onCreateBookmark(CreateBookmark.this);
// Manually dismiss the `AlertDialog`.
// Trigger `createBookmarkListener` and return the DialogFragment to the parent activity.
createBookmarkListener.onCreateBookmark(CreateBookmark.this);
// Manually dismiss the `AlertDialog`.