GithubHelp home page GithubHelp logo

jashion / androidfloatlabel Goto Github PK

View Code? Open in Web Editor NEW

This project forked from iangclifton/androidfloatlabel

0.0 2.0 0.0 1.06 MB

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

License: Apache License 2.0

Java 100.00%

androidfloatlabel's Introduction

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

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.2'
}

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:

<com.iangclifton.android.floatlabel.FloatLabel
    android:id="@+id/float_label_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/example_label" />

You can also dynamically set the label with floatLabel.setLabel("Custom Label") or floatLabel.setLabel(R.string.custom_label).

Custom Layout

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

<com.iangclifton.android.floatlabel.FloatLabel
    android:id="@+id/float_label_custom_layout_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/example_label"
    android:layout="@layout/custom_float_label" />

Your custom layout should include a label TextView (id/float_label) and an EditText (id/edit_text). Right now, 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:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
    <TextView
        android:id="@id/float_label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="1"
        android:textIsSelectable="true"
        android:textAppearance="?android:attr/textAppearanceSmall" />
    <EditText
        android:id="@id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text|textAutoCorrect|textCapSentences|textAutoComplete" />
</merge>

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

androidfloatlabel's People

Contributors

edouardouvrard avatar iangclifton avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.