0

Is it possible to apply an accent color to an particular EditText while others are unaffected ?

i have this :

<?xml version="1.0" encoding="utf-8"?>
<resources>

  <!-- EditText on a blue background -->
  <style name="BlueBGTextView" parent="@android:style/Widget.Material.Light.EditText">
  </style>

  <!-- default EditText -->
  <style name="AppTheme.EditTextStyle" parent="@android:style/Widget.Material.Light.EditText">
  </style>

  <!-- default Theme -->
  <style name="AppTheme" parent="@android:style/Theme.Material.Light.NoActionBar"> 
    <item name="android:editTextStyle">@style/AppTheme.EditTextStyle</item>
    <item name="android:colorAccent">#ff0288d1</item> <!-- use to setup the color of the text selection handles -->   
    <item name="@attr/BlueBGTextViewStyle">@style/BlueBGTextView</item>
  </style>

</resources>

but the problem is that <item name="android:colorAccent">#ff0288d1</item> is applied globally to all my edits. I would like to apply it only to the

<style name="BlueBGTextView" parent="@android:style/Widget.Material.Light.EditText">
</style>

any idea how i can do this ?

Jay Sidri
  • 6,271
  • 3
  • 43
  • 62
zeus
  • 12,173
  • 9
  • 63
  • 184
  • use different theme for edittext! it will work – xbadal May 26 '17 at 15:10
  • this might me what you are looking for. https://stackoverflow.com/q/17449169/6904440 – Simo May 26 '17 at 15:41
  • create a theme and use it only with the specific edittext that you are tryiing to customize. – Simo May 26 '17 at 15:42
  • simo, i don't understand how you can create a them and use it only with the specific EditText, because the theme must be connected in androidmanifest and only one can be connected as i know – zeus May 26 '17 at 15:43

2 Answers2

1

try this, i have tested it on my system.

 <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:theme="@style/Edittext" />

declare your theme is styles.xml

<style name="Edittext" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">#ed2727</item>
    <item name="colorPrimaryDark">#ed2727</item>
    <item name="colorAccent">#ed2727</item>

    <item name="colorControlNormal">#ed2727</item>
    <item name="colorControlActivated">#ed2727</item>
    <item name="colorControlHighlight">#ed2727</item>

</style>

reference here

xbadal
  • 1,284
  • 2
  • 11
  • 24
  • thanks xbadal, but how you do this ? i create my edit via the constructor with a DefStyleAttr – zeus May 26 '17 at 15:17
  • also i want to apply the accent color to only a particular edit, not all the edit of the application – zeus May 26 '17 at 15:20
  • unfortunatly it's not work :( note that i create the EditText manually not via xml – zeus May 27 '17 at 08:10
  • have you created custom Edittext? can you post that class? – xbadal May 27 '17 at 08:12
  • i just create the edittext via it's constructor: EditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) – zeus May 27 '17 at 08:22
1

I have used like this hope this can help u

<style name="MyEditTextTheme">
    <!-- Used for the bottom line when not selected / focused -->
    <item name="colorControlNormal">#9e9e9e</item>
    <!-- colorControlActivated & colorControlHighlight use the colorAccent color by default -->
</style>

Now u can apply this theme to particular edittext

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/MyEditTextTheme"/>
Lokesh Desai
  • 2,607
  • 16
  • 28