Android get text EditText to AlertDialog

3232 views java

I have to take the text from an AlertDialog.

For the AlertDialog I use a specific layout: custom_alertdialog

Instead for all the project use: another layout

I tried like this:

View layout = getLayoutInflater().Inflate(R.layout.custom_alertdialog,null);

Or so:

View layout = View.inflate(SimpleVideoStream.this, R.layout.custom_alertdialog, null);

But the text is not taken in either case. How can I do?

AlertDialog.Builder builder = new AlertDialog.Builder(SimpleVideoStream.this);
        builder.setTitle("Add Subtitle");

                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        View inflatedView = getLayoutInflater().inflate(R.layout.custom_alertdialog, null);
                        final View layout = View.inflate(SimpleVideoStream.this, R.layout.custom_alertdialog, null);

                        EditText url_ele = (EditText) layout.findViewById(;
                        String sub = url_ele.getText().toString();
                        if (!sub.equals("")) {
                            showSub = !showSub;
                            Format textFormat = Format.createTextSampleFormat(null, MimeTypes.APPLICATION_SUBRIP,
                                    null, Format.NO_VALUE, Format.NO_VALUE, "en", null, Format.OFFSET_SAMPLE_RELATIVE);
                            MediaSource textMediaSource = new SingleSampleMediaSource.Factory(dataSourceFactory)
                                    .createMediaSource(Uri.parse(sub), textFormat, C.TIME_UNSET);

                            videoSource = new MergingMediaSource(videoSource, textMediaSource);
                            player.prepare(videoSource, false, false);

                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

        AlertDialog alert = builder.create();
        LayoutInflater inflater = getLayoutInflater();
        View dialoglayout = inflater.inflate(R.layout.custom_alertdialog, null);

answered question

1 Answer


When you inflate a View, it doesn't share an instance with any other times you've inflated it. So what you passed to setView() is completely different from what you're calling findViewById() on. You need to create only one instance and then reference that.

For example:

View view1 = inflater.inflate(R.layout.some_layout, null);
View view2 = inflater.inflate(R.layout.some_layout, null);

EditText text1 = view1.findViewById(;
EditText text2 = view2.findViewById(;


String text2text = text2.getText().toString(); //this will be null, because text1 and text2 aren't the same instance, because view1 and view2 aren't the same instance.


View dialoglayout = inflater.inflate(R.layout.custom_alertdialog, null);

above where you set the positive button, and make it final:

final View dialoglayout = inflater.inflate(R.layout.custom_alertdialog, null);

Remove both inflatedView and layout from inside your listener, and use dialogLayout:

EditText url_ele = (EditText) dialogLayout.findViewById(;

posted this

Have an answer?


Please login first before posting an answer.