10

I am trying to change EditText cursor pointer color (from the primary color blue to white), but no solution is working for my project. I have tried to develop a demo project, where the same code is working fine. This code is working fine for >=android 5.0

I do not know, which attribute is overwritten by my value. I have two options to encounter this problem :

  1. Find the attribute which is controlling that color value.
  2. Change the color value by java code when page loads (in onViewCreated).

Please suggest how to resolve this issue.

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="my_text_layout_style">
        
        <item name="android:textColor">#FFF</item>
        <item name="android:textColorHint">#FFF</item>
      
        <item name="colorAccent">#FFF</item>
        <item name="colorControlNormal">#FFF</item>
        <item name="colorControlActivated">#FFF</item>

        <item name="colorControlHighlight">#FFF</item>

    </style>

    

    <style name="my_edit_text_style">
        <item name="colorAccent">#FFF</item>
        <item name="android:editTextStyle">@style/MyEditTextStyle</item>
    </style>

    <style name="MyEditTextStyle" parent="Widget.AppCompat.EditText" />
</resources>

layout.xml

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/base_disable_btn_bg_color"
    android:orientation="vertical">

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:hint="just a hint"
        app:hintTextAppearance="@style/my_text_layout_style"
        android:theme="@style/my_text_layout_style"
        android:layout_height="wrap_content">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:theme="@style/my_edit_text_style"
            android:layout_height="wrap_content" />

    </android.support.design.widget.TextInputLayout>
</LinearLayout>

Tried to add view programmatically but no success

AppLinearLayout appLinearLayout = (AppLinearLayout) view.findViewById(R.id.some_id);
EditText editText = new EditText(new ContextThemeWrapper(getContext(), R.style.AppTheme_Cursor));
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
editText.setLayoutParams(params);
appLinearLayout.addView(editText);

screenshot

Mete
  • 2,805
  • 1
  • 28
  • 38
Harish Gyanani
  • 1,366
  • 2
  • 22
  • 43

8 Answers8

3

Try this link. For me, it worked. And, you can find and modify the drawables on your SDK here: Android\sdk\platforms\YOUR_ANDROID_VERSION\data\res at drawables-*dpi.

You can copy and modify their color/form as your wish.

Community
  • 1
  • 1
JuLes
  • 251
  • 1
  • 14
2

Try this solution:

Use this attribute in EditText: android:textCursorDrawable="@drawable/cursor_color"

In drawable, create : cursor_color.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

<size android:width="1dp" />

<solid android:color="#c8c8c8" />

Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Rajakumar
  • 907
  • 1
  • 7
  • 17
2

I used this and it's work with me correctly.

<style name="edittext" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorControlNormal">@color/gray</item>
    <item name="colorControlActivated">@color/colorAccent</item>
    <item name="colorControlHighlight">@color/colorAccent</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:textColorHint">@color/gray</item>
</style>

<EditText
            android:id="@+id/email"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="30dp"
            android:gravity="center"
            android:hint="@string/email_address"
            android:inputType="textEmailAddress"
            android:padding="@dimen/padding_16"
            android:textColor="@color/white"
            android:textColorHint="@color/login_color"
            android:textCursorDrawable="@color/mdtp_white"
            android:theme="@style/edit" />
Malik Abu Qaoud
  • 208
  • 1
  • 9
1

Use this attribute in your EditText:

android:textCursorDrawable="@null"
fedorqui
  • 275,237
  • 103
  • 548
  • 598
1

You can create your own style/theme for only this EditText and change the ColorAccent :

<style name="EditTextColorCustom" parent="@style/AppBaseTheme">
    <!-- Customize your theme here. -->
    <item name="colorAccent">@color/colorAccent</item>
</style>

Use this same in style in your values-21 folder.

Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
1

Make a new drawable.

Here, cursor.xml:

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

and use it in EditText like this:

 <EditText
      android:id="@+id/ed"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@drawable/roundedcornerwhite"
      android:hint="edit text"
      android:paddingBottom="10dp"
      android:paddingRight="10dp"
      android:paddingTop="10dp"
      android:maxLines="1"
      android:singleLine="true"
      android:imeOptions="actionNext"
      android:textColor="@color/colorPrimaryText"
      android:textColorHint="@color/hintcolor"
      android:textCursorDrawable="@drawable/cursor"
      android:textSize="16sp" />

UPDATED:

just make a new theme like -

<style name="themex" parent="Theme.AppCompat.Light.NoActionBar">

        <item name="colorPrimaryDark">#666666</item>
        <item name="android:listDivider">@color/white_pressed</item>
        <item name="colorPrimary">@color/colorIcons</item>
        <item name="android:textColor">#b6b6b6</item>
        <item name="android:windowDisablePreview">true</item>
        <item name="colorControlActivated">@color/olagreen</item>
        <item name="android:windowAnimationStyle">@null</item>

    </style>

here colorcontrolactivated will be the answer to ur question. And add this theme in to your activity and remove the the style u gave to edittext in ur layout-

<activity
            android:name="youractivityname"
            android:parentActivityName="com.example.AboutUs"
            android:screenOrientation="portrait"
            android:theme="@style/themex"/>

UPDATE:

If not working for then, just check whether your fragment is inside the activity and you gave the correct theme to that activity. The fragment will also have this theme. If its not working then try this -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:id="@+id/mainlayout"
        style="@style/themex"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorIcons"
        android:orientation="vertical"
        android:weightSum="2">

    </RelativeLayout> 

i hope this will help you.

Aman Verma
  • 3,155
  • 7
  • 30
  • 60
  • It is only changing cursor color. Pointer color is same as before(dark blue#192B3C shown in the image). – Harish Gyanani Jan 03 '17 at 09:17
  • dark blue is you accent color change it accordingly – Aman Verma Jan 03 '17 at 09:33
  • I want to change accent color of edittext. I do not want to change accent color for whole app. – Harish Gyanani Jan 03 '17 at 09:39
  • 1
    create a new theme or style and add this style to the particular activity in manifest – Aman Verma Jan 03 '17 at 09:56
  • Please check I have already done that. Let me know if you find any problem in my code. – Harish Gyanani Jan 03 '17 at 10:01
  • that is working but I am using fragments. It is used in login fragment.Is there any code available to change theme for specific fragment? I have seen a answer which calls `recreate` method. Should I use that approach? – Harish Gyanani Jan 03 '17 at 10:25
  • if your fragment/s are inside any activity then it will use the same theme for the fragment and if thats not working then define the theme for the particular fragment by defining it in the rootview of the layout. – Aman Verma Jan 03 '17 at 11:54
  • check the UPDATE 2 – Aman Verma Jan 03 '17 at 12:16
  • which device you are trying, and could you please update some more code... like are you using viewpager and pageradapter or something for fragment inside activity – Aman Verma Jan 03 '17 at 16:34
  • I am testing it on 4.4 android genymotion. On 5.1 android genymotion is showing correct (white pointer what I am expecting). I have given the layout file above, it is inflated in a fragment. – Harish Gyanani Jan 04 '17 at 05:49
0

I've combined few answers on SO and found the best way to do this:

  1. Look for the original drawable in your android sdk folder (\android-sdk\platforms\android-23\android.jar\res\drawable-hdpi-v4) enter image description here

  2. Edit those images with an editor and put them in your drawable folder.

  3. Add following lines to your style:

    <item name="android:textSelectHandle">@drawable/my_drawable_handle</item>
    <item name="android:textSelectHandleLeft">@drawable/my_drawable_handle_left</item>
    <item name="android:textSelectHandleRight">@drawable/my_drawable_handle_right</item>
    

The result:

enter image description here

Manza
  • 3,427
  • 4
  • 36
  • 57
0
<style name="my_edit_text_style">
    <item name="colorAccent">#FFF</item>
    <item name="android:editTextStyle">@style/MyEditTextStyle</item>
    <item name="colorAccent">#FFF</item>
    <item name="colorControlNormal">#FFF</item>
    <item name="colorControlActivated">#FFF</item>
</style>

This may help you.

kwaghela
  • 126
  • 7