Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't customize the appearance of EditText select handle / anchor on landscape

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:

enter image description here

like image 983
gmmo Avatar asked Jun 02 '17 22:06

gmmo


2 Answers

 <EditText
    android:imeOptions="flagNoFullscreen"
 />

You can use android:imeOptions to tell the system not to display editing UI in fullscreen. enter image description here

like image 70
Tin Tran Avatar answered Sep 23 '22 14:09

Tin Tran


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>
like image 40
Arun Shankar Avatar answered Sep 24 '22 14:09

Arun Shankar