Custom EditText is not showing keyboard on focus
Asked Answered
K

6

31

I am creating a custom EditText class because i need to set some custom fonts; However now when i click on the editText the android keyboard does not pop up anymore...

here is my class:

    package ro.gebs.captoom.utils.fonts;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import ro.gebs.captoom.R;

public class CustomFontEditText extends EditText {

    private Context context;

    public CustomFontEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (!isInEditMode()) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFontEditText,
                    defStyle, 0);

            assert a != null;
            int fontId = a.getInteger(R.styleable.CustomFontEditText_fontNameEdit, -1);
            if (fontId == -1) {
                throw new IllegalArgumentException("The font_name attribute is required and must refer "
                        + "to a valid child.");
            }
            a.recycle();
            initialize(fontId);
        }
        this.context = context;
    }

    public CustomFontEditText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        this.context = context;
    }

    public CustomFontEditText(Context context) {
        super(context);
        this.context = context;
    }

    @SuppressWarnings("ConstantConditions")
    public void initialize(int fontId) {

        Typeface tf = null;
        switch (fontId) {
            case 0:
                tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/OpenSans-Regular.ttf");
                break;
            case 1:
                tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/OpenSans-Bold.ttf");
                break;
            case 2:
                tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/OpenSans-Semibold.ttf");
                break;
            case 3:
                tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/OpenSans-ExtraBold.ttf");
                break;
        }

        setTypeface(tf);
    }
}

and how i use it in XML:

<ro.gebs.captoom.utils.fonts.CustomFontEditText
                        android:id="@+id/add_details_txt_edit"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dip"
                        android:hint="@string/type_here"
                        android:inputType="textPersonName"
                        custom:fontNameEdit="Regular" />

I thought the focusing events were handled by the fact that i extend the EditText class...

Any hints?

Kenlee answered 17/9, 2013 at 11:32 Comment(0)
S
116

It's an old question but if someone cares, the problem is on the implementation of the constructor:

public CustomFontEditText(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
    this.context = context;
}

The last argument ("defStyle") which you set as 0, should be the default style for an EditText. If you take a look at the same constructor on the EditText class:

public EditText(Context context, AttributeSet attrs) {
    this(context, attrs, com.android.internal.R.attr.editTextStyle);
}

As you can see, the default style for an EditText should be used, so your constructor should look like this:

public CustomFontEditText(Context context, AttributeSet attrs) {
    this(context, attrs, android.R.attr.editTextStyle);
    this.context = context;
}
Stome answered 4/4, 2014 at 8:49 Comment(9)
This is the proper fixPlotkin
final int com.android.internal.R.attr.editTextStyle = 0x0101006e if anyone is interested.Cloying
Awesome hint! Now I will always check out the source for the constructorsClemmy
Wow, I've been struggling on this one with a custom font version of EditText... ThanksAward
I use it ,but will happen error Error:com.android.internal.R can not foundSubantarctic
If you are inheriting from AppCompatEditText and have the error @Subantarctic has use android.support.v7.appcompat.R.attr.editTextStyle instead of android.R.attr.editTextStyle.Farmer
The question here is: why?Delfinadelfine
And now it is a androidx.appcompat.R.attr.editTextStyleKeek
What if we want to use a custom style?Rapp
O
4

Make custom EditText with Kotlin to fix focus problem:

class MyEditText @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = android.R.attr.editTextStyle) : AppCompatEditText(context, attrs, defStyleAttr) {


 init {
    doSomething(context)
 }

 private fun doSomething(context: Context) {
   // TODO Set your custom font or whatever you need
 }

}
Osmic answered 23/6, 2020 at 14:23 Comment(1)
defStyleAttr: Int = android.R.attr.editTextStyle is most importantRidicule
R
0
implements KeyListener on your custom EditText Class and override methods of KeyListener
Rugose answered 17/9, 2013 at 11:49 Comment(0)
I
0

try to make reference for edit text at runtime and call request focus()

    et.requestFocus()

and try

   android:focusable="true"
Inimical answered 17/9, 2013 at 11:50 Comment(0)
S
-1

add this

 android:focusable="true"
Sacramentarian answered 17/9, 2013 at 11:34 Comment(0)
S
-3
 editText.setOnTouchListener(new OnTouchListener() 
  {
    @Override
    public boolean onTouch(View v, MotionEvent event) 
     {
         editText.setFocusableInTouchMode(true);
         return false;
     }
  });
Scrimmage answered 17/9, 2013 at 11:59 Comment(3)
this works just fine but i have to add it for each instance of the CustomEditText class, isn't there a way i can add this in my custom class?Kenlee
@AdrianOlar:You need to add for each one instead of one class. Also if this answer solved your problem, accept it.Scrimmage
This is wrong answer. The answer by @lluismontero is accurate.Cadaver

© 2022 - 2024 — McMap. All rights reserved.