I am having a hard time customizing the EditText select handle. I am following this thread:
How to change color / appearance of EditText select handle / anchor?
It looks like pretty straight forward. Yet, I can't get it to work on landscape. Can anyone spot what I am doing wrong? I pretty much pasted the same code on a test activity but the anchor handles' are always the same. I tried using styles as suggested and programmatically. Still I always get the same default blue anchors :(
I am on Nougat not sure if makes any difference.
Test activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.MyCustomTheme);
setContentView(R.layout.activity_main);
final EditText editText = (EditText) findViewById(R.id.edit1);
// tried programatically too and no success
try {
final Field fEditor = TextView.class.getDeclaredField("mEditor");
fEditor.setAccessible(true);
final Object editor = fEditor.get(editText);
final Field fSelectHandleLeft = editor.getClass().getDeclaredField("mSelectHandleLeft");
final Field fSelectHandleRight =
editor.getClass().getDeclaredField("mSelectHandleRight");
final Field fSelectHandleCenter =
editor.getClass().getDeclaredField("mSelectHandleCenter");
fSelectHandleLeft.setAccessible(true);
fSelectHandleRight.setAccessible(true);
fSelectHandleCenter.setAccessible(true);
fSelectHandleLeft.set(editor, ContextCompat.getDrawable(this, R.drawable.small_rect));
fSelectHandleRight.set(editor, ContextCompat.getDrawable(this, R.drawable.small_rect));
fSelectHandleCenter.set(editor, ContextCompat.getDrawable(this, R.drawable.small_rect));
} catch (final Exception e) {
Log.d("CUSTOM_ANCHORS", e.toString());
}
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Hello World"
android:textSize="20sp" />
</LinearLayout>
My styles:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="MyCustomTheme" parent="@style/AppTheme">
<item name="android:textSelectHandle">@drawable/small_rect</item>
<item name="android:textSelectHandleLeft">@drawable/small_rect</item>
<item name="android:textSelectHandleRight">@drawable/small_rect</item>
</style>
the drawable (small_rect.xml)
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="20dp"
android:height="20dp" />
<gradient
android:angle="90"
android:centerColor="#D6D6D6"
android:endColor="#4B6CD6"
android:startColor="#6586F0" />
<corners android:radius="0dp" />
</shape>
the result:
<EditText
android:imeOptions="flagNoFullscreen"
/>
You can use android:imeOptions
to tell the system not to display editing UI in fullscreen.
The below code works perfectly on Nougat
<EditText
android:layout_width="match_parent"
style="@style/MyCustomTheme"
android:text="@string/app_name"
android:layout_height="wrap_content" />
<style name="MyCustomTheme" parent="@style/AppTheme">
<item name="android:textSelectHandle">@android:drawable/arrow_down_float</item>
<item name="android:textSelectHandleLeft">@android:drawable/arrow_down_float</item>
<item name="android:textSelectHandleRight">@android:drawable/arrow_down_float</item>
</style>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With