0

I am trying to add a setHint() to AlertDialog but it wouldn't show up ?

AlertDialog.Builder alertDialog = new AlertDialog.Builder(TestActivityForDialog.this);

    // Setting Dialog Title
    alertDialog.setTitle("Search By Name");

    // Setting Dialog Message
    alertDialog.setMessage("Please enter name:");

    // Setting Icon to Dialog
    alertDialog.setIcon(R.mipmap.ic_launcher);

    //set editText ----
    final EditText input = new EditText(this);
    input.setHint("Last Name (4 - 10 Chars)");
    input.setHintTextColor(Color.YELLOW);
    alertDialog.setView(input);
ML13
  • 895
  • 8
  • 15

1 Answers1

0

You forgot to link up EditText with dialog, like below

AlertDialog.Builder alertDialog = new AlertDialog.Builder(TestActivityForDialog.this);

// Setting Dialog Title
alertDialog.setTitle("Search By Name");

// Setting Dialog Message
alertDialog.setMessage("Please enter name:");

// Setting Icon to Dialog
alertDialog.setIcon(R.mipmap.ic_launcher);

EditText input = (EditText) dialog.findViewById(R.id.editbox);  //THIS GUY
input.setHint("Last Name (4 - 10 Chars)");
input.setHintTextColor(Color.YELLOW);
alertDialog.setView(input);

Read also this: How to give the hint of edittextbox of dialog which is created by code in android


EDIT: According to How to add TextView and EditText using default AlertDialog programmatically I'd created a custom AlertDialog using parameters above.

Here is a code:

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

    LinearLayout layout = new LinearLayout(this);
    LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.setLayoutParams(parms);

    layout.setGravity(Gravity.CLIP_VERTICAL);
    layout.setPadding(2, 2, 2, 2);

    //EditText
    EditText input = new EditText(this);
    input.setHint("Last Name (4 - 10 Chars)");
    input.setHintTextColor(Color.YELLOW);

    layout.addView(input, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));

    alertDialogBuilder.setView(layout);
    alertDialogBuilder.setTitle("Search By Name");
    alertDialogBuilder.setMessage("Please enter name:");
    alertDialogBuilder.setIcon(R.mipmap.ic_launcher);
    alertDialogBuilder.setCancelable(false);

    AlertDialog alertDialog = alertDialogBuilder.create();

    try {
        alertDialog.show();
    } catch (Exception e) {
        // WindowManager$BadTokenException will be caught and the app would
        // not display the 'Force Close' message
        e.printStackTrace();
    }

}

enter image description hereenter image description here

Hope it help

Community
  • 1
  • 1
piotrek1543
  • 19,130
  • 7
  • 81
  • 94