I have a Dialog Fragment that allows a user to specify some text, and a position for the text.
I want to restrict the user from entering text greater than some upper limit of characters. Can I restrict the max character input? Else, where in this code can I include a check to make sure the text is less than LIMIT? Here is the code:
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final CharSequence[] items = {"Above", "Below"};
capPos = Position.ABOVE;
final EditText input = new EditText(getActivity());
input.setInputType(EditorInfo.TYPE_CLASS_TEXT);
input.setHint("Add caption here");
builder.setView(input);
builder.setTitle(R.string.dialog_caption)
.setPositiveButton(R.string.proceed, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
text = input.getText().toString();
mListener.onDialogPositiveClick(CaptionFragment.this);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogNegativeClick(CaptionFragment.this);
}
})
.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
if ("Above".compareTo(items[id].toString()) == 0){
capPos = Caption.Position.ABOVE ;
Log.i("CaptionFragment", "Above");
}else if ("Below".compareTo(items[id].toString()) == 0){
capPos = Caption.Position.BELOW;
Log.i("CaptionFragment", "Below");
}
}
});
// Create the AlertDialog object and return it
return builder.create();
}