Skip to content

Instantly share code, notes, and snippets.

@alexfacciorusso
Last active August 29, 2015 14:21
Show Gist options
  • Save alexfacciorusso/c33460b34b30f9c86b4a to your computer and use it in GitHub Desktop.
Save alexfacciorusso/c33460b34b30f9c86b4a to your computer and use it in GitHub Desktop.
<!--
Copyright (C) 2014 Chris Banes
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="FloatLabelLayout">
<attr name="floatLabelTextAppearance" format="reference" />
<attr name="floatLabelPaddingLeft" format="reference|dimension" />
<attr name="floatLabelPaddingRight" format="reference|dimension" />
<attr name="floatLabelPaddingTop" format="reference|dimension" />
<attr name="floatLabelPaddingBottom" format="reference|dimension" />
<!-- The hint to display in the floating label -->
<attr name="floatLabelHint" format="reference|string" />
</declare-styleable>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- The color when activated/focused (usually your app's accent color) -->
<item android:color="..." android:state_activated="true" />
<!-- The color when not activated/focused (usually android:textColorSecondary) -->
<item android:color="..." />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<your.package.FloatLabelLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:floatLabelTextAppearance="@style/TextAppearance.YourApp.FloatLabel">
<EditText
android:id="@+id/edit_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/account_username_hint"
android:singleLine="true"
android:inputType="textNoSuggestions"
android:imeOptions="actionNext"
android:nextFocusDown="@+id/edit_password" />
</your.package.FloatLabelLayout>
<your.package.FloatLabelLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:floatLabelTextAppearance="@style/TextAppearance.YourApp.FloatLabel">
<EditText
android:id="@+id/edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/account_password_hint"
android:singleLine="true"
android:inputType="textNoSuggestions"
android:imeOptions="actionDone" />
</your.package.FloatLabelLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="TextAppearance.YourApp.FloatLabel" parent="android:TextAppearance.Small">
<item name="android:textColor">@color/float_label</item>
<item name="android:textSize">12sp</item>
</style>
</resources>
/*
* Copyright 2015 Chris Banes, Alex Facciorusso
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorListenerAdapter;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Interpolator;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* Layout which an {@link android.widget.EditText} to show a floating label when the hint is hidden
* due to the user inputting text.
*
* @see <a href="https://dribbble.com/shots/1254439--GIF-Mobile-Form-Interaction">Matt D. Smith on
* Dribble</a>
* @see <a href="http://bradfrostweb.com/blog/post/float-label-pattern/">Brad Frost's blog post</a>
*/
public class FloatLabelLayout extends LinearLayout implements ViewGroup.OnHierarchyChangeListener {
private static final long ANIMATION_DURATION = 150;
private static final float DEFAULT_LABEL_PADDING_LEFT = 3f;
private static final float DEFAULT_LABEL_PADDING_TOP = 4f;
private static final float DEFAULT_LABEL_PADDING_RIGHT = 3f;
private static final float DEFAULT_LABEL_PADDING_BOTTOM = 4f;
private static final int ERROR_LABEL_TEXT_SIZE = 12;
private static final int ERROR_LABEL_PADDING = 4;
private EditText mEditText;
private TextView mLabel;
private CharSequence mHint;
private Interpolator mInterpolator;
private TextView mErrorLabel;
private Drawable mDrawable;
private int mErrorColor;
public FloatLabelLayout(Context context) {
this(context, null);
}
public FloatLabelLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FloatLabelLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setOrientation(VERTICAL);
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FloatLabelLayout);
int leftPadding = a.getDimensionPixelSize(
R.styleable.FloatLabelLayout_floatLabelPaddingLeft,
dipsToPix(DEFAULT_LABEL_PADDING_LEFT));
int topPadding = a.getDimensionPixelSize(
R.styleable.FloatLabelLayout_floatLabelPaddingTop,
dipsToPix(DEFAULT_LABEL_PADDING_TOP));
int rightPadding = a.getDimensionPixelSize(
R.styleable.FloatLabelLayout_floatLabelPaddingRight,
dipsToPix(DEFAULT_LABEL_PADDING_RIGHT));
int bottomPadding = a.getDimensionPixelSize(
R.styleable.FloatLabelLayout_floatLabelPaddingBottom,
dipsToPix(DEFAULT_LABEL_PADDING_BOTTOM));
mHint = a.getText(R.styleable.FloatLabelLayout_floatLabelHint);
mLabel = new TextView(context);
mLabel.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);
mLabel.setVisibility(INVISIBLE);
mLabel.setText(mHint);
ViewCompat.setPivotX(mLabel, 0f);
ViewCompat.setPivotY(mLabel, 0f);
mLabel.setTextAppearance(context,
a.getResourceId(R.styleable.FloatLabelLayout_floatLabelTextAppearance,
android.R.style.TextAppearance_Small));
a.recycle();
mInterpolator = new FastOutSlowInInterpolator();
// Error label configuration
setOnHierarchyChangeListener(this);
mErrorColor = Color.parseColor("#D32F2F");
initErrorLabel();
}
private void initErrorLabel() {
mErrorLabel = new TextView(getContext());
mErrorLabel.setFocusable(true);
mErrorLabel.setFocusableInTouchMode(true);
mErrorLabel.setTextSize(ERROR_LABEL_TEXT_SIZE);
mErrorLabel.setTextColor(mErrorColor);
mErrorLabel.setPadding(dipsToPix(ERROR_LABEL_PADDING), 0, dipsToPix(ERROR_LABEL_PADDING), 0);
mErrorLabel.setVisibility(GONE);
}
public void setErrorColor(int color) {
mErrorColor = color;
mErrorLabel.setTextColor(mErrorColor);
}
public void clearError() {
mErrorLabel.setVisibility(GONE);
mDrawable.clearColorFilter();
}
public void setError(@StringRes int textResId) {
if (textResId < 0) {
mErrorLabel.setVisibility(GONE);
return;
}
mErrorLabel.setVisibility(VISIBLE);
mErrorLabel.setText(textResId);
tintErrorDrawable();
}
private void tintErrorDrawable() {
mDrawable.setColorFilter(mErrorColor, PorterDuff.Mode.SRC_ATOP);
mErrorLabel.requestFocus();
}
public void setError(String text) {
if (TextUtils.isEmpty(text)) {
mErrorLabel.setVisibility(GONE);
return;
}
mErrorLabel.setVisibility(VISIBLE);
mErrorLabel.setText(text);
tintErrorDrawable();
}
@Override
public final void addView(@NonNull View child, int index, ViewGroup.LayoutParams params) {
if (child instanceof EditText) {
setEditText((EditText) child);
}
// Carry on adding the View...
super.addView(child, index, params);
}
private void setEditText(EditText editText) {
// If we already have an EditText, throw an exception
if (mEditText != null) {
throw new IllegalArgumentException("We already have an EditText, can only have one");
}
mEditText = editText;
// Update the label visibility with no animation
updateLabelVisibility(false);
// dismiss error on touch
mEditText.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
clearError();
break;
}
return false;
}
});
// Add a TextWatcher so that we know when the text input has changed
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
updateLabelVisibility(true);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
// Add focus listener to the EditText so that we can notify the label that it is activated.
// Allows the use of a ColorStateList for the text color on the label
mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean focused) {
updateLabelVisibility(true);
}
});
// If we do not have a valid hint, try and retrieve it from the EditText
if (TextUtils.isEmpty(mHint)) {
setHint(mEditText.getHint());
}
}
private void updateLabelVisibility(boolean animate) {
boolean hasText = !TextUtils.isEmpty(mEditText.getText());
boolean isFocused = mEditText.isFocused();
mLabel.setActivated(isFocused);
if (hasText || isFocused) {
// We should be showing the label so do so if it isn't already
if (mLabel.getVisibility() != VISIBLE) {
showLabel(animate);
}
} else {
// We should not be showing the label so hide it
if (mLabel.getVisibility() == VISIBLE) {
hideLabel(animate);
}
}
}
/**
* @return the {@link android.widget.EditText} text input
*/
public EditText getEditText() {
return mEditText;
}
/**
* @return the {@link android.widget.TextView} label
*/
public TextView getLabel() {
return mLabel;
}
/**
* Set the hint to be displayed in the floating label
*/
public void setHint(CharSequence hint) {
mHint = hint;
mLabel.setText(hint);
}
/**
* Show the label
*/
private void showLabel(boolean animate) {
if (animate) {
mLabel.setVisibility(View.VISIBLE);
ViewCompat.setTranslationY(mLabel, mLabel.getHeight());
float scale = mEditText.getTextSize() / mLabel.getTextSize();
ViewCompat.setScaleX(mLabel, scale);
ViewCompat.setScaleY(mLabel, scale);
ViewCompat.animate(mLabel)
.translationY(0f)
.scaleY(1f)
.scaleX(1f)
.setDuration(ANIMATION_DURATION)
.setListener(null)
.setInterpolator(mInterpolator).start();
} else {
mLabel.setVisibility(VISIBLE);
}
mEditText.setHint(null);
}
/**
* Hide the label
*/
private void hideLabel(boolean animate) {
if (animate) {
float scale = mEditText.getTextSize() / mLabel.getTextSize();
ViewCompat.setScaleX(mLabel, 1f);
ViewCompat.setScaleY(mLabel, 1f);
ViewCompat.setTranslationY(mLabel, 0f);
ViewCompat.animate(mLabel)
.translationY(mLabel.getHeight())
.setDuration(ANIMATION_DURATION)
.scaleX(scale)
.scaleY(scale)
.setListener(new ViewPropertyAnimatorListenerAdapter() {
@Override
public void onAnimationEnd(View view) {
mLabel.setVisibility(INVISIBLE);
mEditText.setHint(mHint);
}
})
.setInterpolator(mInterpolator).start();
} else {
mLabel.setVisibility(INVISIBLE);
mEditText.setHint(mHint);
}
}
/**
* Helper method to convert dips to pixels.
*/
private int dipsToPix(float dps) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dps,
getResources().getDisplayMetrics());
}
@Override
public void onChildViewAdded(View parent, View child) {
int childCount = getChildCount();
if (childCount == 1) {
mDrawable = getChildAt(0).getBackground();
final LayoutParams params = generateDefaultLayoutParams();
params.width = LayoutParams.WRAP_CONTENT;
params.height = LayoutParams.WRAP_CONTENT;
addView(mLabel, 0, params);
addView(mErrorLabel);
}
}
@Override
public void onChildViewRemoved(View parent, View child) {
}
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if (mEditText != null) {
mEditText.invalidate();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment