Skip to content Skip to sidebar Skip to footer

How To Change Color Of The Bubble (under Cursor) On Edittext (programmatically)?

I have EditText and want to change color PROGRAMMATICALLY on code. To change color of cursor I use this code. But how to change color of the circle on EditView PROGRAMMATICALLY on

Solution 1:

You will need to use reflection to tint the select handles (bubbles). I wrote the following class this morning:

Example usage:

try {
  EditTextTint.applyColor(editText, Color.CYAN);
} catch (EditTextTint.EditTextTintError e) {
  e.printStackTrace();
}

EditTextTint.java:

import android.content.res.Resources;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.widget.EditText;
import android.widget.TextView;
import java.lang.reflect.Field;

/**
 * Tint the cursor and select handles of an {@link EditText} programmatically.
 */publicclassEditTextTint {

  /**
   * Set the cursor and handle colors for an {@link EditText} programmatically.
   *
   * @param editText
   *     The {@link EditText} to tint
   * @param color
   *     The color to apply for the cursor and select handles
   * @throws EditTextTintError
   *     If an error occured while attempting to tint the view.
   */publicstaticvoidapplyColor(@NonNull EditText editText, @ColorIntint color)throws EditTextTintError {
    EditTextTinteditTextTint=newBuilder(editText)
        .setCursorColor(color)
        .setSelectHandleLeftColor(color)
        .setSelectHandleRightColor(color)
        .setSelectHandleMiddleColor(color)
        .build();
    editTextTint.apply();
  }

  privatefinal EditText editText;
  privatefinal Integer cursorColor;
  privatefinal Integer selectHandleLeftColor;
  privatefinal Integer selectHandleRightColor;
  privatefinal Integer selectHandleMiddleColor;

  privateEditTextTint(Builder builder) {
    editText = builder.editText;
    cursorColor = builder.cursorColor;
    selectHandleLeftColor = builder.selectHandleLeftColor;
    selectHandleRightColor = builder.selectHandleRightColor;
    selectHandleMiddleColor = builder.selectHandleMiddleColor;
  }

  /**
   * Sets the color for the cursor and handles on the {@link EditText editText}.
   *
   * @throws EditTextTintError
   *     if an error occurs while tinting the view.
   */publicvoidapply()throws EditTextTintError {
    try {
      Resourcesres= editText.getContext().getResources();

      // Get the editorFieldfield= TextView.class.getDeclaredField("mEditor");
      field.setAccessible(true);
      Objecteditor= field.get(editText);

      if (cursorColor != null) {
        // Get the cursor drawable, tint it, and set it on the TextView Editor
        field = TextView.class.getDeclaredField("mCursorDrawableRes");
        field.setAccessible(true);
        intcursorDrawableRes= field.getInt(editText);
        DrawablecursorDrawable= res.getDrawable(cursorDrawableRes).mutate();
        cursorDrawable.setColorFilter(cursorColor, PorterDuff.Mode.SRC_IN);
        Drawable[] drawables = {cursorDrawable, cursorDrawable};
        field = editor.getClass().getDeclaredField("mCursorDrawable");
        field.setAccessible(true);
        field.set(editor, drawables);
      }

      String[] resFieldNames = {"mTextSelectHandleLeftRes", "mTextSelectHandleRightRes", "mTextSelectHandleRes"};
      String[] drawableFieldNames = {"mSelectHandleLeft", "mSelectHandleRight", "mSelectHandleCenter"};
      Integer[] colors = {selectHandleLeftColor, selectHandleRightColor, selectHandleMiddleColor};

      for (inti=0; i < resFieldNames.length; i++) {
        Integercolor= colors[i];
        if (color == null) {
          continue;
        }

        StringresFieldName= resFieldNames[i];
        StringdrawableFieldName= drawableFieldNames[i];

        field = TextView.class.getDeclaredField(resFieldName);
        field.setAccessible(true);
        intselectHandleRes= field.getInt(editText);

        DrawableselectHandleDrawable= res.getDrawable(selectHandleRes).mutate();
        selectHandleDrawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);

        field = editor.getClass().getDeclaredField(drawableFieldName);
        field.setAccessible(true);
        field.set(editor, selectHandleDrawable);
      }
    } catch (Exception e) {
      thrownewEditTextTintError("Error applying tint to " + editText, e);
    }
  }

  publicstaticclassBuilder {

    final EditText editText;
    Integer cursorColor;
    Integer selectHandleLeftColor;
    Integer selectHandleRightColor;
    Integer selectHandleMiddleColor;

    publicBuilder(@NonNull EditText editText) {
      this.editText = editText;
    }

    public Builder setCursorColor(@ColorIntint cursorColor) {
      this.cursorColor = cursorColor;
      returnthis;
    }

    public Builder setSelectHandleLeftColor(@ColorIntint selectHandleLeftColor) {
      this.selectHandleLeftColor = selectHandleLeftColor;
      returnthis;
    }

    public Builder setSelectHandleRightColor(@ColorIntint selectHandleRightColor) {
      this.selectHandleRightColor = selectHandleRightColor;
      returnthis;
    }

    public Builder setSelectHandleMiddleColor(@ColorIntint selectHandleMiddleColor) {
      this.selectHandleMiddleColor = selectHandleMiddleColor;
      returnthis;
    }

    public EditTextTint build() {
      returnnewEditTextTint(this);
    }

  }

  publicstaticclassEditTextTintErrorextendsException {

    publicEditTextTintError(String message, Throwable cause) {
      super(message, cause);
    }
  }

}

Note: This should work from Jelly Bean to Nougat. However, since it uses reflection to get and set private fields this may break in future releases of Android or if a manufacturer has made changes to EditText.

Solution 2:

The below method works for all cursors bubble such as left, right and center. I mean,beside your request, It works for both left and right ones.

For example; Beside your request, It works for both left and right ones

You can change that method to only color the center handle by removing the left and right field names in the two arrays.

publicstaticvoidcolorHandles(TextView view, int color) {
  try {
    FieldeditorField= TextView.class.getDeclaredField("mEditor");
    if (!editorField.isAccessible()) {
      editorField.setAccessible(true);
    }

    Objecteditor= editorField.get(view);
    Class<?> editorClass = editor.getClass();

    String[] handleNames = {"mSelectHandleLeft", "mSelectHandleRight", "mSelectHandleCenter"};
    String[] resNames = {"mTextSelectHandleLeftRes", "mTextSelectHandleRightRes", "mTextSelectHandleRes"};

    for (inti=0; i < handleNames.length; i++) {
      FieldhandleField= editorClass.getDeclaredField(handleNames[i]);
      if (!handleField.isAccessible()) {
        handleField.setAccessible(true);
      }

      DrawablehandleDrawable= (Drawable) handleField.get(editor);

      if (handleDrawable == null) {
        FieldresField= TextView.class.getDeclaredField(resNames[i]);
        if (!resField.isAccessible()) {
          resField.setAccessible(true);
        }
        intresId= resField.getInt(view);
        handleDrawable = view.getResources().getDrawable(resId);
      }

      if (handleDrawable != null) {
        Drawabledrawable= handleDrawable.mutate();
        drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        handleField.set(editor, drawable);
      }
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
}

Solution 3:

try this: change the value in values/colors.xml file

<colorname="colorAccent">#263238</color>

change this color code #263238 to your own color code, so that it will be applicable to all the project. Hope this will help you.

Post a Comment for "How To Change Color Of The Bubble (under Cursor) On Edittext (programmatically)?"