Setting fields for EditText in AlertDialog

I created an AlertDialog with EditText in my Android app, but the default fields look really off. I tried to specify the fields as follows:

android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(SpinActivity.this); builder.setTitle("Edit Spin Tags"); builder.setMessage("(separate tags with commas)"); // set input int margin = 10; final EditText input = new EditText(SpinActivity.this); input.setSingleLine(); input.setText(spinTags.toString().replace("[", "").replace("]", "")); builder.setView(input, margin, 0, margin, 0); 

However, from the image below, you can see that it does not apply the desired effect.

enter image description here

Other parameters I tried, including placing input in LinearLayout and setting fields using LayoutParams, before setting the AlertDialog view to LinearLayout.

How to set fields for EditText in AlertDialog?

+7
android alertdialog
source share
3 answers

Actually your solution works fine, except that builder.setView(input, margin, 0, margin, 0); accepts arguments in pixel values. Thus, the value of 20 is very small. Use either a larger margin value, for example. at 100s. or use this function to convert from dp to pixels

 public static int dpToPx(int dp) { return (int) (dp * Resources.getSystem().getDisplayMetrics().density); } 

and then

 int margin = dpToPx(20); 
+6
source share

You can have a LinearLayout as the parent EditText element. Then specify the border of the EditText .

 private void createDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Demo"); builder.setMessage("Some demo message"); LinearLayout parentLayout = new LinearLayout(this); EditText editText = new EditText(this); editText.setHint("Some text"); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); // call the dimen resource having value in dp: 16dp int left = getPixelValue((int)getResources().getDimension(R.dimen.activity_horizontal_margin)); int top = getPixelValue((int)getResources().getDimension(R.dimen.activity_horizontal_margin)); int right = getPixelValue((int)getResources().getDimension(R.dimen.activity_horizontal_margin)); int bottom = getPixelValue((int)getResources().getDimension(R.dimen.activity_horizontal_margin)); // this will set the margins layoutParams.setMargins(left, top, right, bottom); editText.setLayoutParams(layoutParams); parentLayout.addView(editText); builder.setView(parentLayout); builder.setPositiveButton("OK", null); builder.create().show(); } private int getPixelValue(int dp) { Resources resources = getResources(); return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics()); } 

You can find more detailed information at http://www.pcsalt.com/android/set-margins-in-dp-programmatically-android/

+2
source share

Define your Edittext in another layout file when setting the correct fields in this layout. Fill this layout and then set it as a view dialog.

0
source share

All Articles