0

Im having trouble regarding dialogs, so Ive been re-reading the android docs several times over, and am still unsure about the following things and would really appreciate if anyone can answer my questions... Before i ask my questions ill show my code...

CustomDialog (Straight copy from android dev. site)

public class FireMissilesDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(inflater.inflate(R.layout.dialog_createlocation, null))
                .setTitle(R.string.dialog_createlocationtitle)
                // Add action buttons
                .setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        FireMissilesDialogFragment.this.getDialog().cancel();
                    }
                });
        return builder.create();
    }
}`

and here is the layout for the dialog(dialog_createlocation.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
    android:id="@+id/EditTextName"
    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/name"
    android:maxLines="1"/>
<EditText
    android:id="@+id/EditTextAddress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="16dp"
    android:fontFamily="sans-serif"
    android:hint="@string/address"
    android:maxLines="2"/>

Questions:/n

  1. In my main activity, I want to get the text from the two EditText in the dialog. Although Ive seen some SO questions about this but im so overwelmed and cant seem to understand the answers./n

2.Is it necessary for me to create this dialog in its own class?-can i just create it in my main activity(- without creating an inner class)?/n

3.Im confused with why to create a custom dialog, it has to extend a fragment-why not just an activity?/n

4.I create an instance of the above dialog in my main activity (which is not a fragment) and i got some issues doing this:

public void showNoticeDialog() {
// Create an instance of the dialog fragment and show it
DialogFragment dialog = new FireMissilesDialogFragment();
dialog.show(getSupportFragmentManager(), "NoticeDialogFragment");

}

Thanks!

Izak
  • 909
  • 9
  • 24

1 Answers1

1
  1. In my main activity, I want to get the text from the two EditText in the dialog. Although Ive seen some SO questions about this but im so overwelmed and cant seem to understand the answers.
EditText editTextName = dialog.getDialog().findViewById(R.id.EditTextName);
String name = editTextName.getText().toString();
  1. Is it necessary for me to create this dialog in its own class?-can i just create it in my main activity(- without creating an inner class)?

Yes, you can. AlertDialog just give you already present structure for your dialog. But to make your own just use Dialog Class.

3.Im confused with why to create a custom dialog, it has to extend a fragment-why not just an activity?

Its not necessary to use only Fragment for Dialog. as per second answer.

4.I create an instance of the above dialog in my main activity (which is not a fragment) and i got some issues doing this:

Post stacktrace or error log for this.

user370305
  • 108,599
  • 23
  • 164
  • 151
  • Thanks so much!!!!! ive been trying to do this for 3 days!!!! only q is that ive seen answers on SO that you need to inflate EditText through an inflater or something like that(http://stackoverflow.com/questions/8471348/how-do-i-get-edittext-value-from-a-custom-alertdialog-where-retrieval-of-values)-is that way required-how come i didnt need to do that – Izak Apr 21 '15 at 22:02
  • @Isaac - As you are not using EditText in your AlertDialog inner class or not making direct reference to it. Here we get reference of EditText in your Activity using `Dialog` reference and after calling `findViewById()`. Thats why.. :) – user370305 Apr 21 '15 at 23:45