EditText cursor and pointer color for below android 5.0

孤街醉人 提交于 2019-12-08 15:40:53

问题


I am trying to change EditText cursor pointer color (from primary color blue to white), but no solution is working for my project. I have tried to develop 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 overridden by my value. I have two options to encounter this problem :-
1. find the attribute which is controlling that color value.
2. change 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);


回答1:


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.




回答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" />




回答3:


Use this attribute in your EditText:

android:textCursorDrawable="@null"



回答4:


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.




回答5:


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" />



回答6:


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.




回答7:


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)

  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:




回答8:


<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.



来源:https://stackoverflow.com/questions/41240065/edittext-cursor-and-pointer-color-for-below-android-5-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!