Skip to content

Commit

Permalink
Adjust label metrics according with Material Design specs. Update Gr…
Browse files Browse the repository at this point in the history
…adle plugin and build tools . Separate lib from sample app.
  • Loading branch information
JesusM committed Mar 19, 2015
1 parent d1466dd commit c19b682
Show file tree
Hide file tree
Showing 23 changed files with 158 additions and 98 deletions.
22 changes: 10 additions & 12 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
apply plugin: 'android'
apply plugin: 'com.android.application'

android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
compileSdkVersion parent.ext.compileSdkVersion
buildToolsVersion parent.ext.buildToolsVersion

defaultConfig {
minSdkVersion 11
targetSdkVersion 19
versionCode 1
versionName "1.0"
minSdkVersion parent.ext.minSdkVersion
targetSdkVersion parent.ext.targetSdkVersion
versionName parent.ext.versionName
versionCode parent.ext.versionCode
}
buildTypes {
release {
runProguard false
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:19.1.0'
compile 'com.android.support:support-v4:19.1.0'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.android.support:appcompat-v7:22.+'
compile project(':lib')
}
22 changes: 10 additions & 12 deletions app/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin">

<com.jesusm.floatinglabelpass.app.ui.activities.customviews.FloatLabelLayout
<com.example.jesus.lib.FloatLabelLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:floatLabelTextAppearance="@style/TextAppearance.YourApp.FloatLabel">
android:layout_height="wrap_content">

<EditText
android:id="@+id/edit_username"
Expand All @@ -22,12 +22,11 @@
android:imeOptions="actionNext"
android:nextFocusDown="@+id/edit_password" />

</com.jesusm.floatinglabelpass.app.ui.activities.customviews.FloatLabelLayout>
</com.example.jesus.lib.FloatLabelLayout>

<com.jesusm.floatinglabelpass.app.ui.activities.customviews.PasswordFloatLabelLayoutCheck
<com.example.jesus.lib.PasswordFloatLabelLayoutCheck
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:floatLabelTextAppearance="@style/TextAppearance.YourApp.FloatLabel"
app:showStateIcon="true">

Expand All @@ -39,12 +38,11 @@
android:singleLine="true"
android:imeOptions="actionDone" />

</com.jesusm.floatinglabelpass.app.ui.activities.customviews.PasswordFloatLabelLayoutCheck>
</com.example.jesus.lib.PasswordFloatLabelLayoutCheck>

<com.jesusm.floatinglabelpass.app.ui.activities.customviews.MailLabelLayoutCheck
<com.example.jesus.lib.MailLabelLayoutCheck
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:floatLabelTextAppearance="@style/TextAppearance.YourApp.FloatLabel"
app:showStateIcon="true">

Expand All @@ -56,6 +54,6 @@
android:singleLine="true"
android:imeOptions="actionDone" />

</com.jesusm.floatinglabelpass.app.ui.activities.customviews.MailLabelLayoutCheck>
</com.example.jesus.lib.MailLabelLayoutCheck>

</LinearLayout>
7 changes: 0 additions & 7 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,4 @@
<!-- Customize your theme here. -->
</style>


<style name="TextAppearance.YourApp.FloatLabel" parent="android:TextAppearance.Small">
<item name="android:textColor">@color/float_label</item>
<item name="android:textSize">11sp</item>
<item name="android:textStyle">bold</item>
</style>

</resources>
18 changes: 17 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,26 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
classpath 'com.android.tools.build:gradle:1.1.0'
}
}


ext {
versionMajor = 1
versionMinor = 1
versionPatch = 0

versionCode = versionMajor * 1000 + versionMinor * 100 + versionPatch
versionName = versionMajor + "." + versionMinor + "." + versionPatch

minSdkVersion = 14
targetSdkVersion = 22
compileSdkVersion = 22
buildToolsVersion = '22.0.0'

}

allprojects {
repositories {
mavenCentral()
Expand Down
1 change: 1 addition & 0 deletions lib/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
23 changes: 23 additions & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion parent.ext.compileSdkVersion
buildToolsVersion parent.ext.buildToolsVersion

defaultConfig {
minSdkVersion parent.ext.minSdkVersion
targetSdkVersion parent.ext.targetSdkVersion
versionName parent.ext.versionName
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile 'com.android.support:support-v4:22.+'
compile 'com.nineoldandroids:library:2.4.0'
}
17 changes: 17 additions & 0 deletions lib/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/jesus/Library/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.jesus.lib;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
8 changes: 8 additions & 0 deletions lib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jesus.lib">

<application android:allowBackup="true" android:label="@string/app_name">

</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package com.jesusm.floatinglabelpass.app.ui.activities.customviews;
package com.example.jesus.lib;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.content.res.ResourcesCompat;
import android.text.InputType;
import android.util.AttributeSet;
import android.widget.EditText;

import com.jesusm.floatinglabelpass.app.R;
import com.nineoldandroids.animation.ArgbEvaluator;
import com.nineoldandroids.animation.ValueAnimator;

/**
* Created by Jesus on 05/05/14.
*/
public abstract class CheckStrenghtLabelLayout extends FloatLabelLayout {

private static final long LABEL_COLOR_CHANGE_ANIMATION_TIME = 333L;
Expand Down Expand Up @@ -67,7 +64,7 @@ private void checkDrawAcceptIcon() {

private void initAcceptDrawableCompound() {
// Read your drawable from somewhere
Drawable dr = getResources().getDrawable(R.drawable.ic_action_navigation_accept);
Drawable dr = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_action_navigation_accept, null);
Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
// Scale it to the label size
int acceptDrawableSize = (int) getLabel().getTextSize();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jesusm.floatinglabelpass.app.ui.activities.customviews;/*
package com.example.jesus.lib;/*
* Copyright (C) 2014 Chris Banes
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -17,19 +17,18 @@
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jesusm.floatinglabelpass.app.R;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;
Expand All @@ -42,17 +41,15 @@
* @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 FrameLayout {
public class FloatLabelLayout extends LinearLayout {


private static final float DEFAULT_PADDING_LEFT_RIGHT_DP = 12f;
private static final long ANIMATION_DURATION = 150;
private static final int DEFAULT_PADDING_TOP_BOTTOM_DP = 8;
private static final long ANIMATION_DURATION = 100;
private int mLabelColor = Color.GRAY;

private EditText mEditText;
private TextView mLabel;


public FloatLabelLayout(Context context) {
this(context, null);
}
Expand All @@ -63,46 +60,42 @@ public FloatLabelLayout(Context context, AttributeSet attrs) {

public FloatLabelLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setOrientation(VERTICAL);

final TypedArray a = context
.obtainStyledAttributes(attrs, R.styleable.FloatLabelLayout);

final int sidePadding = a.getDimensionPixelSize(
R.styleable.FloatLabelLayout_floatLabelSidePadding,
dipsToPix(DEFAULT_PADDING_LEFT_RIGHT_DP));
mLabel = new TextView(context);
mLabel.setPadding(sidePadding, 0, sidePadding, 0);
mLabel.setVisibility(INVISIBLE);
initLabelAppearance(context, a);
mLabelColor = mLabel.getCurrentTextColor();
addView(mLabel, 0, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

a.recycle();
}

private void initLabelAppearance(Context context, TypedArray a) {
mLabel.setVisibility(INVISIBLE);
mLabel.setPadding(0, dipsToPix(DEFAULT_PADDING_TOP_BOTTOM_DP), 0, 0);
mLabel.setTextAppearance(context,
a.getResourceId(R.styleable.FloatLabelLayout_floatLabelTextAppearance,
android.R.style.TextAppearance_Small)
R.style.PasswordFloatingLabelLayout_DefaultStyle)
);
mLabelColor = mLabel.getCurrentTextColor();


addView(mLabel, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);


a.recycle();
}


@Override
public final void addView(View child, int index, ViewGroup.LayoutParams params) {
public final void addView(@NonNull View child, int index, ViewGroup.LayoutParams params) {
if (child instanceof 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");
}

// Update the layout params so that the EditText is at the bottom, with enough top
// margin to show the label
final LayoutParams lp = new LayoutParams(params);
lp.gravity = Gravity.BOTTOM;
lp.topMargin = (int) mLabel.getTextSize();
params = lp;

child.setPadding(child.getPaddingLeft(), dipsToPix(DEFAULT_PADDING_TOP_BOTTOM_DP),
child.getPaddingRight(), dipsToPix(DEFAULT_PADDING_TOP_BOTTOM_DP));
if (mLabel != null) {
mLabel.setPadding(child.getPaddingLeft(), mLabel.getPaddingTop(),
child.getPaddingRight(), mLabel.getPaddingBottom());
}
setEditText((EditText) child);
}

Expand Down Expand Up @@ -199,7 +192,7 @@ public TextView getLabel() {
*/
private void showLabel() {
ObjectAnimator alpha = ObjectAnimator.ofFloat(mLabel, "alpha", 0f, 1f);
ObjectAnimator translationY = ObjectAnimator.ofFloat(mLabel, "translationY", mLabel.getHeight(), 0f);
ObjectAnimator translationY = ObjectAnimator.ofFloat(mLabel, "translationY", mLabel.getHeight() / 2, 0f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(ANIMATION_DURATION);
animatorSet.playTogether(alpha, translationY);
Expand All @@ -226,18 +219,16 @@ public void onAnimationRepeat(Animator animation) {
});
animatorSet.start();


}


/**
* Hide the label using an animation
*/
private void hideLabel() {
ViewHelper.setAlpha(mLabel, 1f);
ViewHelper.setTranslationY(mLabel, 0f);
ObjectAnimator alpha = ObjectAnimator.ofFloat(mLabel, "alpha", 0f);
ObjectAnimator translationY = ObjectAnimator.ofFloat(mLabel, "translationY", mLabel.getHeight());
ObjectAnimator translationY = ObjectAnimator.ofFloat(mLabel, "translationY", mLabel.getHeight() / 2);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.addListener(new com.nineoldandroids.animation.Animator.AnimatorListener() {
@Override
Expand All @@ -247,7 +238,7 @@ public void onAnimationStart(com.nineoldandroids.animation.Animator animation) {

@Override
public void onAnimationEnd(com.nineoldandroids.animation.Animator animation) {
mLabel.setVisibility(View.GONE);
mLabel.setVisibility(View.INVISIBLE);
}

@Override
Expand Down
Loading

0 comments on commit c19b682

Please sign in to comment.