ThePaul
ThePaul

Reputation: 105

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:

Upvotes: 6

Views: 4969

Answers (1)

waqaslam
waqaslam

Reputation: 68177

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();

Upvotes: 27

Related Questions