Koushik
Koushik

Reputation: 13

How to override Theme.AppCompat.Light.DarkActionBar theme for edittext

I am trying to create a style for EditText (MyEditText), which inherits from base theme and added extra property settings.

<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">
    <item name="homeAsUpIndicator">@drawable/ic_back_indicator</item>
</style>

<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
<style name="MyEditText" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:background">#dbffb1</item>
</style>

When applied MyEditText style to EditText control, existing styles (from theme) seems to be overridden (like line under EditText vanished). Same thing happens when background is changed for EditText in the layout.

I am using targetSdkVersion 23. I like to know what is the proper method to override a single property from default theme.

Here is the code for EditText in layout (as asked by @3amoura):

<EditText android:layout_width="match_parent"
    android:layout_height="82dp"
    android:id="@+id/et_description"
    android:inputType="textMultiLine|textLongMessage"
    android:lines="10"
    android:hint="Describe your complaint in details"
    android:gravity="top"
    style="@style/MyEditText" />

Upvotes: 1

Views: 1127

Answers (2)

UDI
UDI

Reputation: 1602

Edited

If you are only trying to add a color of the Line drawn -> setBackgroundTint

If you want to change both the background color and the line color, then custom layer-list or selector will be the only option. A sample layer-list

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <solid android:color="#dbffb1" /> <!-- background color-->
    </shape>
</item>

<item
    android:top="-2dp"
    android:right="-2dp"
    android:left="-2dp">
    <shape>
        <solid android:color="@android:color/transparent" />
        <stroke
            android:width="1dp"
            android:color="@color/colorAccent" />  <!-- color of stroke -->
    </shape>
</item>
</layer-list>

Upvotes: 0

Omar HossamEldin
Omar HossamEldin

Reputation: 3111

The style of MyEditText should inherit from Widget.AppCompat.EditText instead of Theme.AppCompat.Light.DarkActionBar such that it will be

<style name="MyEditText" parent="Widget.AppCompat.EditText">
<item name="android:background">#dbffb1</item>

Then you use the style in the EditText xml

Upvotes: 1

Related Questions