Change the color of the cursor of an EditText in Android across all the sdk

前端 未结 5 2359
半阙折子戏
半阙折子戏 2021-02-19 08:30

Want to change the android\'s edittext cursor color, that has to be worked on across all the devices

相关标签:
5条回答
  • 2021-02-19 08:59

    The way to get it across all platforms is as such.

    1 - Open your layout.xml in your layout folder. Find the edit text and set

    android:cursorVisible="true"
    

    this will set the cursor for devices lower that os version 11

    2 - Create your cursor_drawable.xml in the drawable folder

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

    3 - Create a folder layout-v11

    4 - Copy your layout.xml into the layout-v11

    5 - Find your edit text and set android:textCursorDrawable="@drawable/cursor_drawable"

    This will make a cursor be shown on all devices and OS.

    0 讨论(0)
  • 2021-02-19 09:03

    i had to use a drawable like this:

    mycursor.xml:

          <?xml version="1.0" encoding="utf-8"?>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
            <size android:width="1dp" />
            <solid android:color="@android:color/holo_blue_light"/> 
    <!--make sure its a solid tag not stroke or it wont work -->
        </shape>
    

    in my edit text i set the cursor drawable attributes like this:

                                <EditText
                                android:id="@+id/et_details"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:cursorVisible="true"
                               android:textCursorDrawable="@drawable/mycursor"  
                                />
    
    0 讨论(0)
  • 2021-02-19 09:21

    Create drawalble xml: edtcolor_cursor

        <?xml version="1.0" encoding="utf-8"?>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" >
            <size android:width="1dp" />
            <solid android:color="#FFFFFF"  />
        </shape>
    
    <EditText  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:cursorVisible="true"
        android:textCursorDrawable="@drawable/edtcolor_cursor"
        />
    
    0 讨论(0)
  • 2021-02-19 09:22

    Assign android:textCursorDrawable attribute to @null and set android:textColor as the cursor color.

    0 讨论(0)
  • 2021-02-19 09:22

    Refer to this link. You can try this to

    android:textCursorDrawable
    0 讨论(0)
提交回复
热议问题