Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/IanGClifton/AndroidFloatLabel

Library project with a custom view that implements the Float Label pattern
https://github.com/IanGClifton/AndroidFloatLabel

Last synced: about 6 hours ago
JSON representation

Library project with a custom view that implements the Float Label pattern

Awesome Lists containing this project

README

        

AndroidFloatLabel
=================

This repository contains an Android library project for Android 4.0+ with a custom view that implements the Float Label pattern (http://dribbble.com/shots/1254439--GIF-Float-Label-Form-Interaction) and an example project using the Float Label library. The custom View, FloatLabel, basically consists of two pieces: the EditText and the label (a TextView).

Quick Overview
--------------

- [YouTube demo](http://www.youtube.com/watch?v=9VoVxw8aAx0)
- [Google Play example app](https://play.google.com/store/apps/details?id=com.iangclifton.android.floatlabelexample)

Getting Started
---------------

Download the source to use it as a library project or use it directly from Maven Central in your dependencies. For example:

dependencies {
compile 'com.iangclifton.android:floatlabel:1.0.4'
}

For most use, you can simply use the custom view in your XML layout, specifying a label to use as both the EditText hint and the label TextView with the android:hint property. Example:

You can also dynamically set the label with floatLabel.setLabel("Custom Label") or floatLabel.setLabel(R.string.custom_label). You can dynamically set the text of the EditText with floatLabel.setText(). All the typical setText variations are supported. If you want to set the text without an animation (such as if you're programmatically preparing views in onCreate), use floatLabel.setTextWithoutAnimation() (again, all the usual variations are supported).

If you need a reference to the EditText, you can call floatLabel.getEditText().

Custom Layout
-------------

If you want to specify a custom layout to use, you can do something like this:

Your custom layout should include a label TextView (id/float_label) and an EditText (id/edit_text). The custom layouts are extremely limited because the FloatLabel simply lays out the label and the EditText and ignores all other views. This is very efficient but also prevents you from creating a much more complex layout. Here's an example:





Custom Animation
----------------

You can override the animations used to show and hide the label by implementing a FloatLabel.LabelAnimator and calling floatLabel.setLabelAnimator(new MyLabelAnimator());. Note that you should use the alpha property of the label to show and hide it rather than the View.setVisibility(int) method. Example:

private static class CustomLabelAnimator implements FloatLabel.LabelAnimator {
/*package*/ static final float SCALE_X_SHOWN = 1f;
/*package*/ static final float SCALE_X_HIDDEN = 2f;
/*package*/ static final float SCALE_Y_SHOWN = 1f;
/*package*/ static final float SCALE_Y_HIDDEN = 0f;

@Override
public void onDisplayLabel(View label) {
final float shift = label.getWidth() / 2;
label.setScaleX(SCALE_X_HIDDEN);
label.setScaleY(SCALE_Y_HIDDEN);
label.setX(shift);
label.animate().alpha(1).scaleX(SCALE_X_SHOWN).scaleY(SCALE_Y_SHOWN).x(0f);
}

@Override
public void onHideLabel(View label) {
final float shift = label.getWidth() / 2;
label.setScaleX(SCALE_X_SHOWN);
label.setScaleY(SCALE_Y_SHOWN);
label.setX(0f);
label.animate().alpha(0).scaleX(SCALE_X_HIDDEN).scaleY(SCALE_Y_HIDDEN).x(shift);
}
}

Related Projects
----------------

* [Fork of Float Label for Android 2.3+](https://github.com/edouardouvrard/AndroidFloatLabel-API9)