Last active
July 26, 2021 18:02
-
-
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
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
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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