Oush
Oush

Reputation: 3348

Applying a custom typeface to an editText

I have 2 EditText(s) in my activity. One for a mobile no, and the other for a pin no. The mobile no editText has an inputType of phone:

Mobile No EditText

<android.support.design.widget.TextInputLayout
        android:id="@+id/tiMobileNoFirst"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="10dp">

        <myPackageName.CustomEditText
            android:id="@+id/etMobileNo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/mobile_no"
            android:inputType="phone"
            android:maxLength="12"
            android:maxLines="1"
            android:textColor="@color/black" />
    </android.support.design.widget.TextInputLayout>

and the pin no has an inputType of numberPassword:

PIN No EditText

<android.support.design.widget.TextInputLayout
        android:id="@+id/tiPINFirst"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp">

        <myPackageName.CustomEditText
            android:id="@+id/etPINFirst"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/pin"
            android:inputType="numberPassword"
            android:maxLength="4"
            android:maxLines="1"
            android:textColor="@color/black" />
    </android.support.design.widget.TextInputLayout>

my CustomEditText class looks like below:

public class CustomEditText extends AppCompatEditText {
public CustomEditText(Context context) {
    super(context);
    EditHelper.setTypeFace(context,this);
}

public CustomEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    EditHelper.setTypeFace(context,this);
}

public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    EditHelper.setTypeFace(context,this);
}
}

class EditHelper{
static Typeface typeface;

public static void setTypeFace(Context context, EditText editText){
    if(typeface == null){
        typeface = Typeface.createFromAsset(context.getAssets(),"fonts/canaro_light.otf");
    }
    editText.setTypeface(typeface,Typeface.BOLD);
}
}

Applying the custom Typeface to the mobile no works well, however on applying it to the pin no, doesn't work, it reverts back to the default android typeface.

Changing the inputType of the pin to something else works, however I need to use the numberPassword in this situation.

How can I accomplish the desired result?

Upvotes: 0

Views: 69

Answers (2)

V-rund Puro-hit
V-rund Puro-hit

Reputation: 5534

numberPassword uses its own typeface.

what you can do is set input type of your pin no edittext as number and programmatically set its **transformation method* to password

etPINFirst.setTransformationMethod(new PasswordTransformationMethod());

and change android:inputType="numberPassword" to android:inputType="number" in your etPINFirst

Upvotes: 1

Oush
Oush

Reputation: 3348

Setting the inputType in Java did the trick!

etPINFirst.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_VARIATION_PASSWORD);

Upvotes: 0

Related Questions