Reputation: 3746
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?
Upvotes: 44
Views: 31925
Reputation: 424
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
}
Upvotes: 0
Reputation: 553
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);
}
}
}
Upvotes: 2
Reputation: 3632
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
Upvotes: -2
Reputation: 38121
Update 2019: There is no public API to set the cursor drawable. See https://stackoverflow.com/a/57555148/253468 for API available on 29 and above, before that conditionally you'll need to use reflection as described below.
Before API 29 you can set it programmatically by using reflection. The field mCursorDrawableRes
hasn't changed so this should work on all devices, unless a manufacturer changed something or it is later changed.
EditText yourEditText = new EditText(context);
...
try {
// https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564
Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
f.setAccessible(true);
f.set(yourEditText, R.drawable.cursor);
} catch (Exception ignored) {
}
Define a cursor drawable in your app:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ff000000" />
<size android:width="1dp" />
</shape>
You can also set the cursor color with the following method:
public static void setCursorDrawableColor(EditText editText, int color) {
try {
Field fCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
fCursorDrawableRes.setAccessible(true);
int mCursorDrawableRes = fCursorDrawableRes.getInt(editText);
Field fEditor = TextView.class.getDeclaredField("mEditor");
fEditor.setAccessible(true);
Object editor = fEditor.get(editText);
Class<?> clazz = editor.getClass();
Field fCursorDrawable = clazz.getDeclaredField("mCursorDrawable");
fCursorDrawable.setAccessible(true);
Drawable[] drawables = new Drawable[2];
drawables[0] = editText.getContext().getResources().getDrawable(mCursorDrawableRes);
drawables[1] = editText.getContext().getResources().getDrawable(mCursorDrawableRes);
drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN);
drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN);
fCursorDrawable.set(editor, drawables);
} catch (Throwable ignored) {
}
}
Upvotes: 70
Reputation: 178
Using reflection for the cursor drawable is prohibited from Android Q.
Instead, there is a new setTextCursorDrawable API and we can use this.
Upvotes: 10
Reputation: 7651
If you use the AppCompat library and your Activity extends AppCompatActivity, you can set the color of the text cursor (for all EditTexts) using an XML style of colorAccent:
<style name="AppTheme" parent="@style/Theme.AppCompat">
<item name="colorAccent">#FF4081</item>
</style>
This works on Android 5+ and is backwards compatible with older Android versions from Android 4.4 (API 19) to Android 4.0 (API 14). Note: If you're using an AutoCompleteTextView, ensure that any custom subclass of it extends AppCompatAutoCompleteTextView, otherwise the text cursor styling will not work.
See also this guide, for how to apply the color accent (tint) to Dialog buttons: Android v21 Theme.Appcompat color accent is ignored, no padding on dialogs
Upvotes: 0
Reputation: 11
the answer is ---------------------> It's unattainable.
Actually, I also met this problem in my work, but after checking google's doc and source code, I fond that you cannot set this attribute in java code.
In TextView
class you can find the code below
case com.android.internal.R.styleable.TextView_textCursorDrawable:
mCursorDrawableRes = a.getResourceId(attr, 0);
break;
but the method textCursorDrawable()
is only existed in R.attr
, if you wanna set this attribute, you can only call the construct method below by including a editText in XML file.
public EditText(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.editTextStyle);
}
Upvotes: 1