Skip to content

Instantly share code, notes, and snippets.

@AfzalivE
Forked from ljubisa987/gist:e33cd5597da07172c55d
Last active August 29, 2015 14:24
Show Gist options
  • Save AfzalivE/eea5918ac0c61eb08343 to your computer and use it in GitHub Desktop.
Save AfzalivE/eea5918ac0c61eb08343 to your computer and use it in GitHub Desktop.
TextInputLayout with fixed "hints not showing"
package com.centralway.numbrs.numbrsapp.views;
import android.content.Context;
import android.graphics.Canvas;
import android.support.design.widget.TextInputLayout;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class CustomTextInputLayout extends TextInputLayout {
private boolean mIsHintSet;
private CharSequence mHint;
public CustomTextInputLayout(Context context) {
super(context);
setTransitionGroup(true);
}
public CustomTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
setTransitionGroup(true);
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (child instanceof EditText) {
// Since hint will be nullify on EditText once on parent addView, store hint value locally
mHint = ((EditText)child).getHint();
}
super.addView(child, index, params);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (!mIsHintSet && ViewCompat.isLaidOut(this)) {
// We have to reset the previous hint so that equals check pass
setHint(null);
// In case that hint is changed programatically
CharSequence currentEditTextHint = getEditText().getHint();
if (currentEditTextHint != null && currentEditTextHint.length() > 0) {
mHint = currentEditTextHint;
}
setHint(mHint);
mIsHintSet = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment