User
User

Reputation: 4211

EditText is not working in CardView

I'm trying to put an EditText or AppCompatEditText in a CardView but EditText is not getting focus. Unable to find what I'm missing in the layout.

        <android.support.v7.widget.CardView
            android:id="@+id/card_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="14dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="14dp"
            app:cardBackgroundColor="@color/color_secondary_ultra_light">
            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/edit_username"
                android:padding="10dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@null"
                android:drawablePadding="4dp"
                android:drawableTint="@android:color/darker_gray"
                android:drawableRight="@mipmap/ic_person_black_24dp"
                android:hint="Username"
                android:inputType="textWebEmailAddress"
                android:maxLines="1"
                android:textColorHint="@android:color/darker_gray"
                android:textSize="18sp"
                android:focusable="true"
                android:clickable="true"/>
        </android.support.v7.widget.CardView>

Solutions posted here is not working in my case.

Upvotes: 2

Views: 4940

Answers (5)

User
User

Reputation: 4211

I've put android:descendantFocusability="beforeDescendants" property in CardView and now it is working properly.

Thanks for everybody's efforts.

Upvotes: 1

Prasad Pawar
Prasad Pawar

Reputation: 1701

Though the solutions here should have worked, you can use a hack to save time. In your code:

        EditText et = (EditText)findViewById(R.id.editTextId); 
        et.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                    ((EditText) view).requestFocus();
                    InputMethodManager imm = (InputMethodManager) 
                                       getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
             }
        });

But before using this, please make sure that your EditText is not being overlapped by some other views. That might be the issue which doesn't need this workaround.

Upvotes: 0

Surbhi Sharma
Surbhi Sharma

Reputation: 49

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="10dip"
    android:paddingRight="10dip"
    >
 <EditText
        android:id="@+id/Edit"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inputType="textCapSentences|textMultiLine"
        android:overScrollMode="always"
        android:scrollbarStyle="insideInset"
        android:scrollbars="vertical"

        />
</LinearLayout>

</android.support.v7.widget.CardView>

Upvotes: 0

Surbhi Sharma
Surbhi Sharma

Reputation: 49

Use Edit text instead of AppCompatEditText.

Upvotes: 0

Nishant Pardamwar
Nishant Pardamwar

Reputation: 1485

dont use AppCompatEditText use EditText instead it will use AppCompatEditText by default

<android.support.v7.widget.CardView
            android:id="@+id/card_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="14dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="14dp"
            app:cardBackgroundColor="@color/color_secondary_ultra_light">
            <EditText
                android:id="@+id/edit_username"
                android:padding="10dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@null"
                android:drawablePadding="4dp"
                android:drawableTint="@android:color/darker_gray"
                android:drawableRight="@mipmap/ic_person_black_24dp"
                android:hint="Username"
                android:inputType="textWebEmailAddress"
                android:maxLines="1"
                android:textColorHint="@android:color/darker_gray"
                android:textSize="18sp"
                android:focusable="true"
                android:clickable="true"/>
        </android.support.v7.widget.CardView>

you can check in the AppCompatEditText documentation that EditText uses it by default.

This will automatically be used when you use EditText in your layouts. You should only need to manually use this class when writing custom views.

Upvotes: 2

Related Questions