Search code examples
androidandroid-edittexttextviewandroid-dialogfragmentandroid-dialog

EditText value of custom AlertDialog to Textview


I found some similar questions here, but all of their were either really confusing for such a newbie like me or didn't help to solve my problem: I have a TextView tv. By click on it I want to display a custom AlertDialog with EditText + Cancel- and Update-Buttons. By click on Update-Button of my Dialog I want to replace the text of the same TextView (tv) with the value of EditText from Dialog. Here is one of my attepmts:

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

    final TextView tv̶̶C̶̶l̶̶i̶̶c̶̶k̶̶e̶̶d̶ = (TextView) findViewById(R.id.tv_id);
    tv.setText("Initial value");

    tv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            UpdateDialogFragment updDiag = new UpdateDialogFragment();
            updDiag.show(getFragmentManager(), "dialog");
            tv.setText(updDiag.value); // I try to get the value of EditText like this, but it doesn't work
        }
        });
}

public class UpdateDialogFragment extends DialogFragment {
    String value; // I try to get the value of EditText like this, but it doesn't work
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();

        builder.setView(inflater.inflate(R.layout.edit_tv, null))
                .setPositiveButton("Update", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        EditText et = (EditText) UpdateDialogFragment.this.getDialog().findViewById(R.id.et_tv);
                        value = et.getText().toString();  // I try to get the value of EditText like this, but it doesn't work
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        UpdateDialogFragment.this.getDialog().cancel();
                    }
                });
        return builder.create();
    }
}

And my edit_tv.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/et_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_marginTop="40dp"
        android:hint="Enter text ..."
        android:layout_gravity="center_horizontal">

        <requestFocus />
    </EditText>

</LinearLayout>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.yev.tabletasting.MainActivity">

    <TextView
        android:id="@+id/tv_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />    

</RelativeLayout>

In my case the Text of tv will be setted to "" (probably null) after click on "Update". I would be thankful for every advice, thanking you in anticipation!


Solution

  • Okay, so you haven't really mentioned as to why you prefer to use the DialogFragment rather than just an AlertDialog as what Victor Holotescu answered. But here goes, I tried out your code and managed to receive a NullPointerException when I try to click the Update Button. So I looked at it and modified the code, here it is:

    MainActivity

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            final TextView tv = (TextView) findViewById(R.id.tv_id);
            tv.setText("Initial value");
    
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    UpdateDialogFragment updDiag = new UpdateDialogFragment().newInstance(tv); // Passed the TextView here
                    updDiag.show(getFragmentManager(), "dialog");
                }
            });
        }
    
        public static class UpdateDialogFragment extends DialogFragment {
            String value; // I try to get the value of EditText like this, but it doesn't work
            TextView tvToEdit;
    
            public UpdateDialogFragment newInstance(TextView tvToEdit){
                UpdateDialogFragment udf = new UpdateDialogFragment();
                udf.tvToEdit = tvToEdit;
                return udf;
            }
    
            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                LayoutInflater inflater = getActivity().getLayoutInflater();
    
                builder.setView(inflater.inflate(R.layout.edit_tv, null))
                        .setPositiveButton("Update", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                EditText et = (EditText) UpdateDialogFragment.this.getDialog().findViewById(R.id.et_tv);
                                value = et.getText().toString();
                                tvToEdit.setText(value);
                            }
                        })
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                UpdateDialogFragment.this.getDialog().cancel();
                            }
                        });
                return builder.create();
            }
        }
    }
    

    I added some TODO comments (labeled with Read comments below. :)) inside the code, check them out. I tested it and it runs properly. Hope this was able to help you in some way. For more information regarding AlertDialogs and DialogFragments, here's a good post -- DialogFragment advantages over AlertDialog.

    EDIT

    Okay. So I edited it where you just pass a TextView to the UpdateDialogFragment by creating a newInstance method -- referenced it here SetText of TextView in DialogFragment from Calling Activity -- Hope this helps. :)