Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText setText not displaying on a Dialog Fragment

I'm kinda new to Android app development, and don't know how to debug this. So I'm trying to do EditText.setText on a DialogFragment. It prints out the right string on the log, but still showing the old string based on .xml file("Enter item here."). Is there any way to update it?

public class MyAlertDialogFragment extends DialogFragment{
public MyAlertDialogFragment() {}

public static MyAlertDialogFragment newInstance(String desc) {
    MyAlertDialogFragment frag = new MyAlertDialogFragment();
    Bundle args = new Bundle();
    args.putString("desc", desc);
    frag.setArguments(args);
    return frag;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
    alertDialogBuilder.setView(R.layout.custom_dialog);
    alertDialogBuilder.setTitle("Edit to-do list");

    final View layout = View.inflate(MainActivity.getAppContext(), R.layout.custom_dialog, null);
    final EditText mEditText = (EditText) layout.findViewById(R.id.EditDesc);
    String desc = getArguments().getString("desc");
    mEditText.setText(desc);
    System.out.println("EditText is " + mEditText.getText().toString());

    alertDialogBuilder.setPositiveButton("OK",  new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // on success
            EditTodoFragment.EditTodoFragmentListener listener = (EditTodoFragment.EditTodoFragmentListener)getActivity();
            listener.onFinishEditDialog(mEditText.getText().toString());
        }
    });
    alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    return alertDialogBuilder.create();
}

}

The log file

07-06 21:04:20.738 4115-4115/com.example.light.todolist I/System.out: EditText is Doing grocery

The xml file is:

<TextView
    android:layout_width="238dp"
    android:layout_height="wrap_content"
    android:text="Edit Item Below:"
    android:id="@+id/EditTitle"
    android:layout_weight="0.04" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/EditDesc"
    android:text="Enter item here.">
<requestFocus />
</EditText>

like image 751
user3315093 Avatar asked Jul 07 '16 01:07

user3315093


2 Answers

You don't use layout view for Dialog so that you can't see text on EditText. Please change as here:

 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
    alertDialogBuilder.setTitle("Edit to-do list");

    final View layout = View.inflate(MainActivity.getAppContext(), R.layout.custom_dialog, null);
    alertDialogBuilder.setView(layout );
like image 109
mdtuyen Avatar answered Oct 24 '22 00:10

mdtuyen


Try this:

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

LayoutInflater inflater = getActivity().getLayoutInflater();
View customView = inflater.inflate(R.layout.custom_dialog, null);

alertDialogBuilder.setView(customView);
alertDialogBuilder.setTitle("Edit to-do list");

final EditText mEditText = (EditText) customView.findViewById(R.id.EditDesc);
String desc = getArguments().getString("desc");
mEditText.setText(desc);

alertDialogBuilder.setPositiveButton("OK",  new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // on success
        EditTodoFragment.EditTodoFragmentListener listener = (EditTodoFragment.EditTodoFragmentListener)getActivity();
        listener.onFinishEditDialog(mEditText.getText().toString());
    }
});
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
    }
});

return alertDialogBuilder.create();
like image 2
Tiago Ribeiro Avatar answered Oct 23 '22 23:10

Tiago Ribeiro