Search code examples
androidandroid-edittextandroid-textinputedittext

Custom style for all Edit Texts in the application


I have an Android application which uses Material design theme with backward compatibility through AppCompat.

There are multiple Textview's and EditText's in my application. I would like the same properties to be applied to all these TextView's and EditText's across the application. In order to achieve this, i have defined a custom style as shown below:

<!-- Base application theme. -->
    <style name="ParentTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="ArabicTheme" parent="ParentTheme">
        <item name="android:editTextStyle">@style/arabicEditText</item>
        <item name="android:textViewStyle">@style/arabicTextView</item>
    </style>

    <style name="arabicEditText" parent="@android:style/Widget.EditText">
        <item name="android:gravity">right</item>
        <item name="android:ellipsize">end</item>
    </style>

    <style name="arabicTextView" parent="@android:style/Widget.TextView">
        <item name="android:gravity">right</item>
    </style>

In my AndroidManifest.xml file under the <Application> tag, i have set android:theme="@style/ArabicTheme".

Below is the output of the activity: enter image description here

As seen in the above output, the Style is being applied to TextView only. However, The same is not being applied to EditText.

Incase, if i explicitly specify these properties to the EditText in the corresponding Actitivy's xml as shown below:

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="Name"
        android:ellipsize="end"
        android:gravity="right"
        android:ems="10"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="16dp"
        android:id="@+id/editText" />

i.e I have explicitly added android:ellipsize="end" and android:gravity="right" to the <EditText>, and only then the output is as expected:

enter image description here

Like i said, i have multiple TextView's and EditText's and i cannot explicitly add these properties to all of them. So, is there a way i can achieve this using Styles or any other approach? Am i doing something wrong here?


Solution

  • Your approach is correct. Just remove android: from the editText's attribute name:

        <item name="editTextStyle">@style/arabicEditText</item>
    

    I cannot explain this though. I guess appCompat things don't reuse android attributes, but add another ones with similiar names. Same goes with colorPrimary, srcCompat and others.