Reputation: 2101
The Question:
The Assumptions:
The Minor Details:
The EditText has the following types:
The height/width of the AlertDialog is set in pixels, scaled via density (see the code);
The Confusion:
The Code:
private void show_activation_prompt()
{
// =============================================================
// ======== create EditText which filters text =================
// =============================================================
// filter for alphanumerics, insert dashes during entry, capitalize, and turn off suggestions
final TextWatcher textEditorWatcher = new SafeTextWatcher(); //custom
final LayoutParams lparams = new LayoutParams(100,100);
final EditText edittext= new EditTextPersist(mContext);
edittext.addTextChangedListener(textEditorWatcher);
edittext.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS |
InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
);
edittext.setLayoutParams(lparams);
//edittext.setEms(10); //doesn't work either
// =============================================================
// ======== create alert dlg, set btn callbacks ================
// =============================================================
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setPositiveButton("Upgrade", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
String activationKey = edittext.getText().toString();
on_key_entered(activationKey);
}
} );
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
post_activation_result(QlmLicense.ACTIVATION_INVALID);
}
} );
// =============================================================
// ======== configure the AlertDialog/EditText =================
// =============================================================
int nWidth, nHeight;
final float scale = mContext.getResources().getDisplayMetrics().density;
nWidth = (int) (900 * scale + 0.5f);
nHeight = (int) (500 * scale + 0.5f);
builder.setView(edittext)
.setMessage("Enter an Activation Key:")
.setTitle("Upgrade From Trial Version")
.setCancelable(false)
.show()
.getWindow()
.setLayout(nWidth,nHeight);
}
Upvotes: 2
Views: 1418
Reputation: 1469
I had simple answer for your question to apply LayoutParams
for Edittext
in AlertDialog
by change LayoutParams
into FrameLayout.LayoutParams
and call edittext.setLayoutParams(lparams);
after calling dialog.show()
:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
int nWidth, nHeight;
final float scale = getApplicationContext().getResources().getDisplayMetrics().density;
nWidth = (int) (900 * scale + 0.5f);
nHeight = (int) (500 * scale + 0.5f);
final FrameLayout.LayoutParams lparams = new FrameLayout.LayoutParams(100,100);
final EditText edittext= new EditText(this);
//edittext.addTextChangedListener(textEditorWatcher);
edittext.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS |
InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
);
// =============================================================
// ======== create alert dlg, set btn callbacks ================
// =============================================================
builder.setPositiveButton("Upgrade", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
String activationKey = edittext.getText().toString();
//on_key_entered(activationKey);
Toast.makeText(getApplicationContext(), activationKey, Toast.LENGTH_LONG).show();
}
} );
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//post_activation_result(QlmLicense.ACTIVATION_INVALID);
}
});
builder.setView(edittext)
.setMessage("Enter an Activation Key:")
.setTitle("Upgrade From Trial Version")
.setCancelable(false)
.show()
.getWindow()
.setLayout(nWidth, nHeight);
//Change setLayoutParams to here
edittext.setLayoutParams(lparams);
Upvotes: 4