Last active
January 29, 2021 01:11
-
-
Save adam-hurwitz/28e303b4c37bfa4630d6b8253cb6a0a6 to your computer and use it in GitHub Desktop.
ODG - EditText Utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Handle input length | |
private void handleInputLength(){ | |
editTextViewName.addTextChangedListener(new TextWatcher() { | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) {} | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
if (s.length() > 6){animateMethod();} | |
} | |
@Override | |
public void afterTextChanged(Editable editable) { | |
String numpadText = editable.toString(); | |
if (editable.length() == 7){numpadText = numpadText.substring(0, editable.length()-1);} | |
} | |
}); | |
// Keyboard visibility | |
editTextViewName.postDelayed(() -> showSoftKeyboard(editTextViewName), 100); | |
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); | |
public static void showKeyboard(Context context, View view) { | |
if (view.requestFocus()) { | |
InputMethodManager imm = (InputMethodManager) | |
context.getSystemService(Context.INPUT_METHOD_SERVICE); | |
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); | |
} | |
} | |
public static void hideKeyboard(Context context, View view) { | |
if (view != null) { | |
InputMethodManager inputMethodManager = (InputMethodManager) | |
context.getSystemService(Activity.INPUT_METHOD_SERVICE); | |
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0); | |
} | |
} | |
// Keyboard visibility - DialogFragment | |
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); | |
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); | |
// Comma formatting | |
@NonNull | |
private TextWatcher editTextWatcher() { | |
return new TextWatcher() { | |
... | |
@Override | |
public void afterTextChanged(Editable editable) { | |
view.removeTextChangedListener(this); | |
try { | |
String originalString = editable.toString(); | |
Long longval; | |
if (originalString.contains(",")) { | |
originalString = originalString.replaceAll(",", ""); | |
} | |
longval = Long.parseLong(originalString); | |
DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US); | |
formatter.applyPattern("#,###,###,###"); | |
String formattedString = formatter.format(longval); | |
//setting text after format to EditText | |
view.setText(formattedString); | |
view.setSelection(view.getText().length()); | |
} catch (NumberFormatException nfe) { | |
nfe.printStackTrace(); | |
} | |
view.addTextChangedListener(this); | |
} | |
}); | |
// Format String to Int | |
offerPriceText = offerPriceText.replace(",", ""); | |
Integer.parseInt(offerPriceText) | |
// Validation | |
TextView tv = new TextView(this); | |
int maxLength = 10; | |
InputFilter[] fArray = new InputFilter[1]; | |
fArray[0] = new InputFilter.LengthFilter(maxLength); | |
tv.setFilters(fArray); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment