164 lines
5.7 KiB
Java
164 lines
5.7 KiB
Java
package ch.pizzalink.android.view;
|
|
|
|
import android.content.Context;
|
|
import android.content.res.TypedArray;
|
|
import android.graphics.Typeface;
|
|
import android.text.Editable;
|
|
import android.text.InputType;
|
|
import android.text.TextWatcher;
|
|
import android.util.AttributeSet;
|
|
import android.view.View;
|
|
import android.view.inputmethod.InputMethodManager;
|
|
import android.widget.EditText;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.TextView;
|
|
|
|
import ch.pizzalink.android.R;
|
|
import ch.pizzalink.android.activity.BaseActivity;
|
|
import ch.pizzalink.android.helper.PasswordHelper;
|
|
|
|
/**
|
|
* Created by cimenmus on 28/09/2017.
|
|
*/
|
|
|
|
public class PizzalinkEditText extends LinearLayout implements View.OnClickListener {
|
|
|
|
private View rootView;
|
|
private TextView hintTextView;
|
|
private EditText editText;
|
|
private boolean isPasswordHidden = true;
|
|
private Typeface typeFace;
|
|
private String hint;
|
|
private String inputType;
|
|
|
|
/*
|
|
public PizzalinkEditText(LinearLayout rootView, boolean isLeftIconVisible, int leftIconId,
|
|
int editTextHintId, int inputType, boolean isPasswordIconVisible){
|
|
|
|
initViews(rootView);
|
|
setLeftIcon(isLeftIconVisible, leftIconId);
|
|
setPasswordIcon(isPasswordIconVisible);
|
|
setEditTextt(editTextHintId, inputType);
|
|
}
|
|
*/
|
|
|
|
public PizzalinkEditText(Context context) {
|
|
super(context);
|
|
init(context);
|
|
}
|
|
|
|
public PizzalinkEditText(Context context, AttributeSet attrs) {
|
|
super(context, attrs);
|
|
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PizzalinkEditText, 0, 0);
|
|
try {
|
|
hint = a.getString(R.styleable.PizzalinkEditText_hint);
|
|
inputType = a.getString(R.styleable.PizzalinkEditText_inputType);
|
|
} finally {
|
|
a.recycle();
|
|
}
|
|
init(context);
|
|
}
|
|
|
|
private void init(Context context) {
|
|
rootView = inflate(context, R.layout.layout_pizzalink_edittext, this);
|
|
hintTextView = (TextView) rootView.findViewById(R.id.hintTextView);
|
|
editText = (EditText) rootView.findViewById(R.id.editText);
|
|
hintTextView.setText(hint);
|
|
setInputType();
|
|
rootView.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
focusEditText();
|
|
}
|
|
});
|
|
}
|
|
|
|
private void setInputType(){
|
|
if (inputType == null)
|
|
inputType = "text";
|
|
switch (inputType){
|
|
case "name":
|
|
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS |
|
|
InputType.TYPE_TEXT_FLAG_CAP_WORDS);
|
|
break;
|
|
case "password":
|
|
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
|
|
break;
|
|
case "phone":
|
|
editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_CLASS_PHONE);
|
|
break;
|
|
case "email":
|
|
editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
|
|
break;
|
|
case "number":
|
|
editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_CLASS_NUMBER);
|
|
break;
|
|
case "address":
|
|
editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_CLASS_TEXT |
|
|
InputType.TYPE_TEXT_FLAG_CAP_WORDS | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
|
|
|
|
editText.addTextChangedListener(new TextWatcher() {
|
|
@Override
|
|
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
|
|
|
|
@Override
|
|
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
|
|
|
|
@Override
|
|
public void afterTextChanged(Editable editable) {
|
|
// if edittext has 10chars & this is not called yet, add new line
|
|
if(editText.getText().length() == 40 * editText.getLineCount()) {
|
|
editText.append("\n");
|
|
}
|
|
}
|
|
});
|
|
break;
|
|
default:
|
|
editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_CLASS_TEXT |
|
|
InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
public void onClick(View v) {
|
|
hideShowPassword();
|
|
}
|
|
|
|
private void hideShowPassword(){
|
|
if(isPasswordHidden)
|
|
editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
|
|
else
|
|
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
|
|
isPasswordHidden = !isPasswordHidden;
|
|
editText.setTypeface(typeFace);
|
|
}
|
|
|
|
private void focusEditText(){
|
|
editText.requestFocus();
|
|
InputMethodManager inputMethodManager=(InputMethodManager)BaseActivity.currentActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
|
|
}
|
|
|
|
public boolean isEmpty(){
|
|
return editText.getText().toString().isEmpty();
|
|
}
|
|
|
|
public boolean isEmail(){
|
|
return PasswordHelper.EMAIL_ADDRESS_PATTERN.matcher(editText.getText()).matches();
|
|
}
|
|
|
|
public String getText(){
|
|
return editText.getText().toString();
|
|
}
|
|
|
|
|
|
public TextView getHintTextView() {
|
|
return hintTextView;
|
|
}
|
|
|
|
public EditText getEditText() {
|
|
return editText;
|
|
}
|
|
} |