question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Change the cursor color via styling

See original GitHub issue

Looks like the TextInputLayout and TextInputEditTextLayout are defaulting the cursor (and cursor bubble) color to primary color. I tested it by switching the global primary color of the app and it works. However, when I try to create a theme for that specific view and override the primary color, it doesn’t work.

<style name="Widget.AppTheme.TextInputEditText.Dense" parent="Widget.MaterialComponents.TextInputEditText.FilledBox.Dense">
        <item name="colorPrimary">@color/orange</item>
</style>

        <com.google.android.material.textfield.TextInputLayout
            app:hintEnabled="false"
            app:hintAnimationEnabled="false"
            android:id="@+id/f_login_email_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_default"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/f_login_subheader">

            <com.google.android.material.textfield.TextInputEditText
                style="@style/Widget.AppTheme.TextInputEditText.Dense"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="12dp"
                android:ems="10"
                android:textSize="@dimen/text_size_text_entry"
                android:hint="@string/login_email_hint"
                android:inputType="textEmailAddress" />
        </com.google.android.material.textfield.TextInputLayout>

Could somebody help with this?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:22 (6 by maintainers)

github_iconTop GitHub Comments

58reactions
erick-aguero22commented, Apr 15, 2020

Hi everyone I share my way of setting the OutlinedBox style.

First of all, I am using version 1.1.0

implementation 'com.google.android.material:material:1.1.0'

Just like you mentioned @dsn5ft , I create a style by referencing the style TextInputEditText.OutlinedBox

<style name="exampleCursor" parent="ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox">
    <item name="colorControlActivated">@color/green</item>
</style>

In my case I use a style for my TextInputLayout, I add android:theme with the created style

<style name="TextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
   <item name="android:background">@android:color/transparent</item>
    <item name="android:textColorHint">@color/colorAccent</item>
    <item name="android:textColor">@color/colorAccent</item>
    <item name="hintTextColor">@color/colorAccent</item>
    <item name="boxStrokeColor">@color/silver</item>
    <item name="boxStrokeWidth">1dp</item>
    <item name="errorEnabled">true</item>
    <item name="counterEnabled">false</item>
    <item name="android:theme">@style/exampleCursor</item>
</style>

and I add the style in the component

<com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/TextInputLayoutStyle"
….

and that’s it, the color change to the cursor works

screenshots_device

regards.

39reactions
dsn5ftcommented, Dec 11, 2019

The reason why using android:theme works is because colorControlActivated is a theme attribute, as opposed to a specific component attribute (e.g., hintTextColor).

One way you could customize a global theme attribute but only have that customization apply to a specific component is to use our materialThemeOverlay functionality. E.g.:

Create a theme overlay that customizes colorControlActivated:

  <style name="ThemeOverlay.AppTheme.TextInputEditText.FilledBox.Dense" parent="ThemeOverlay.MaterialComponents.TextInputEditText.FilledBox.Dense">
    <item name="colorControlActivated">@color/orange</item>
  </style>

Create a TextInputLayout style that uses the theme overlay:

  <style name="Widget.AppTheme.TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.FilledBox.Dense">
    <item name="materialThemeOverlay">@style/ThemeOverlay.AppTheme.TextInputEditText.FilledBox.Dense</item>
  </style>

Set the TextInputLayout style as the default in your app theme:

  <item name="textInputStyle">@style/Widget.AppTheme.TextInputLayout</item>
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Change the Cursor Color in CSS | SamanthaMing.com
Use the caret-color to change the color of the cursor. Now you can change the color to match the theme of ... The...
Read more >
caret-color - CSS: Cascading Style Sheets - MDN Web Docs
The caret-color CSS property sets the color of the insertion caret, the visible marker where the next character typed will be inserted.
Read more >
How to Change Cursor on Hover in CSS - W3docs
To specify the cursor appearance, use the CSS cursor property, which is used to change the mouse cursor type on elements. It can...
Read more >
How to change cursor color without changing text color?
Yes it's easy. Set your font color normally then use a custom cursor. http://beradrian.wordpress.
Read more >
CSS caret-color property - W3Schools
Definition and Usage. The caret-color property specifies the color of the cursor (caret) in inputs, textareas, or any element that is editable.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found