Created
October 25, 2013 03:58
-
-
Save jasonostrander/7149265 to your computer and use it in GitHub Desktop.
Float label example (http://bradfrostweb.com/blog/post/float-label-pattern/) for Android.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jwo.example.sampleform; | |
import android.annotation.TargetApi; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.graphics.Color; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
import android.util.AttributeSet; | |
import android.util.Log; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.*; | |
public class MainActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
LinearLayout layout = new LinearLayout(this); | |
layout.setOrientation(LinearLayout.VERTICAL); | |
setContentView(layout); | |
FormField field = new FormField(this); | |
field.setHint("Title"); | |
layout.addView(field); | |
View view = new View(this); | |
view.setBackgroundColor(Color.GRAY); | |
view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 2)); | |
layout.addView(view); | |
field = new FormField(this); | |
field.setHint("Address"); | |
layout.addView(field); | |
} | |
public static class FormField extends LinearLayout implements View.OnFocusChangeListener, TextWatcher { | |
EditText mEditText; | |
TextView mLabel; | |
public FormField(Context context) { | |
super(context); | |
init(); | |
} | |
private void init() { | |
setOrientation(VERTICAL); | |
mEditText = new EditText(getContext()); | |
mEditText.setBackground(null); | |
mLabel = new TextView(getContext()); | |
mLabel.setVisibility(View.INVISIBLE); | |
mLabel.setPadding(mEditText.getPaddingLeft(), 0, 0, 0); | |
addView(mLabel); | |
addView(mEditText); | |
mEditText.setOnFocusChangeListener(this); | |
mEditText.addTextChangedListener(this); | |
} | |
public void setHint(String text) { | |
mEditText.setHint(text); | |
mLabel.setText(text); | |
} | |
public void setText(String text) { | |
mEditText.setText(text); | |
} | |
@Override | |
public void onFocusChange(View v, boolean hasFocus) { | |
if (!hasFocus) { | |
mLabel.setTextColor(Color.GRAY); | |
} else { | |
mLabel.setTextColor(Color.BLUE); | |
} | |
} | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
} | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
} | |
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) | |
@Override | |
public void afterTextChanged(Editable s) { | |
if (s.length() > 0 && mLabel.getVisibility() == View.INVISIBLE) { | |
mLabel.setVisibility(View.VISIBLE); | |
mLabel.setAlpha(0); | |
mLabel.setTranslationY(mLabel.getHeight()/2); | |
mLabel.animate().alpha(1).translationY(0); | |
} else if (s.length() == 0 && mLabel.getVisibility() == View.VISIBLE) { | |
mLabel.animate().alpha(0).translationY(mLabel.getHeight()/2).withEndAction(new Runnable() { | |
@Override | |
public void run() { | |
mLabel.setVisibility(View.INVISIBLE); | |
} | |
}); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment