Question

working on a dialog to pop up that inside if it has two textEdit's. I would like to get the information from the text edits on submit, but dont know how. I also dont know how to do a submit or a cancel button for a plain dialog ( i know you can on an alertDialog with "setNegativeButton").

So how do I add a submit or cancel to a regular dialog? this is what I am working with thus far:

public void changeEmail(View v){

    Dialog dialog =  new Dialog(this);
    dialog.setContentView(R.layout.change_email_dialog);
    dialog.setTitle("Enter your new email");
    dialog.show();


}

I guess also I was wondering if someone could quickly explain how I would get the info from the two textEdits that are inside of the layout that the dialog is using?

Was it helpful?

Solution

create a layout file: custom.xml

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

    <TextView
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFF" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFF"/>

</RelativeLayout>

create the following onClick function on click of the button(when you want the dialog box to appear):

button.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View arg0) {

            // custom dialog
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.custom);
            dialog.setTitle("Title");

            // set the custom dialog components - text, image and button
            TextView text = (TextView) dialog.findViewById(R.id.text1);
            text.setText("Text view 1");

            TextView text = (TextView) dialog.findViewById(R.id.text2);
            text.setText("Text view 2");

            dialog.show();
          }
        });

Here is a link to the tutorial for getting exactly what you want.

Here is a link to a question on Stack Overflow which provides information about custom dialog box.

OTHER TIPS

     protected void showCustomDialog(final int position) {
            final Dialog dialog;
            dialog = new Dialog(getActivity());
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.alert_enter);
            EditText editText;
            EditText editText2;
            editText = (EditText)dialog.findViewById(R.id.abc);
            editText2 = (EditText)dialog.findViewById(R.id.abcd);
            buttonOK = (Button)dialog.findViewById(R.id.quantity_ok_button);
            buttonOK.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                        String text=editText.getText().toString();
                        String text2=editText2.getText().toString();
                        dialog.dismiss();
                }
            });
}

You can try this code. I have done this to get a value from dialog box. Use the values text and text 2.

make your own dialog

class YourDialog extends Dialog {

private TextView mTextView1, mTextView1;
private Button mButton1, mButton2;

public YourDialog(Context context) {
    super(context);


}

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

    // init your component from layout
    // and add listiner to them

}

}

I had been facing exactly the same hurdle.

Here is what i did after a research.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:weightSum="1"
tools:context="com.example.sinnaren.simply.MainActivity">
<Button
    android:id="@+id/button_add_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="Add a title"
    android:textAllCaps="false"/>
</LinearLayout>

dialog_add_title_list.xml

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

<EditText
    android:id="@+id/edit_add_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="title name"
    android:inputType="textCapWords"
    android:textAllCaps="false" />

<EditText
    android:id="@+id/edit_add_desc"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="title description"
    android:inputType="textCapSentences"
    android:textAllCaps="false" />
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity implements myDialogFragment.MyDialogListener {
private Button buttonAdd;
private myDialogFragment dialog;

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

    dialog = new myDialogFragment();
    buttonAdd = (Button)findViewById(R.id.button_add_title);

    buttonAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("MainActivity", "onClick: going to show dialog");
            dialog.show(getSupportFragmentManager(), "myCustomAlert !!");

        }
    });

}

@Override
public void onDialogPositiveClick(DialogFragment dialog, String name, String desc) {
    Toast.makeText(getApplicationContext(), "name :" + name + "description :" + desc, Toast.LENGTH_SHORT).show();
}

@Override
public void onDialogNegativeClick(DialogFragment  dialog) {
    Toast.makeText(getApplicationContext(), "Cancel clicked", Toast.LENGTH_SHORT).show();
    }
}

myDialogFragment.java

public class myDialogFragment extends DialogFragment {
private Dialog alert;
MyDialogListener dialogListener;

public interface MyDialogListener {
    public void onDialogPositiveClick(DialogFragment dialog, String namel, String desc);
    public void onDialogNegativeClick(DialogFragment dialog);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    dialogListener = (MyDialogListener)context;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Log.w("onCreateDialog", "onCreateDialog: onCreateDialog invoked");
    AlertDialog.Builder alertDialogBuider = new AlertDialog.Builder(getActivity());

    alertDialogBuider.setTitle("Add a Title");

    LayoutInflater inflater = getActivity().getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.dialog_add_title_list, null);
    alertDialogBuider.setView(dialogView);
    alertDialogBuider.setPositiveButton("Add", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            EditText textChapterName = (EditText)dialogView.findViewById(R.id.edit_add_title);
            EditText textChapterDesc = (EditText)dialogView.findViewById(R.id.edit_add_desc);
            dialogListener.onDialogPositiveClick(myDialogFragment.this,
                    textChapterName.getText().toString(),
                    textChapterDesc.getText().toString());
        }
    });

    alertDialogBuider.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialogListener.onDialogNegativeClick(myDialogFragment.this);
        }
    });

    return alertDialogBuider.create();
}

@Override
public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
    Toast.makeText(getActivity(), "Dialog: onDismiss", Toast.LENGTH_SHORT).show();
    }
}

When using the support library, be sure that you import android.support.v4.app.DialogFragment class and not android.app.DialogFragment.

dialog created

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top