Created
April 25, 2016 16:22
-
-
Save lbalmaceda/b0d848d3eb336f63378680923cb6b75b to your computer and use it in GitHub Desktop.
Custom SignUp Fields
This file contains 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
public class CustomField implements Parcelable { | |
private static final int CAP_SENTENCES = 0; | |
private static final int CAP_WORDS = 1; | |
private static final int CAP_CHARACTERS = 2; | |
private static final int MAX_ALLOWED_LENGTH = 100; | |
private int cap; | |
private int maxLength; | |
private boolean onlyNumbers; | |
private boolean hideInput; | |
private String hint; | |
private CustomField(@NonNull String hint){ | |
if (hint.isEmpty()){ | |
throw new IllegalArgumentException("The hint cannot be empty!"); | |
} | |
this.hint = hint; | |
this.maxLength = MAX_ALLOWED_LENGTH; | |
this.cap = CAP_SENTENCES; | |
this.onlyNumbers = false; | |
} | |
public void configureField(@NonNull EditText field){ | |
field.setInputType(getInputType()); | |
field.setHint(hint); | |
field.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)}); | |
if (hideInput){ | |
field.setTransformationMethod(PasswordTransformationMethod.getInstance()); | |
} | |
} | |
public static Builder newBuilder(@NonNull String hint){ | |
return new Builder(hint); | |
} | |
private int getInputType(){ | |
int flags = InputType.TYPE_CLASS_TEXT; | |
if (onlyNumbers){ | |
flags = InputType.TYPE_CLASS_NUMBER; | |
} | |
if (hideInput){ | |
return flags | InputType.TYPE_TEXT_VARIATION_PASSWORD; | |
} | |
if (cap == 0){ | |
return flags | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES; | |
} else if (cap == 1){ | |
return flags | InputType.TYPE_TEXT_FLAG_CAP_WORDS; | |
} else { | |
return flags | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS; | |
} | |
} | |
private void setCap(int cap){ | |
this.cap = cap; | |
} | |
private void setMaxLength(int max){ | |
if (max <= 0 || max > MAX_ALLOWED_LENGTH){ | |
max = MAX_ALLOWED_LENGTH; | |
} | |
this.maxLength = max; | |
} | |
private void onlyNumbers(){ | |
this.onlyNumbers = true; | |
} | |
private void hideInput(){ | |
this.hideInput = true; | |
} | |
private void setHint(@NonNull String hint){ | |
this.hint = hint; | |
} | |
protected CustomField(Parcel in) { | |
cap = in.readInt(); | |
maxLength = in.readInt(); | |
onlyNumbers = in.readByte() != 0x00; | |
hideInput = in.readByte() != 0x00; | |
hint = in.readString(); | |
} | |
@Override | |
public int describeContents() { | |
return 0; | |
} | |
@Override | |
public void writeToParcel(Parcel dest, int flags) { | |
dest.writeInt(cap); | |
dest.writeInt(maxLength); | |
dest.writeByte((byte) (onlyNumbers ? 0x01 : 0x00)); | |
dest.writeByte((byte) (hideInput ? 0x01 : 0x00)); | |
dest.writeString(hint); | |
} | |
@SuppressWarnings("unused") | |
public static final Parcelable.Creator<CustomField> CREATOR = new Parcelable.Creator<CustomField>() { | |
@Override | |
public CustomField createFromParcel(Parcel in) { | |
return new CustomField(in); | |
} | |
@Override | |
public CustomField[] newArray(int size) { | |
return new CustomField[size]; | |
} | |
}; | |
public static class Builder { | |
private CustomField field; | |
public Builder(@NonNull String hint){ | |
this.field = new CustomField(hint); | |
} | |
public Builder maxLength(int max){ | |
field.setMaxLength(max); | |
return this; | |
} | |
public Builder onlyNumbers(){ | |
field.onlyNumbers(); | |
return this; | |
} | |
public Builder hideInput(){ | |
field.hideInput(); | |
return this; | |
} | |
public Builder capWords(){ | |
field.setCap(CAP_WORDS); | |
return this; | |
} | |
public Builder capSentences(){ | |
field.setCap(CAP_SENTENCES); | |
return this; | |
} | |
public Builder capCharacters(){ | |
field.setCap(CAP_CHARACTERS); | |
return this; | |
} | |
public CustomField build(){ | |
return field; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment