Change the line color of an Edittext

Asked

Viewed 5,861 times

4

I have a Layout simulating a PIN screen (type one lockscreen), where I have a EditText. I’m using the AppCompat v21 to be able to use the Material style elements. However, I want to set the highlight color only on this PIN screen (Accent) to the EditText, without altering the whole project.

Actually, this is my file styles.xml:

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

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/indigo</item>
        <item name="colorPrimaryDark">@color/indigo_dark</item>
        <item name="colorAccent">@android:color/white</item>
    </style>
</resources>

I want only the Edittext of the PIN screen to use the white color in the highlight. The rest of the app I want to have another color, but I’m not able to do it. Is there a tag I need to use? Is there any way to do this via code?

  • 1

    Renan, from what I’ve seen, is predicted in the next versions of Appcompat the attribute theme for widgets (currently only the Toolbar has this feature). On Android Lollipop this is already supported. More details on: https://chris.banes.me/2014/11/12/theme-vs-style/

2 answers

2


The generator Android Holo Colors can help you in this task, or you can create your own drawable to define as your background EditText, but involves more elements like focus color, disabled and etc.

For example, if you just want a solid color from the bottom edge, create a file bg_pin.xml in the directory res/drawable thus:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
  <!-- cor da borda -->
  <item>
      <shape>
          <solid android:color="@android:color/white" />
      </shape>
  </item>

  <!-- cor de fundo, tamanho somente da borda inferior -->
  <item android:bottom="1dp">
      <shape>
          <solid android:color="@android:color/transparent" />
      </shape>
  </item>
</layer-list>

And then, in the layout of your XML where you want to apply the style, something like this:

<EditText
    ....
    android:background="@drawable/bg_pin" />

1

I believe that the most correct way would be to create a derivation of the main theme of the project to this screen, because if other changes are necessary in it, you don’t have to keep creating specific files, but just change the theme to this screen.

Supposing your theme is called AppTheme.

I would create a theme for this screen and in it I would define the colorAcent.

<style name="AppTheme.Pin" parent="AppTheme">
    <item name="colorAccent">cor desejada</item>
</style>

Then just go on Androidmanifest.xml and specify that for this Activity, the theme will be this:

 <activity  android:name=".PinActivity"
       android:theme="@style/AppTheme.Pin"/>

If you want, you can inform by code also, within the method onCreate:

setTheme(R.style.AppTheme_Pin);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.