Layout in Alertdialog

Asked

Viewed 1,165 times

0

I have this AlertDialog that shows on the other screen layout , in that layout has several EditText and a Button. How can I get the sword text of each of the edittext when I click on ok or the button on the layout that will appear in the AlertDialog. I tried to do with LayoutInflanter but I couldn’t. Look:

Context context = getBaseContext();
LayoutInflater inflant = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
final LinearLayout layout = (LinearLayout) inflant.inflate(R.layout.main, null);

AlertDialog.Builder dialogo = new AlertDialog.Builder(MainActivity.this);

dialogo.setTitle("Informe o Nome");
dialogo.setView(R.layout.main);
dialogo.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface di, int e) {
        ettext = (EditText) layout.findViewById(R.id.ettext);

        Toast.makeText(getBaseContext(), ettext.getText(), Toast.LENGTH_SHORT).show();
    }
});
dialogo.show();

2 answers

3


Use Layoutinflater to get a View that represents your xml file.

LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.main, null, false);

From the view, take the instances of your Edittext and Button:

EditText edtText= (EditText) view.findViewById(R.id.edit_text);
Button btn = (Button) view.findViewById(R.id.button);

If you need to use Linearlayout, assign an id to it and take the instance as shown above. Finally, add the View to your Dialog:

 dialogo.setView(view);

0

That’s how it worked

public void Dialog() {

    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    final LayoutInflater inflater = LayoutInflater.from(getContext());
    View view = inflater.inflate(R.layout.dialog_esqueci_senha, null, false);
    final EditText editText = view.findViewById(R.id.et_dialog_esqueci_senha_email);
    builder.setView(view)

            .setPositiveButton("Enviar", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    Toast.makeText(getContext(), ""+editText.getText().toString(), Toast.LENGTH_SHORT).show();
                }
            })
            .setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                }
            });
    builder.show();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.