Search code examples
androidkotlinandroid-edittextandroid-softkeyboardtruetype

Kotlin - Issue in opening Soft Keyboard for the CustomFonts


I have created below class to set the custom fonts or any otf fonts in my normal EditText. Am working with kotlin and created below class for the generalization of using CustomEditText.

I have added fonts file in asset and created below class named CustomEditText:

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Typeface
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatEditText

class CustomEditText : AppCompatEditText{

constructor(context: Context) : this(context, null) {
    init(null)
}

constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) {
    init(attrs)
}

constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
    context,
    attrs,
    defStyleAttr
) {
    init(attrs)
}

private fun init(attrs: AttributeSet?) {
    CustomEditText(context, attrs, 0)
}

/**
 * @param context:This is an abstract class whose implementation is provided by Android Operating System.
 * @param attrs:A collection of attributes, as found associated with a tag in an XML document.
 * @param defStyle:
 */
fun CustomEditText(
    context: Context,
    attrs: AttributeSet?,
    defStyle: Int
) {
    try {
        val a: TypedArray =
            context.obtainStyledAttributes(attrs, R.styleable.CustomEditText, defStyle, 0)
        val customEnumValue: CustomEnumBrandonNew.CustomFontType =
            CustomEnumBrandonNew.CustomFontType.fromId(
                a.getInt(
                    R.styleable.CustomEditText_font_type,
                    0
                )
            )
        a.recycle()
        typeface = when (customEnumValue) {
            CustomEnumBrandonNew.CustomFontType.BLACK ->
                Typeface.createFromAsset(context.assets, "BrandonText-Black.otf") // BrandonTextBlack().getInstance(context)?.getTypeFace()
            CustomEnumBrandonNew.CustomFontType.BLACK_ITALIC ->
                Typeface.createFromAsset(context.assets, "BrandonText-BlackItalic.otf") // BrandonTextBlackItalic().getInstance(context)?.getTypeFace()
            CustomEnumBrandonNew.CustomFontType.BOLD ->
                Typeface.createFromAsset(context.assets, "BrandonText-Bold.otf") // BrandonTextBold().getInstance(context)?.getTypeFace()
            CustomEnumBrandonNew.CustomFontType.BOLD_ITALIC ->
                Typeface.createFromAsset(context.assets, "BrandonText-BoldItalic.otf") // BrandonTextBoldItalic().getInstance(context)?.getTypeFace()
            CustomEnumBrandonNew.CustomFontType.LIGHT ->
                Typeface.createFromAsset(context.assets, "BrandonText-Light.otf") // BrandonTextBoldItalic().getInstance(context)?.getTypeFace()
            CustomEnumBrandonNew.CustomFontType.LIGHT_ITALIC ->
                Typeface.createFromAsset(context.assets, "BrandonText-LightItalic.otf") // BrandonTextBoldItalic().getInstance(context)?.getTypeFace()
            CustomEnumBrandonNew.CustomFontType.MEDIUM ->
                Typeface.createFromAsset(context.assets, "BrandonText-Medium.otf") // BrandonTextBoldItalic().getInstance(context)?.getTypeFace()
            CustomEnumBrandonNew.CustomFontType.MEDIUM_ITALIC ->
                Typeface.createFromAsset(context.assets, "BrandonText-MediumItalic.otf") // BrandonTextBoldItalic().getInstance(context)?.getTypeFace()
            CustomEnumBrandonNew.CustomFontType.REGULAR ->
                Typeface.createFromAsset(context.assets, "BrandonText-Regular.otf") // BrandonTextBoldItalic().getInstance(context)?.getTypeFace()
            CustomEnumBrandonNew.CustomFontType.REGULAR_ITALIC ->
                Typeface.createFromAsset(context.assets, "BrandonText-RegularItalic.otf") // BrandonTextBoldItalic().getInstance(context)?.getTypeFace()
            CustomEnumBrandonNew.CustomFontType.THIN ->
                Typeface.createFromAsset(context.assets, "BrandonText-Thin.otf") // BrandonTextBoldItalic().getInstance(context)?.getTypeFace()
            CustomEnumBrandonNew.CustomFontType.THIN_ITALIC ->
                Typeface.createFromAsset(context.assets, "BrandonText-ThinItalic.otf") // BrandonTextBoldItalic().getInstance(context)?.getTypeFace()
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}
}

Before it, I have also created seperate classes i.e. BrandonTextBlack, BrandonTextBlackItalic, BrandonTextBold etc. as below :

class BrandonTextBold{

private var instance: BrandonTextBold? = null
private var typeface: Typeface? = null

fun getInstance(context: Context): BrandonTextBold? {
    synchronized(BrandonTextBold::class.java) {
        if (instance == null) {
            typeface = Typeface.createFromAsset(context.resources.assets, "BrandonText-Bold.otf")
        }
        return instance
    }
}

fun getTypeFace(): Typeface? {
    return typeface
}

}

Now, in layout xml file, using it as below :

<CustomEditText
            android:id="@+id/edt_mobile_number"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="@dimen/dimen_40"
            android:background="@drawable/drawable_round_button_transperent"
            android:hint="@string/str_hint_enter_your_mobile_number"
            android:imeOptions="actionDone"
            android:inputType="number|phone"
            android:maxLength="10"
            android:textColorHint="@color/colorHintAndBorder"
            android:maxLines="1"
            android:paddingBottom="@dimen/dimen_8"
            android:paddingTop="@dimen/dimen_8"
            android:paddingRight="@dimen/dimen_15"
            android:paddingLeft="@dimen/dimen_15"
            android:textColor="@color/colorBlack"
            android:textSize="@dimen/font_dimen_20"
            app:font_type="regular" />

Here notice that I have used app:font_type="regular". So, I can have normal font type in my edittext. It's done.

But, the issue is softkeyboard not opening when I press or try to write something in it.

What might be the issue?


Solution

  • Use default style as EditText style in your all constructors instead of 0

    From

     constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) {
            init(attrs)
        }
    

    To

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, android.R.attr.editTextStyle) {
            init(attrs)
        }