N Sharma
N Sharma

Reputation: 34497

How to set the left and right margin in dp of EditText in Alert Dialog

Hello I am trying to set the left and right margin of the EditText which is in AlertDialog. I want to set them in dp so that it will work correctly to all device densities. I am trying to do this same like below but still no margin at all.

AlertDialog.Builder alerBuilder = new AlertDialog.Builder(PhotoOptions.this);
alerBuilder.setMessage(getString(R.string.input_compress_value));

final EditText compressValueEditText = new EditText(PhotoOptions.this);
compressValueEditText.setInputType(InputType.TYPE_CLASS_NUMBER);

LayoutParams params = new LayoutParams(
        LayoutParams.WRAP_CONTENT,      
        LayoutParams.WRAP_CONTENT
);
params.leftMargin = 100;

alerBuilder.setView(compressValueEditText);
compressValueEditText.setLayoutParams(params);

Thanks in advance.

Upvotes: 1

Views: 616

Answers (1)

cygery
cygery

Reputation: 2319

You have to convert the dp value to a pixel value, e.g., via

params.leftMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());

Upvotes: 1

Related Questions