Skip to content

Instantly share code, notes, and snippets.

@thanhbinh84
Created July 30, 2017 10:44
Show Gist options
  • Save thanhbinh84/c36f930c1978e4f0d949c8f9a7662495 to your computer and use it in GitHub Desktop.
Save thanhbinh84/c36f930c1978e4f0d949c8f9a7662495 to your computer and use it in GitHub Desktop.
Customize TextInputLayout for workaround the issue https://github.com/chrisjenx/Calligraphy/issues/201
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.AttributeSet;
import uk.co.chrisjenx.calligraphy.CalligraphyTypefaceSpan;
import static se.bghrt.bigheart.utils.Utils.getTypeFace;
/**
* Customize TextInputLayout for workaround the issue https://github.com/chrisjenx/Calligraphy/issues/201
*/
public class CalligraphyTextInputLayout extends TextInputLayout {
private Context mContext;
public CalligraphyTextInputLayout(Context context) {
this(context, null, 0);
}
public CalligraphyTextInputLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CalligraphyTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
}
@Override
public void setError(@Nullable CharSequence error) {
Spannable spannable = null;
if (!TextUtils.isEmpty(error))
spannable = wrapInCustomFont(mContext, error.toString());
super.setError(spannable);
}
private Spannable wrapInCustomFont(Context context, String text) {
CalligraphyTypefaceSpan typefaceSpan = new CalligraphyTypefaceSpan(getTypeFace(context));
SpannableString spannable = new SpannableString(text);
spannable.setSpan(typefaceSpan, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return spannable;
}
}
@Lingviston
Copy link

Why SPAN_EXCLUSIVE_EXCLUSIVE? Shouldn't it be SPAN_INCLUSIVE_EXCLUSIVE?

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