1

I have layout like this -

<android.support.design.widget.TextInputLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content">

     <android.support.design.widget.TextInputEditText
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:hint="@string/form_username"/>

 </android.support.design.widget.TextInputLayout>

As per our UI design document, we need to have different custom font for Floating label and Edittext.

Any help is appreciated. Thank you.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Nagesh Jatagond
  • 344
  • 2
  • 13

2 Answers2

2

you can do this by this way,

As of Design Library v23, you can use TextInputLayout#setTypeface().

This will set the typeface on both the expanded and floating hint.

Using a custom span

final SpannableString ss = new SpannableString("Error");
ss.setSpan(new FontSpan(tf), 0, ss.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
til.setError(ss);

private static final class FontSpan extends MetricAffectingSpan {

    private final Typeface mNewFont;

    private FontSpan(Typeface newFont) {
        mNewFont = newFont;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        ds.setTypeface(mNewFont);
    }

    @Override
    public void updateMeasureState(TextPaint paint) {
        paint.setTypeface(mNewFont);
    }
}

enter image description here


Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
  • 1
    Please don't just copy from other users' answers. If you think another another post has the solution(s), vote it as a duplicate of that post. – Mike M. Apr 19 '17 at 08:55
  • i know that sir, but this question is some what different. – Amit Vaghela Apr 19 '17 at 08:56
  • Really? Even though you think a straight copy-paste from answers there is the solution? By "there", I mean [this post](http://stackoverflow.com/questions/30765287/change-font-of-the-floating-label-edittext-and-textinputlayout), the link for which you just edited out of your answer. – Mike M. Apr 19 '17 at 09:04
  • 1
    i know i have edied but have given solution to questioner and provided combined solution. – Amit Vaghela Apr 19 '17 at 09:06
  • 1
    I don't see where OP is asking about the error text. Did you even read the code you plagiarized? It is plagiarism now, since you've removed any sort of attribution. – Mike M. Apr 19 '17 at 09:30
  • i know that if user want to customize his font than it can be done by this way in TextInputLayout – Amit Vaghela Apr 19 '17 at 09:34
0

For custom fonts in EditText you can use following class:

public class CustomEditText extends AppCompatEditText {

public CustomEditText (Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(attrs);
}

public CustomEditText (Context context, AttributeSet attrs) {
    super(context, attrs);
    init(attrs);

}

public CustomEditText (Context context) {
    super(context);
    init(null);
}

Typeface myTypeface;

private void init(AttributeSet attrs) {

    if (attrs != null) {
        TypedArray a = getContext().obtainStyledAttributes(attrs,
                R.styleable.CustomTextView);
        String fontName = "Orkney Medium.otf";
        if (fontName != null && !isInEditMode()) {
            myTypeface = Typeface.createFromAsset(getContext().getAssets(),
                    fontName);

        }
        setTypeface(myTypeface);
        a.recycle();
    }


}

}

XML Code :

  <com.utils.CustomEditText
                    android:id="@+id/edt_first_name_register"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@null"
                    android:hint="@string/hint_first_name"
                    android:inputType="textCapWords"
                    android:imeOptions="actionNext"
                    android:singleLine="true"
                    android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
                    android:paddingTop="@dimen/15dp"
                    android:textColor="@color/black"
                    android:textSize="20sp" />

For TextInputLayout custom classes does not work in all cases, But you can do something like this for optimized solution.

Put this in your Java Class :

 public static void setTextInputLayoutTypeFace(Context mContext, TextInputLayout... textInputLayout) {
    for (TextInputLayout til : textInputLayout) {
        Typeface typeface = Typeface.createFromAsset(mContext.getAssets(), "Orkney Regular.otf");
        til.setTypeface(typeface);
    }

}

Call the above method using :

setTextInputLayoutTypeFace(mContext, tlFirstNameRegister, tlLastNameRegister,
            tlUsernameRegister, tlEmailIdRegister, tlDateOfBirthRegister, tlMobileRegister, tlPasswordRegister,
            tlConfPasswordRegister);

Its not the best solution. But it will work.

Nir Patel
  • 357
  • 4
  • 17