Skip to content

Instantly share code, notes, and snippets.

@briangriffey
Created April 23, 2014 14:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briangriffey/11217376 to your computer and use it in GitHub Desktop.
Save briangriffey/11217376 to your computer and use it in GitHub Desktop.
package com.homeaway.floatlabel.library;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
/**
* Created by briangriffey on 12/18/13.
*/
public class FloatLabelEditText extends ViewGroup implements TextWatcher {
private EditText mEditText;
private TextView mLabel;
public FloatLabelEditText(Context context) {
super(context);
init(context, null);
}
public FloatLabelEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.FloatLabelEditText);
int labelStyle = array.getResourceId(R.styleable.FloatLabelEditText_labelStyle, 0);
mEditText = new EditText(context, attrs);
mEditText.addTextChangedListener(this);
addView(mEditText);
mLabel = new TextView(context);
mLabel.setText(mEditText.getHint());
mLabel.setVisibility(View.INVISIBLE);
if (labelStyle != 0) {
mLabel.setTextAppearance(context, labelStyle);
}
addView(mLabel);
array.recycle();
}
public FloatLabelEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int totalHeight = MeasureSpec.getSize(heightMeasureSpec);
int halfHeight = totalHeight / 2;
int mode = MeasureSpec.getMode(heightMeasureSpec);
//yolo, be as big as you want homie
if (mode == MeasureSpec.UNSPECIFIED) {
mLabel.measure(widthMeasureSpec, heightMeasureSpec);
mEditText.measure(widthMeasureSpec, heightMeasureSpec);
} else {
int labelHeightSpec = MeasureSpec.makeMeasureSpec(halfHeight, MeasureSpec.AT_MOST);
mLabel.measure(widthMeasureSpec, labelHeightSpec);
int remainingHeight = totalHeight - mLabel.getMeasuredHeight();
int editHeightSpec = MeasureSpec.makeMeasureSpec(remainingHeight, MeasureSpec.AT_MOST);
mEditText.measure(widthMeasureSpec, editHeightSpec);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
mLabel.layout(0, 0, mLabel.getMeasuredWidth(), mLabel.getMeasuredHeight());
mEditText.layout(0, mLabel.getMeasuredHeight(), mEditText.getMeasuredWidth(), mLabel.getMeasuredHeight() + mEditText.getMeasuredHeight());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.toString().length() > 0) {
if (mLabel.getVisibility() != View.VISIBLE) {
mLabel.setVisibility(View.VISIBLE);
mLabel.setAlpha(0);
mLabel.setTranslationY(mLabel.getMeasuredHeight());
mLabel.animate().alpha(1).translationY(0).setDuration(100).start();
}
} else {
mLabel.setVisibility(View.INVISIBLE);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment