This Fragment dialog will do the job for you. Please note that the dialog will remain open after rotating the screen, saving any text that the user has already typed. If you do not want this, you need to remove the fragment in your activity at Stop. The signature of newInstance can be changed to everything you need.
import android.app.Activity; import android.app.Dialog; import android.app.DialogFragment; import android.content.DialogInterface; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AlertDialog; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; public class TextViewDialogFragment extends DialogFragment implements DialogInterface.OnClickListener, DialogInterface.OnShowListener, TextWatcher { final static private String TITLE = "title", MESSAGE = "message", IDENTIFIER = "identifier", INPUT_TYPE = "inputType", POSITIVE_TEXT = "pText", NEGATIVE_TEXT = "nText", CANCELABLE = "cancelable"; public TextViewDialogFragment() { super(); } static public TextViewDialogFragment newInstance(int title, @Nullable String message, int identifier, int inputType, int positiveText, int negativeText, boolean cancelable) { TextViewDialogFragment fragement = new TextViewDialogFragment(); Bundle args = new Bundle(); args.putInt(TITLE, title); args.putString(MESSAGE, message); args.putInt(IDENTIFIER, identifier); args.putInt(INPUT_TYPE, inputType); args.putInt(POSITIVE_TEXT, positiveText); args.putInt(NEGATIVE_TEXT, negativeText); args.putBoolean(CANCELABLE, cancelable); fragement.setArguments(args); return fragement; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Activity activity = getActivity(); Bundle args = getArguments(); EditText input = new EditText(activity); input.setInputType(args.getInt(INPUT_TYPE)); input.setId(R.id.dialog_edit_text); input.addTextChangedListener(this); AlertDialog.Builder alert = new AlertDialog.Builder(activity); alert.setCancelable(args.getBoolean(CANCELABLE)).setTitle(args.getInt(TITLE)).setMessage(args.getString(MESSAGE)).setView(input).setPositiveButton(args.getInt(POSITIVE_TEXT), this); int negativeText = args.getInt(NEGATIVE_TEXT); if (negativeText != 0) { alert.setNegativeButton(negativeText, this); } AlertDialog dialog = alert.create(); dialog.setOnShowListener(this); return dialog; } @Override public void onShow(DialogInterface dialog) {
Add tools for your activity (any type of activity is in order):
public class Myctivity extends AppCompatActivity implements TextViewDialogFragment.Callbacks { ... }
Create a diaglogFragment in your activity as follows:
final static int SOMETHING = 1; myDF = TextViewDialogFragment.newInstance(R.string.my_title, "my message", SOMETHING, InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES, R.string.yay, android.R.string.cancel, true);
Process the result in your activity as follows:
@Override public void onTextViewDialogResult(int which, int identity, String text) { if (which == AlertDialog.BUTTON_NEGATIVE) {
You need to create a resource identifier, so add this resource somewhere under res / values
<?xml version="1.0" encoding="utf-8"?> <resources> <item name="dialog_edit_text" type="id"/> </resources>