use of android.text.InputFilter in project butter-android by butterproject.
the class NumberDialogFragment method onCreateDialog.
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
Bundle arguments = getArguments();
if (arguments == null || !arguments.containsKey(ARG_MAX_VALUE) || !arguments.containsKey(ARG_MIN_VALUE) || !arguments.containsKey(ARG_TITLE) || onResultListener == null) {
return builder.create();
}
final int defaultValue = arguments.getInt(ARG_DEFAULT_VALUE, arguments.getInt(ARG_MAX_VALUE) / 2);
final LinearLayout layout = new LinearLayout(getActivity());
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(params);
final int max = arguments.getInt(ARG_MAX_VALUE);
final int min = arguments.getInt(ARG_MIN_VALUE);
final EditText editText = new EditText(getActivity());
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
editText.setHint(String.valueOf(defaultValue));
editText.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
editText.setFilters(new InputFilter[] { new InputFilter() {
@Override
public CharSequence filter(CharSequence charSequence, int in, int i1, Spanned spanned, int i2, int i3) {
try {
int input = Integer.parseInt(spanned.toString() + charSequence.toString());
if (input > max) {
return "";
} else {
return null;
}
} catch (NumberFormatException nfe) {
return "";
}
}
} });
layout.addView(editText);
builder.setView(layout).setTitle(arguments.getString(ARG_TITLE)).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int output;
if (editText.getText().toString().isEmpty()) {
output = defaultValue;
} else {
output = Integer.parseInt(editText.getText().toString());
}
onResultListener.onNewValue(output < min ? min : output);
dialog.dismiss();
}
}).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return builder.create();
}
use of android.text.InputFilter in project android_packages_apps_DU-Tweaks by DirtyUnicorns.
the class BatteryLightDialog method setAlphaSliderVisible.
public void setAlphaSliderVisible(boolean visible) {
mHexColorInput.setFilters(new InputFilter[] { new InputFilter.LengthFilter(visible ? 8 : 6) });
mColorPicker.setAlphaSliderVisible(visible);
}
use of android.text.InputFilter in project android_packages_apps_DU-Tweaks by DirtyUnicorns.
the class NotificationLightDialog method setAlphaSliderVisible.
public void setAlphaSliderVisible(boolean visible) {
mHexColorInput.setFilters(new InputFilter[] { new InputFilter.LengthFilter(visible ? 8 : 6) });
mColorPicker.setAlphaSliderVisible(visible);
}
use of android.text.InputFilter in project android_packages_apps_Settings by DirtyUnicorns.
the class Utf8ByteLengthFilterTest method testFilter.
@SmallTest
public void testFilter() {
// Define the variables
CharSequence source;
SpannableStringBuilder dest;
// Constructor to create a LengthFilter
InputFilter lengthFilter = new Utf8ByteLengthFilter(10);
InputFilter[] filters = { lengthFilter };
// filter() implicitly invoked. If the total length > filter length, the filter will
// cut off the source CharSequence from beginning to fit the filter length.
source = "abc";
dest = new SpannableStringBuilder("abcdefgh");
dest.setFilters(filters);
dest.insert(1, source);
String expectedString1 = "aabbcdefgh";
assertEquals(expectedString1, dest.toString());
dest.replace(5, 8, source);
String expectedString2 = "aabbcabcgh";
assertEquals(expectedString2, dest.toString());
dest.insert(2, source);
assertEquals(expectedString2, dest.toString());
dest.delete(1, 3);
String expectedString3 = "abcabcgh";
assertEquals(expectedString3, dest.toString());
dest.append("12345");
String expectedString4 = "abcabcgh12";
assertEquals(expectedString4, dest.toString());
// 2 Chinese chars == 6 bytes in UTF-8
source = "\u60a8\u597d";
dest.replace(8, 10, source);
assertEquals(expectedString3, dest.toString());
dest.replace(0, 1, source);
String expectedString5 = "\u60a8bcabcgh";
assertEquals(expectedString5, dest.toString());
dest.replace(0, 4, source);
String expectedString6 = "\u60a8\u597dbcgh";
assertEquals(expectedString6, dest.toString());
// 2 Latin-1 chars == 4 bytes in UTF-8
source = "\u00a3\u00a5";
dest.delete(2, 6);
dest.insert(0, source);
String expectedString7 = "\u00a3\u00a5\u60a8\u597d";
assertEquals(expectedString7, dest.toString());
dest.replace(2, 3, source);
String expectedString8 = "\u00a3\u00a5\u00a3\u597d";
assertEquals(expectedString8, dest.toString());
dest.replace(3, 4, source);
String expectedString9 = "\u00a3\u00a5\u00a3\u00a3\u00a5";
assertEquals(expectedString9, dest.toString());
// filter() explicitly invoked
dest = new SpannableStringBuilder("abcdefgh");
CharSequence beforeFilterSource = "TestLengthFilter";
String expectedAfterFilter = "TestLength";
CharSequence actualAfterFilter = lengthFilter.filter(beforeFilterSource, 0, beforeFilterSource.length(), dest, 0, dest.length());
assertEquals(expectedAfterFilter, actualAfterFilter);
}
use of android.text.InputFilter in project connect-sdk-client-android by Ingenico-ePayments.
the class RenderTextField method renderField.
@Override
public View renderField(PaymentProductField field, InputDataPersister inputDataPersister, ViewGroup rowView, PaymentContext paymentContext) {
if (field == null) {
throw new InvalidParameterException("Error rendering textfield, field may not be null");
}
if (rowView == null) {
throw new InvalidParameterException("Error rendering textfield, rowView may not be null");
}
if (inputDataPersister == null) {
throw new InvalidParameterException("Error rendering textfield, inputDataPersister may not be null");
}
PaymentItem paymentItem = inputDataPersister.getPaymentItem();
AccountOnFile accountOnFile = inputDataPersister.getAccountOnFile();
// Create new EditText and set its style, restrictions, mask and keyboardtype
EditText editText = new EditText(rowView.getContext());
editText.setTextAppearance(rowView.getContext(), R.style.TextField);
if (field.getDataRestrictions().getValidator().getLength() != null) {
// Set maxLength for field
Integer maxLength = field.getDataRestrictions().getValidator().getLength().getMaxLength();
if (maxLength > 0) {
editText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxLength) });
editText.setEms(maxLength);
}
}
Translator translator = Translator.getInstance(rowView.getContext());
String label = translator.getPaymentProductFieldPlaceholderText(paymentItem.getId(), field.getId());
editText.setHint(label);
// Set correct inputType type
switch(field.getDisplayHints().getPreferredInputType()) {
case INTEGER_KEYBOARD:
editText.setInputType(android.text.InputType.TYPE_CLASS_NUMBER);
break;
case STRING_KEYBOARD:
editText.setInputType(android.text.InputType.TYPE_CLASS_TEXT);
break;
case PHONE_NUMBER_KEYBOARD:
editText.setInputType(android.text.InputType.TYPE_CLASS_PHONE);
break;
case EMAIL_ADDRESS_KEYBOARD:
editText.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
break;
case DATE_PICKER:
editText.setInputType(InputType.TYPE_DATETIME_VARIATION_DATE);
default:
editText.setInputType(android.text.InputType.TYPE_CLASS_TEXT);
break;
}
// Check if this edittext should be obfuscated
if (field.getDisplayHints().isObfuscate()) {
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
// Add mask functionality when a mask is set
Boolean addMasking = field.getDisplayHints().getMask() != null;
Integer maskLength = 0;
if (field.getDisplayHints().getMask() != null) {
maskLength = field.getDisplayHints().getMask().replace("{", "").replace("}", "").length();
editText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(maskLength) });
} else if (field.getDataRestrictions().getValidator().getLength() != null) {
maskLength = field.getDataRestrictions().getValidator().getLength().getMaxLength();
}
// Set values from account on file
if (accountOnFile != null) {
for (KeyValuePair attribute : accountOnFile.getAttributes()) {
if (attribute.getKey().equals(field.getId())) {
if (field.getDisplayHints().getMask() != null) {
StringFormatter stringFormatter = new StringFormatter();
String maskedValue = stringFormatter.applyMask(field.getDisplayHints().getMask().replace("9", "*"), attribute.getValue());
editText.setText(maskedValue);
} else {
editText.setText(attribute.getValue());
}
if (!attribute.isEditingAllowed()) {
editText.setEnabled(false);
}
}
}
}
// Add OnTextChanged watcher for this inputfield
editText.addTextChangedListener(new FieldInputTextWatcher(inputDataPersister, field.getId(), editText, addMasking));
// get input information from inputDataPersister
String paymentProductValue = inputDataPersister.getValue(field.getId());
if (paymentProductValue != null && accountOnFile == null) {
editText.setText(paymentProductValue);
}
// Add it to parentView
rowView.addView(editText);
return editText;
}
Aggregations