Skip to content

Instantly share code, notes, and snippets.

@ljubisa987
Last active July 26, 2021 18:02
Show Gist options
  • Save ljubisa987/e33cd5597da07172c55d to your computer and use it in GitHub Desktop.
Save ljubisa987/e33cd5597da07172c55d to your computer and use it in GitHub Desktop.
TextInputLayout temporary workaround for hint not showing on initial load for SDK > 20
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);
}
public CustomTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@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;
}
}
}
@cashlo
Copy link

cashlo commented Jun 25, 2015

When the hint is added programmatically to the EditText, it's shown twice.
I had to clear it:

            if (currentEditTextHint != null && currentEditTextHint.length() > 0) {
                mHint = currentEditTextHint;
                getEditText().setHint("");
            }

@StErMi
Copy link

StErMi commented Jun 28, 2015

@cashio you can replace currentEditTextHint != null && currentEditTextHint.length() > 0 with !TextUtils.isEmpty(currentEditTextHint)

@AfzalivE
Copy link

hints do not respect transitions, they appear immediately instead of animating.

Fixed here, might be slightly hacky but whatever
https://gist.github.com/AfzalivE/eea5918ac0c61eb08343

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment