Skip to content

Instantly share code, notes, and snippets.

@Kolyall
Last active November 13, 2019 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kolyall/bcfdabdf5d7f10c7573e1b45dfa91c4d to your computer and use it in GitHub Desktop.
Save Kolyall/bcfdabdf5d7f10c7573e1b45dfa91c4d to your computer and use it in GitHub Desktop.
EditText patterns

EditText:

  1. Change error color at TextInputLayout
  2. Programly clear focus on touch at any view
  3. Programly MaxLength of EditText
<style name="TextLimitStyle" parent="TextAppearance.AppCompat.Small">
<item name="android:textColor">@color/colorAccent< /item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">18sp</item>
</style>
<style name="TextLimitError" parent="TextAppearance.AppCompat.Small">
<item name="android:textColor">@color/colorRed</item>
</style>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterEnabled="true"
app:counterMaxLength="30"
app:counterOverflowTextAppearance="@style/TextLimitError"
app:counterTextAppearance="@style/TextLimitStyle">
<android.support.design.widget.TextInputEditText
android:id="@+id/etMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="Say something..."
android:maxLength="30" />
</android.support.design.widget.TextInputLayout>
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setupHideKeyboardOnFocusChange();
}
public void setupHideKeyboardOnFocusChange() {
setupHideKeyboardOnFocusChange(getActivity().findViewById(android.R.id.content));
}
public void setupHideKeyboardOnFocusChange(View view) {
// Set up touch listener for non-text box views to hide keyboard.
if (!(view instanceof EditText) && !(view instanceof CheckableImageButton)) {
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideKeyboard();
return false;
}
});
}
//If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupHideKeyboardOnFocusChange(innerView);
}
}
}
//region add LengthFilter
InputFilter[] filters = inputEditText.getFilters();
InputFilter[] inputFilters = new InputFilter[filters.length + 1];
InputFilter.LengthFilter lengthFilter = new InputFilter.LengthFilter(30);
for (int i = 0; i < filters.length; i++) {
inputFilters[i] = filters[i];
}
inputFilters[inputFilters.length - 1] = lengthFilter;
inputEditText.setFilters(inputFilters);
//endregion
OR
android:maxLength="30"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment