GetText from EditText in Dialog Dialog

I have a DialogFragment like this

enter image description here

The dialog is called by clicking the plus button in MainActivity, below which it is initially empty. When I click "continue", I create a button, and while it works. I also want the line that I insert in the EditText in DialogFragment to be displayed on the button I just created. My problem is that I cannot get the line entered in EditText.

Here is the code:

Mainactivity

public class MainActivity extends FragmentActivity implements IProjectDialFrag {

    private ProjectDialogFragment projectDialFrag = new ProjectDialogFragment();

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        switch (id) {
            case R.id.action_settings:
                //TODO
                return true;
            case R.id.filters:
                Intent intent1 = new Intent(MainActivity.this, FiltersActivity.class);
                startActivity(intent1);
                return true;
            case R.id.action_new:
                return true;
            case R.id.add_button:
                Intent intent2 = new Intent(MainActivity.this, ButtonsActivity.class);
                startActivity(intent2);
                return true;
            case R.id.add_project:
                projectDialFrag.show(getFragmentManager(), "projectDialog");
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }

    @Override
    public void onDialogNegativeClick(DialogFragment dialog) {
        return;
    }

    @Override
    public void onDialogPositiveClick(DialogFragment dialog) { 
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.project_dialog_fragment, null);

        EditText editText = (EditText) view.findViewById(R.id.project_name);
        String projectName = editText.getText().toString();
        Button projectButton = new Button(this);
        projectButton.setText(projectName);
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.main_layout);
        linearLayout.addView(projectButton);

    }

}

DialogFrament

public class ProjectDialogFragment extends DialogFragment {

    private IProjectDialFrag iProjDialFrag;

    @SuppressLint("InflateParams")
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
         AlertDialog.Builder createProjectAlert = new AlertDialog.Builder(getActivity());

         createProjectAlert.setTitle("Create Project");

         LayoutInflater inflater = getActivity().getLayoutInflater();

         createProjectAlert.setView(inflater.inflate(R.layout.project_dialog_fragment, null))

            .setPositiveButton(R.string.conti_nue, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int id) {
                    iProjDialFrag.onDialogPositiveClick(ProjectDialogFragment.this);
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int id) {
                    iProjDialFrag.onDialogNegativeClick(ProjectDialogFragment.this);

                }
            });

         return createProjectAlert.create();

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        iProjDialFrag = (IProjectDialFrag) activity;
    }

}

Interface

public interface IProjectDialFrag {

    public void onDialogPositiveClick(DialogFragment dialog);
    public void onDialogNegativeClick(DialogFragment dialog);

}

and XML layout for reference:

DialogFragment Dialog Box

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <EditText 
        android:id="@+id/project_name" 
        android:inputType="text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="4dp"
        android:hint="@string/hint_project_alert" />

</LinearLayout>

MainActivity Layout

<LinearLayout 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:orientation="horizontal"
    android:id="@+id/main_layout">

</LinearLayout>

Where am I mistaken?

+4
source share
3 answers

findViewById :

EditText editText = (EditText) getDialog().findViewById(R.id.project_name);
if (editText != null) {
    Log.e("", "Value is: " + editText.getText());
} else {
    Log.e("", "EditText not found!");
}

: onCreateDialog.

+11

,

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

         createProjectAlert.setTitle("Create Project");

         LayoutInflater inflater = getActivity().getLayoutInflater();

         View view = mInflater.inflate(R.layout.project_dialog_fragment, null);
         EditText project_name = (EditText) view.findViewById(R.id.project_name);

         createProjectAlert.setView(inflater.inflate(R.layout.project_dialog_fragment, null))

            .setPositiveButton(R.string.conti_nue, new DialogInterface.OnClickListener() {

                @Override
                 public void onClick(DialogInterface dialog, int id) {
                    String projectName = editText.getText().toString();
                    iProjDialFrag.onDialogPositiveClick(ProjectDialogFragment.this);
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int id) {
                    iProjDialFrag.onDialogNegativeClick(ProjectDialogFragment.this);

                }
            });
+2

You should not bind ( findViewById()or ButterKnife.bind()) your views in the onDialogPositiveClick method, they will not work.

Always consider using fragment lifecycle callbacks onResume()or onStart()to bind a view. For example, in your case, the code would look like this:

@Override
public void onResume() {
    super.onResume();
    editText = (EditText) getDialog().findViewById(R.id.project_name);
}

Where is your editText- class variable ( EditText editText;)

0
source

All Articles