set textCursorDrawable programmatically

Viewed 28603

If I add an EditText in XML I can set textCursorDrawable="@null":

<EditText
    android:id="@+id/txtE3Casecode4"
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="#C7C7C5"
    android:textCursorDrawable="@null"
    android:ems="10"
    android:inputType="number"
    android:maxLength="2"
    android:text="01"
    android:textColor="#000000" />

Now I draw an EditText in Java. I want set android:textCursorDrawable="@null".

LinearLayout.LayoutParams paramtext = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.FILL_PARENT,
    LinearLayout.LayoutParams.WRAP_CONTENT);
EditText txtOther = new EditText(this);
txtOther.setLayoutParams(paramtext);
txtOther.setBackgroundColor(Color.WHITE);
txtOther.setTextColor(Color.BLACK);
txtOther.setId(99999);
// txtOther.setCursorDrawable ?                                

How do set it?

7 Answers

This work for me

 public void setCursorDrawable(@DrawableRes int resId) {
        if (mEditText == null)
            return;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            mEditText.setTextCursorDrawable(resId);
            return;
        }
        try {
            @SuppressLint("SoonBlockedPrivateApi") Field field = TextView.class.getDeclaredField("mCursorDrawableRes");
            field.setAccessible(true);
            field.set(mEditText, resId);
        } catch (Throwable throwable) {
            Logger.e(TAG, throwable);
        }
    }
}

MySharedPreferences().color = Color in Hexadecimal -> #FF5733

Programmatically a method

    fun EditText.standardSimpleEditText(){
    this.apply {
        highlightColor = getColorWithAlpha(getValidateColorHex(MySharedPreferences().color), 0.2f)

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
            val drawable = ContextCompat.getDrawable(this.context, R.drawable.cursor) as Drawable
            val customDrawable = tintDrawable(drawable, MySharedPreferences().color)
            textCursorDrawable = customDrawable
        }
    }
}

Cursor.XML

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="@color/colorBlack" />
    <size android:width="1.8dp" />
</shape>

Validate color hexa

fun getValidateColorHex(color: Int = MySharedPreferences().color): Int{
    val hexColor = "#"+Integer.toHexString(color).substring(2)
    val validateColor = Color.parseColor(hexColor)
    return validateColor
}

You should use TextInputLayout you are in 2020 , it's working on all devices

<com.google.android.material.textfield.TextInputLayout
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="30dp"
        android:layout_marginTop="26dp"
        android:layout_marginEnd="30dp"
        android:theme="@style/TextInputLayoutAppearance"
        app:endIconMode="clear_text"
        app:hintEnabled="false">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/gallery_search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/inter_medium"
            android:hint="Hint here"
            android:inputType="text"
            android:cursorVisible="true"
            android:focusable="true"
            />

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

This is main don't forget to add this, you can change the color of the cursor from here

<style name="TextInputLayoutAppearance" parent="Widget.Design.TextInputLayout">
        <item name="colorControlNormal">@color/black</item>
        <item name="colorControlActivated">@color/black</item>
        <item name="colorControlHighlight">@color/blue</item>
    </style>

For more information about TextInputLayout visit here:- https://material.io/develop/android/components/text-fields

Related