Skip to content

Instantly share code, notes, and snippets.

@malmstein
Last active August 29, 2015 14:06
Show Gist options
  • Save malmstein/a31113ca820a31ed2a24 to your computer and use it in GitHub Desktop.
Save malmstein/a31113ca820a31ed2a24 to your computer and use it in GitHub Desktop.
custom layout attribute loading
<declare-styleable name="FloatLabelLayout">
<attr name="floatLabelTextAppearance" format="reference" />
<attr name="floatLabelSidePadding" format="reference|dimension" />
</declare-styleable>
public final class FloatLabelLayout extends FrameLayout {
private static final long ANIMATION_DURATION = 150;
private static final float DEFAULT_PADDING_LEFT_RIGHT_DP = 12f;
private EditText mEditText;
private TextView mLabel;
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);
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);
mLabel.setTextAppearance(context,
a.getResourceId(R.styleable.FloatLabelLayout_floatLabelTextAppearance,
android.R.style.TextAppearance_Small));
addView(mLabel, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
a.recycle();
}
<com.novoda.dch.view.FloatLabelLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:floatLabelTextAppearance="@style/dch_FloatLabel">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment