CROSP
CROSP

Reputation: 4617

Applying style globally Android with theme and Style inheritance

I am trying to create base theme for whole my app, I have created custom styles for base components like EditText, Button, TextView and wanna to set them globally in my app.

Here is my xml code.

 <style name="AppThemeBase" parent="Theme.AppCompat.NoActionBar">
        <item name="colorPrimary">@color/base_app_color</item>
        <item name="colorPrimaryDark">@color/base_app_color</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="windowActionBarOverlay">true</item>
        <item name="windowActionModeOverlay">true</item>
        <item name="android:editTextStyle">@style/EditTextBaseStyle</item>
        <item name="android:textViewStyle">@style/TextViewBaseStyle</item>
    </style>

My custom edit text style

<style name="EditTextBaseStyle" parent="Widget.AppCompat.EditText">
        <item name="android:textColor">@color/base_text_color</item>
        <item name="android:textSize">@dimen/text_size_edit_text</item>
        <item name="android:textColorHint">@color/base_hint_color</item>
        <item name="android:layout_centerHorizontal">true</item>
        <item name="android:background">@drawable/edit_text_rounded_corners_bg</item>
    </style>

I also tried to use @android:style/Widget.EditText as a parent.

<style name="TextViewBaseStyle" parent="@android:style/Widget.TextView">
    <item name="android:textColor">@color/base_text_color</item>
    <item name="android:textSize">@dimen/size_text_base</item>
    <item name="android:layout_margin">@dimen/margin_text_view_base</item>
</style>

And of course in AndroidManifest.xml file

<application
    android:name=".application.QrApplication"
    android:allowBackup="true"
    android:icon="@drawable/ic_logo_green"
    android:label="@string/app_name"
    android:theme="@style/AppThemeBase">
    <activity
        android:name=".ui.activities.LoginActivity"
        android:configChanges="orientation|keyboardHidden"
        android:label="@string/app_name"
        android:noHistory="true"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

I have several questions

  1. Why it doesn't apply style for each view (EditText and TextView), if set style manually in xml layout it works like a charm, but I cannot get it working globally
  2. About style inheritance. There are two types of inheritance explicit or implicit or for user custom styles using . (dot) and using keyword parent for default themes, but what will be if combine this types of inheritance like this <style name="SomeStyle.ParentStyle" parent="Widget.AppCompat.Button">?
  3. Are styles and theme applied to fragments inside activity, theoretically it should because for inflating fragment context from activity is used which is ContextWrapper ?

Please if you have any ideas what can cause such problem, help to solve it or at least share your thoughts.

Thanks.

Upvotes: 0

Views: 623

Answers (1)

CROSP
CROSP

Reputation: 4617

The problem was solved

Instead of overriding

<item name="android:editTextStyle">@style/EditTextBaseStyle</item>

I have to override

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

To figure out what style item you need to override, follow the inheritance tree of base style/theme and you will find what is the name of style item you need to use.

BUT NOTE
Text view style should be still

<item name="android:textViewStyle">@style/TextViewBaseStyle</item>

It is weird but as is.

Upvotes: 1

Related Questions