1

I am very new to Java and I'm trying to keep things simple. Why doesn't this work? I have an XML layout with a EditText field and a Submit Button. I want to press it, an AlertDialog to pop up with the TextView of what I inputted into the EditText. What I tried keeps crashing:

        //CustomAlertDialogPopUp
public void submitButton(View v) {

    LayoutInflater inflater = getLayoutInflater();
    View alertLayout = inflater.inflate(R.layout.alertXML, null);

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Alert");
    alert.setView(alertLayout);

    AlertDialog dialog = alert.create();
    dialog.show();


    EditText text1 = (EditText)findViewById(R.id.inputText);
    TextView text2 = (TextView)findViewById(R.id.alertText);
    String result = text1.getText().toString();
    text2.setText(result);

}

Sorry if I sound stupid! Like I say, super new.

Andrei Zhytkevich
  • 8,039
  • 2
  • 31
  • 45
Lawrence Ferguson
  • 179
  • 2
  • 4
  • 11

1 Answers1

0

If you want to keep it simple, this will work:

For custom AlertDialog view you can refer to this question

public void submitButton(View v){
    new AlertDialog.Builder(this)
            .setTitle("Message")
            .setMessage(text1.getText().toString())
            .setPositiveButton("Dismiss", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            })
            .show();
}
Community
  • 1
  • 1
Bill
  • 4,506
  • 2
  • 18
  • 29