Set EditText cursor color

后端 未结 24 1403
醉话见心
醉话见心 2020-11-22 10:57

I am having this issue where I am using the Android\'s Holo theme on a tablet project. However, I have a fragment on screen which has a white background. I am adding an

相关标签:
24条回答
  • 2020-11-22 11:48

    There is a new way to change cursor color in latest Appcompact v21
    Just change colorAccent in style like this:

     <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette-->
    
        <!-- colorPrimary is used for the default action bar background -->
        <item name="colorPrimary">#088FC9</item>
    
        <!-- colorPrimaryDark is used for the status bar -->
        <item name="colorPrimaryDark">#088FC9</item>
    
        <!-- colorAccent is used as the default value for colorControlActivated
             which is used to tint widgets -->
        <!-- THIS IS WHAT YOU'RE LOOKING FOR -->
        <item name="colorAccent">#0091BC</item> 
    </style>
    

    Then apply this style on your app theme or activities.

    Update: this way only works on API 21+
    Update 2: I'm not sure the minimum android version that it can work.
    Tested by android version:

    2.3.7 - didn't work
    4.4.4 - worked
    5.0 - worked
    5.1 - worked
    
    0 讨论(0)
  • 2020-11-22 11:48

    Wow I am real late to this party but it has had activity 17 days ago It would seam we need to consider posting what version of Android we are using for an answer so as of now this answer works with Android 2.1 and above Go to RES/VALUES/STYLES and add the lines of code below and your cursor will be black

        <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <!--<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">-->
        <!-- Customize your theme here. -->
        <item name="colorControlActivated">@color/color_Black</item>
        <!--Sets COLOR for the Cursor in EditText  -->
    </style>
    

    You will need a this line of code in your RES/COLOR folder

    <color name="color_Black">#000000</color>
    

    Why post this late ? It might be nice to consider some form of categories for the many headed monster Android has become!

    0 讨论(0)
  • 2020-11-22 11:48

    Its even easier than that.

    <style name="MyTextStyle">
        <item name="android:textCursorDrawable">#000000</item>
    </style>
    

    This works in ICS and beyond. I haven't tested it in other versions.

    0 讨论(0)
  • 2020-11-22 11:49

    For me I modified both the AppTheme and a value colors.xml

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorControlNormal">@color/yellow</item>
        <item name="colorAccent">@color/yellow</item>
    </style>
    

    Here is the colors.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="yellow">#B7EC2A</color>
    </resources>
    

    I took out the android:textCursorDrawable attribute to @null that I placed inside the editText style. When I tried using this, the colors would not change.

    0 讨论(0)
  • 2020-11-22 11:49

    If using style and implement

    colorControlActivate

    replace its value other that color/white.

    0 讨论(0)
  • 2020-11-22 11:50

    Late to the party,Here's is my answer,

    This is for the people who are not looking to change the colorAccent in their parent theme,but wants to change EditText attributes!

    This answer demos how to change ......

    1. Bottom line color
    2. Cursor color
    3. Cursor pointer color (I used my custom image).......... of EditText using style applied to the Activity theme.

    <android.support.v7.widget.AppCompatEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hey" />
    

    Example:

    <style name="AppTheme.EditText" parent="@style/Widget.AppCompat.EditText">
        <item name="android:textColor">@color/white</item>
        <item name="android:textColorHint">#8AFFFFFF</item>
        <item name="android:background">@drawable/edit_text_background</item> // background (bottom line at this case)
        <item name="android:textCursorDrawable">@color/white</item>  // Cursor
        <item name="android:textSelectHandle">@drawable/my_white_icon</item> // For pointer normal state and copy text state
        <item name="android:textSelectHandleLeft">@drawable/my_white_icon</item>
        <item name="android:textSelectHandleRight">@drawable/my_white_icon</item>
    </style>
    

    Now create a drawable(edit_text_background) add a resource xml for the background!You can customize as you want!

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:bottom="0dp"
            android:left="-3dp"   
            android:right="-3dp"
            android:top="-3dp">
    
            <shape android:shape="rectangle">
                <stroke
                    android:width="1dp"
                    android:color="@color/white"/>
            </shape>
        </item>
        </layer-list>
    

    Now as you did set this style in your Activity theme.

    Example :

    In your Activity you have a theme,set this custom editText theme to that.

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Your Theme data -->
        <item name="editTextStyle">@style/AppTheme.EditText</item> // inculude this
    </style>
    
    0 讨论(0)
提交回复
热议问题