Hot questions for Using Android EditText in android textinputedittext

Top Java Programmings / Android EditText / android textinputedittext

Question:

I'm hoping you would help me to get to the problem I'm facing right now.

I created a custom search view using a TextInputEditText, it's really simple actually and works fine on all the Android version with the exception of 5.1(Lollipop), the EditText never hide the hint when write or even show the cursor, also the text is not visible.

I t I have tried everything I know, but I can't figure what could it be.

Thank you so much for your time.

This is my custom class view :

public class CustomSearchView extends RelativeLayout {

@BindView(R.id.csvIcon) ImageView csvIcon;
public @BindView(R.id.csvEditText) TextInputEditText editText;

public CustomSearchView(Context context) {
    super(context);
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.custom_search_view, this, true);
    ButterKnife.bind(this);
}

public CustomSearchView(Context context, AttributeSet attrs) {
    super(context, attrs);
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.custom_search_view, this, true);
    ButterKnife.bind(this);
    TypedArray typedArray = context.getTheme().obtainStyledAttributes(
            attrs,
            R.styleable.CustomSearchView,
            0, 0);
    String hintText = typedArray.getString(R.styleable.CustomSearchView_hintText);
    try {
        int max = typedArray.getInteger(R.styleable.CustomSearchView_csv_length, 17);
        editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(max)});
        editText.setInputType(typedArray.getInt(R.styleable.CustomSearchView_android_inputType, InputType.TYPE_CLASS_TEXT));
        editText.setHint(hintText);
        csvIcon.setImageDrawable(typedArray.getDrawable(R.styleable.CustomSearchView_csv_icon));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

This are the attribute specification :

<declare-styleable name="CustomSearchView">
    <attr name="csv_icon" format="reference"/>
    <attr name="hintText" format="string"/>
    <attr name="csv_length" format="integer"/>
    <attr name="android:inputType"/>
    <attr name="android:digits"/>
</declare-styleable>

This is my XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/charcoal_grey_two">

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

        <ImageView
            android:id="@+id/csvIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:src="@drawable/icon_search"/>

        <android.support.design.widget.TextInputEditText
            android:id="@+id/csvEditText"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_gravity="center"
            android:cursorVisible="true"
            android:focusableInTouchMode="true"
            android:layout_marginLeft="10dp"
            android:imeOptions="actionSearch"
            android:inputType="textAutoComplete"
            android:textSize="20dp"
            android:textColorHint="@color/slate_grey"
            android:textColor="@color/slate_grey"
            android:background="@android:color/transparent"/>

    </LinearLayout>

</RelativeLayout>

And this is my implementation in a fragment :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="@color/gunmetal">

<mx.com.segurosbancomer.gpsajustadores.view.custom.TopBarLayout
    android:id="@+id/topBar"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    app:tb_color="@color/gunmetal"
    app:tb_text="@string/bf_title"
    app:tb_text_color="@color/white"
    app:tb_btnleft_visibility="0"
    app:tb_btnright_visibility="8"
    app:tb_btnleft="@drawable/icon_prev_white"
    app:tb_btnright="@drawable/icon_dot_gray"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:orientation="vertical">

    <mx.com.segurosbancomer.gpsajustadores.view.custom.CustomTextView
        android:id="@+id/tituloPoliza"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/bf_search"
        android:textColor="@color/white"
        android:textSize="18dp"
        android:layout_margin="10dp"
        app:ctv_customFont="stagsansbook.otf" />


    <mx.com.segurosbancomer.gpsajustadores.view.custom.CustomSearchView
        android:id="@+id/searchPoliza"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_margin="10dp"
        android:cursorVisible="true"
        android:focusableInTouchMode="true"
        android:inputType="textAutoComplete"
        android:digits="ABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890"
        app:csv_length="30"
        app:csv_icon="@drawable/icon_search"
        app:hintText="@string/bf_poliza" />

    <mx.com.segurosbancomer.gpsajustadores.view.custom.CustomSearchView
        android:id="@+id/searchInciso"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_margin="10dp"
        android:cursorVisible="true"
        android:focusableInTouchMode="true"
        android:inputType="textAutoComplete"
        android:digits="ABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890"
        app:csv_length="30"
        app:csv_icon="@drawable/icon_search"
        app:hintText="@string/bf_inciso" />


    <View
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:background="@color/charcoal_grey_two"/>

    <mx.com.segurosbancomer.gpsajustadores.view.custom.CustomTextView
        android:id="@+id/tituloSerie"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/bf_search_serie"
        android:textColor="@color/white"
        android:textSize="18dp"
        android:layout_margin="10dp"
        app:ctv_customFont="stagsansbook.otf" />

    <mx.com.segurosbancomer.gpsajustadores.view.custom.CustomSearchView
        android:id="@+id/searchSerie"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_margin="10dp"
        android:cursorVisible="true"
        android:focusableInTouchMode="true"
        android:inputType="textAutoComplete"
        android:digits="ABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890"
        app:csv_length="17"
        app:csv_icon="@drawable/icon_search"
        app:hintText="@string/bf_serie"/>


</LinearLayout>

</LinearLayout>

Answer:

Finally I find the solution. If some else have a similar problem just like me, the issue was resolved by removing this line from the AndroidManifest.xml

android:hardwareAccelerated="false"

This line allow to the fragment and all the components to work perfectly.

Question:

so I have editText with closes and success icons and maximum password 6, the first thing I do is if the password is not maximum 6, the icon remains closes like I drew, if the password is maximum 6 then the closes icon changes to the success icon, my problem how to change the closes icon to a success icon if the maximum 6 conditions are met

this is my code

 <com.google.android.material.textfield.TextInputLayout
    android:id="@+id/textInputPassword"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="20dp"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="20dp"
    android:hint="Password"
    app:counterEnabled="true"
    app:counterMaxLength="6"
    app:endIconMode="clear_text"
    app:endIconDrawable="@drawable/ic_canceles"
    app:errorEnabled="true"
    app:endIconTint="#DF0000"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputEmail">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/editTextPassword"
        android:layout_width="match_parent"
        android:maxLines="1"
        android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>

Answer:

Try this in your on create method

TextInputLayout editText = findViewById(R.id.textInputPassword);


                editText.getEditText().addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if (s.toString().length() > 6) {
                            editText.setEndIconDrawable(R.drawable.success);
                        } else {
                            editText.setEndIconDrawable(R.drawable.ic_canceles);
                        }
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });