So I have a dialog in which the user inputs data, here it is the user’s age. So I would like that when/if the user leaves the edittext blank, the edittext will do the shake animation like that in the api demos. So far I cannot get the dialog from not dismissing when invalid info is inputed. Thanks.
mInflater = (LayoutInflater) Information.this.getSystemService(LAYOUT_INFLATER_SERVICE);
mLayout = mInflater.inflate(R.layout.agedialog, null);
mAgeEditText = (EditText) mLayout.findViewById(R.id.AgeEditText);
mAgeResultTextView = (TextView) findViewById(R.id.AgeResultTextView);
final Animation shake = AnimationUtils.loadAnimation(Information.this, R.anim.shake);
mInputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mInputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
new AlertDialog.Builder(Information.this).setView(mLayout).setTitle(R.string.EnterYourAge)
.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mInputManager.hideSoftInputFromWindow(mAgeEditText.getWindowToken(), 0);
if (TextUtils.isEmpty(mAgeEditText.getText())) {
mAgeEditText.startAnimation(shake);
// here the dialog dismisses even if I call startAnimation
}
else {
HelperClass.getInstance().setAge(Integer.parseInt(mAgeEditText.getText().toString()));
mAgeResultTextView.setText(HelperClass.getInstance().getAge());
}
}
}).setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mInputManager.hideSoftInputFromWindow(mAgeEditText.getWindowToken(), 0);
dialog.cancel();
}
}).show();
Ok so I figured out the answer a long time after this was asked but just in case someone wants this I thought it was wise to go ahead and post the answer.
Basically this can NOT be done with the Alert Dialog, it has to do with the behavior when the button and negative buttons are clicked. However it can be done with a regular Dialog. You need a few animation xmls, and those can be found in the api demo’s anim folder under res (The original java file is Animation1.java in api demos so you can check that out). I also made it so that the keyboard comes up ready for the user to type. You can rid of that by deleting all the inputmanager stuff. Anyway here’s the code to the dialog.
Here is the code to my layout. I made this dialog look exactly like an Alert Dialog with the Ok and Cancel buttons along with the gray background.