3

I have an EditText wrapped in a TextInputLayout below a RecyclerView, both inside a NestedScrollView.

Here is my layout a bit simplified :

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="fill_vertical"
    android:layout_marginBottom="?attr/actionBarSize"
    android:background="@android:color/white"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- Stuff -->

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layoutManager="LinearLayoutManager"
            tools:listitem="@layout/list_item_comment_empty"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <android.support.design.widget.TextInputLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:paddingEnd="@dimen/global_horizontal_padding"
              android:paddingRight="@dimen/global_horizontal_padding"
                android:textColorHint="@color/light_green_color">

                <custom.FontEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:hint="@string/your_comment_here"
                    android:textColor="@android:color/black"
                    custom:fontName="lato"
                    custom:fontStyle="regular"/>
            </android.support.design.widget.TextInputLayout>

            <ImageButton
                style="@style/OrangeButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="@string/send"
                android:padding="@dimen/global_vertical_padding"
                android:src="@drawable/ic_send"/>
        </LinearLayout>

In my Manifest I have this on the activity :

android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan"

My problem is :

The EditText won't keep the focus on touch. I touch it, the bar below it appears like it should, but then immediately disapear like if it just lost the focus : no Hint going up, no bar below it : nothing. And of course no Keyboard showing up.

Any idea is very welcome. Thanks in advance for your help.

Edit : My question has been marked as a possible duplicate of this one EditText Loses Focus on Click It's not. I have tried those solutions, it doesn't work, and the Keyboard doesn't shows up at all.

**Edit : **

Here my custom Edit Text code :

public class FontEditText extends EditText {
    private int mFontName;
    private int mFontStyle;

    public FontEditText(Context context) {
        super(context);
        init(context, null);
    }

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

    public FontEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public FontEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.FontViewAttributes,
                0, 0);

        try {
            mFontName = a.getInteger(R.styleable.FontViewAttributes_fontName, 3);
            mFontStyle = a.getInteger(R.styleable.FontViewAttributes_fontStyle, 3);
        } finally {
            a.recycle();
        }

        String fontName = "";
        switch (mFontName) {
            case 0:
                switch (mFontStyle) {
                    case 0:
                        fontName = "lato_bold.ttf";
                        break;
                    case 1:
                        fontName = "lato_italic.ttf";
                        break;
                    case 2:
                        fontName = "lato_light.ttf";
                        break;
                    case 3:
                        fontName = "lato_regular.ttf";
                        break;
                    case 4:
                        fontName = "lato_thin.ttf";
                        break;
                    default :
                        fontName = "lato_regular.ttf";
                        break;
                }
                break;
            case 1:
                switch (mFontStyle) {
                    case 0:
                        fontName = "notoSerif_bold.ttf";
                        break;
                    case 3:
                        fontName = "notoSerif_regular.ttf";
                        break;
                    default :
                        fontName = "notoSerif_regular.ttf";
                        break;
                }
                break;
        }

        try {
            setTypeface(Typeface.createFromAsset(context.getAssets(), Consts.FONT_PATH_PREFIX + fontName));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
Community
  • 1
  • 1
Kazuya
  • 145
  • 2
  • 9
  • 1
    http://stackoverflow.com/questions/14910131/edittext-loses-focus-on-click/14945890#14945890 – guillaumegoui Feb 02 '16 at 10:15
  • do a Durya!!! on Goui's Answer. – Aizen Feb 02 '16 at 10:17
  • Tried that already. It doesn't change anything. – Kazuya Feb 02 '16 at 10:40
  • I see your EditText is a custom one. May you post the code of it ? Maybe the solution is there. Have you tried with a standard EditText ? – guillaumegoui Feb 02 '16 at 13:31
  • Yeah i tried this too. But it's just changing the font. I edited my question in case it can cause my problem that i didn't see, even if i can image why (but Android made stranger things too...) – Kazuya Feb 02 '16 at 13:41
  • 1
    Avoid using listview or recyclerview in scrollingview. Your first LinearLayout has no orientation. Try to replace the NestedScrollView with a RelativeLayout, remove the first LinearLayout and put the RecyclerView on top of the second LinearLayout which would be on the bottom of the RelativeLayout. – guillaumegoui Feb 02 '16 at 15:39
  • I'll try that. Thank you. – Kazuya Feb 02 '16 at 16:04
  • OK. Iv'e tested several solutions and ended up getting rid of the RecyclerView. I add my items to a linear layout. I find it very ugly but it's the only way I could make it work. now I have the inverse problem... The EditText takes the focus when I update the entire view and make it appear (after logging in) \o/. I saw this already somewhere, I should figure it out. Anyway thank you for your help @Goui :) – Kazuya Feb 04 '16 at 14:14
  • if you don't want the EditText to get the focus just call clearFocus() on it. – guillaumegoui Feb 09 '16 at 08:45

0 Answers0