|
| 1 | +package com.wrapp.android.views; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.res.TypedArray; |
| 5 | +import android.text.Editable; |
| 6 | +import android.text.TextWatcher; |
| 7 | +import android.util.AttributeSet; |
| 8 | +import android.view.LayoutInflater; |
| 9 | +import android.view.View; |
| 10 | +import android.widget.EditText; |
| 11 | +import android.widget.LinearLayout; |
| 12 | +import android.widget.TextView; |
| 13 | + |
| 14 | +import com.nineoldandroids.animation.Animator; |
| 15 | +import com.nineoldandroids.animation.AnimatorListenerAdapter; |
| 16 | +import com.nineoldandroids.animation.AnimatorSet; |
| 17 | +import com.nineoldandroids.animation.ObjectAnimator; |
| 18 | +import com.wrapp.android.R; |
| 19 | + |
| 20 | +public class FloatLabeledEditText extends LinearLayout { |
| 21 | + |
| 22 | + private String hint; |
| 23 | + |
| 24 | + private TextView hintTextView; |
| 25 | + private EditText editText; |
| 26 | + |
| 27 | + public FloatLabeledEditText(Context context) { |
| 28 | + this(context, null); |
| 29 | + initialize(); |
| 30 | + } |
| 31 | + |
| 32 | + public FloatLabeledEditText(Context context, AttributeSet attrs) { |
| 33 | + this(context, attrs, 0); |
| 34 | + } |
| 35 | + |
| 36 | + public FloatLabeledEditText(Context context, AttributeSet attrs, int defStyle) { |
| 37 | + super(context, attrs, defStyle); |
| 38 | + |
| 39 | + TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.FloatLabeledEditText); |
| 40 | + try { |
| 41 | + hint = a.getString(R.styleable.FloatLabeledEditText_floatingHint); |
| 42 | + } finally { |
| 43 | + a.recycle(); |
| 44 | + } |
| 45 | + |
| 46 | + initialize(); |
| 47 | + } |
| 48 | + |
| 49 | + private void initialize() { |
| 50 | + setOrientation(VERTICAL); |
| 51 | + |
| 52 | + View view = LayoutInflater.from(getContext()).inflate(R.layout.float_labeled_edit_text, this); |
| 53 | + |
| 54 | + hintTextView = (TextView) view.findViewById(R.id.FloatLabeledEditTextHint); |
| 55 | + editText = (EditText) view.findViewById(R.id.FloatLabeledEditTextEditText); |
| 56 | + |
| 57 | + if (hint != null) { |
| 58 | + setHint(hint); |
| 59 | + } |
| 60 | + |
| 61 | + hintTextView.setVisibility(View.INVISIBLE); |
| 62 | + editText.addTextChangedListener(onTextChanged); |
| 63 | + } |
| 64 | + |
| 65 | + private TextWatcher onTextChanged = new TextWatcher() { |
| 66 | + @Override |
| 67 | + public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void afterTextChanged(Editable editable) { |
| 76 | + setShowHint(editable.length() != 0); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + private void setShowHint(final boolean show) { |
| 81 | + AnimatorSet animation = null; |
| 82 | + if ((hintTextView.getVisibility() == VISIBLE) && !show) { |
| 83 | + animation = new AnimatorSet(); |
| 84 | + ObjectAnimator move = ObjectAnimator.ofFloat(hintTextView, "translationY", 0, hintTextView.getHeight() / 8); |
| 85 | + ObjectAnimator fade = ObjectAnimator.ofFloat(hintTextView, "alpha", 1, 0); |
| 86 | + animation.playTogether(move, fade); |
| 87 | + } else if ((hintTextView.getVisibility() != VISIBLE) && show) { |
| 88 | + animation = new AnimatorSet(); |
| 89 | + ObjectAnimator move = ObjectAnimator.ofFloat(hintTextView, "translationY", hintTextView.getHeight() / 8, 0); |
| 90 | + ObjectAnimator fade = ObjectAnimator.ofFloat(hintTextView, "alpha", 0, 1); |
| 91 | + animation.playTogether(move, fade); |
| 92 | + } |
| 93 | + |
| 94 | + if (animation != null) { |
| 95 | + animation.addListener(new AnimatorListenerAdapter() { |
| 96 | + @Override |
| 97 | + public void onAnimationStart(Animator animation) { |
| 98 | + super.onAnimationStart(animation); |
| 99 | + hintTextView.setVisibility(VISIBLE); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void onAnimationEnd(Animator animation) { |
| 104 | + super.onAnimationEnd(animation); |
| 105 | + hintTextView.setVisibility(show ? VISIBLE : INVISIBLE); |
| 106 | + } |
| 107 | + }); |
| 108 | + animation.start(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private void setHint(String hint) { |
| 113 | + this.hint = hint; |
| 114 | + editText.setHint(hint); |
| 115 | + hintTextView.setText(hint); |
| 116 | + } |
| 117 | + |
| 118 | + public Editable getText() { |
| 119 | + return editText.getText(); |
| 120 | + } |
| 121 | + |
| 122 | + public void setText(CharSequence text) { |
| 123 | + editText.setText(text); |
| 124 | + } |
| 125 | +} |
0 commit comments