Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing EditText inside of an AlertDialog

I'm looking for a way to resize (so it doesn't touch the edges) an EditText inside of an AlertDialog.

enter image description here

My sample code

AlertDialog.Builder alert = new AlertDialog.Builder(myActivity.this);

alert.setTitle("Test");

EditText textBox = new EditText(myActivity.this);
alert.setView(textBox);

alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
    // do nothing 
    }
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
// do nothing
}
});

alert.show();

I've tried solutions provided here with no success:

  • An Answer
  • Another Answer
like image 536
ThePaul Avatar asked Feb 19 '12 01:02

ThePaul


1 Answers

Add your EditText inside a LinearLayout and set margins to that layout.

AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setTitle("Test");

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
     LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(20, 0, 30, 0);

EditText textBox = new EditText(myActivity.this);
layout.addView(textBox, params);

alert.setView(layout);

alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
    // do nothing 
    }
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
// do nothing
}
});

alert.show();
like image 55
waqaslam Avatar answered Sep 30 '22 16:09

waqaslam