How to Make a Edittext Box in a Dialog

How to make a edittext box in a dialog

Use Activtiy Context

Replace this

  final EditText input = new EditText(this);

By

  final EditText input = new EditText(MainActivity.this);  
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
alertDialog.setView(input); // uncomment this line

EditText in Alert Dialog Android

The reason you are not able to access editTextField is because of it is declared as local variable in alertDialog() method.

If in case you want to keep that variable as local ,you need to set the listener method directly in that method as i show below.

private EditText = new EditText(this);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Message")
.setView(inputEditTextField)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String editTextInput = inputEditTextField.getText().toString();
Log.d("onclick","editext value is: "+ editTextInput);
}
})
.setNegativeButton("Cancel", null)
.create();
dialog.show();


Else you can replace this line from alertDialog() method with this below line

editTextField = new EditText(this.getContext());



you need add this line above onCreate method

EditTextField editTextField;

How to make a Edit-text box in set error message using without toast ( Alter dialog box ) using Android?

You can extend Dialog and create your own dialog

public class CustomDialog extends Dialog implements View.OnClickListener {
private boolean success = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_dialog);
Button positive = (Button) findViewById(R.id.button_positive);
Button negative = (Button) findViewById(R.id.button_negative);
EditText field = (EditText) findViewById(R.id.field);

positive.setOnClickListener(this);
negative.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button_positive:
onPositiveButtonClicked();
break;
case R.id.button_negative:
//onNegativeButtonClicked();
break;
}
}

private void onPositiveButtonClicked() {
if(verifyForm()) {
success = true;
dismiss();
}
}

public boolean isSuccess() {
return success;
}

private boolean verifyForm() {
boolean valid = true;
/* verify each field and setError() if not valid */
if(!TextUtils.isEmpty(field.getText())) { //or any other condition
valid = false;
field.setError("error message");
}
return valid;
}
}

You can show your CustomDialog like this

final CustomDialog customDialog = new CustomDialog();
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if(customDialog.isSuccess()) {
//update your folder manager
}
}
}
customDialog.show();

how to put values of EditText in the dialogue box

Hy @Rohan, sorry for the delay but I was off. So, here is my solution, I hope I understood well your problem. This is the code for btn2:

btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Ascode.this);

String a;
a = editText1.getText().toString();

a = a.replace(" ","");
byte b[] = a.getBytes();
String[] s = a.split("(?!^)");
String message = "";

for(int i=0;i<s.length();i++)
{
message += "Value of: \n"+s[i]+ " is " +b[i]+"\n";
}

alertDialogBuilder.setTitle("Detailed Output is");
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setMessage(message)
.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});

AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});

Hope this helps

EditText value of custom AlertDialog to Textview

Okay, so you haven't really mentioned as to why you prefer to use the DialogFragment rather than just an AlertDialog as what Victor Holotescu answered. But here goes, I tried out your code and managed to receive a NullPointerException when I try to click the Update Button. So I looked at it and modified the code, here it is:

MainActivity

public class MainActivity extends AppCompatActivity {

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

final TextView tv = (TextView) findViewById(R.id.tv_id);
tv.setText("Initial value");

tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
UpdateDialogFragment updDiag = new UpdateDialogFragment().newInstance(tv); // Passed the TextView here
updDiag.show(getFragmentManager(), "dialog");
}
});
}

public static class UpdateDialogFragment extends DialogFragment {
String value; // I try to get the value of EditText like this, but it doesn't work
TextView tvToEdit;

public UpdateDialogFragment newInstance(TextView tvToEdit){
UpdateDialogFragment udf = new UpdateDialogFragment();
udf.tvToEdit = tvToEdit;
return udf;
}

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

builder.setView(inflater.inflate(R.layout.edit_tv, null))
.setPositiveButton("Update", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
EditText et = (EditText) UpdateDialogFragment.this.getDialog().findViewById(R.id.et_tv);
value = et.getText().toString();
tvToEdit.setText(value);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
UpdateDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
}
}

I added some TODO comments (labeled with Read comments below. :)) inside the code, check them out. I tested it and it runs properly. Hope this was able to help you in some way. For more information regarding AlertDialogs and DialogFragments, here's a good post -- DialogFragment advantages over AlertDialog.

EDIT

Okay. So I edited it where you just pass a TextView to the UpdateDialogFragment by creating a newInstance method -- referenced it here SetText of TextView in DialogFragment from Calling Activity -- Hope this helps. :)

How to add two edit text fields or views in an AlertDialog box?

See Creating a Custom Layout in android.

Sample Image

EDIT

alertDialog.setTitle("Values");
final EditText quantity = new EditText(SecondScan.this);
final EditText lot = new EditText(SecondScan.this);

quantity.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
lot.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

Project=arr[0].toString();
Item=arr[1].toString();

LinearLayout ll=new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(quantity);
ll.addView(lot);
alertDialog.setView(ll);

alertDialog.setCancelable(false);
alertDialog.setPositiveButton("Update", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//ACTION
}
});

AlertDialog alert = alertDialog.create();
alert.show();


Related Topics



Leave a reply



Submit