Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText cursor and pointer color for below android 5.0

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

like image 812
Harish Gyanani Avatar asked Dec 20 '16 10:12

Harish Gyanani


3 Answers

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.

like image 59
JuLes Avatar answered Nov 16 '22 23:11

JuLes


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

like image 34
Rajakumar Avatar answered Nov 16 '22 22:11

Rajakumar


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" />
like image 2
Malik Abu Qaoud Avatar answered Nov 16 '22 22:11

Malik Abu Qaoud