androidandroid-studioandroid-layoutthemesandroid-styles

Why does Android Studio have an issue with using custom Styles in my project?


Everywhere I reference one of my projects custom styles in a layout I get this syntax error about extraneous input.

Syntax error:

enter image description here

The style still gets applied to the layout correctly and the project builds without any issues but these errors are shown everywhere a style is referenced and I'm not sure why. Also, when attempting to find all references to specific styles, none of them show that they are being used in any layout files, but I know they are.

This is where my styles are located:

themes.xml

enter image description here

And here is an example of one of the styles located in the themes.xml file:

style.xml

enter image description here

I know that styles used to be put in the file called styles.xml and that the new way is to use themes.xml. So my best guess is that the issue is related to this change but I'm not completely sure. Even the google documentation here still says to use the styles.xml file.


Solution

  • To fix this you need to move GeneralButtonSyle from themes.xml to style.xml

    enter image description here

    Styles and Themes are different

    Among the ways one can actualize app design is Android themes and styles.

    1. Themes

    Themes act like a group of predefined styles that one can apply to the whole app or an activity . The default themes in Android fit well in most apps, but one can always create a custom theme that is appropriate for their app.

    2.Styles

    Another group of styles available under android design is styles. Like themes, styles also define styles, but they are applied to a particular view or group of views. Styles are much granular than themes and, as such, are applicable to independent UI elements . Styles can also inherit or extend attributes from other styles, making it fast and straightforward to alter.

    Hence, google recommends that we use themes and styles effectively to maintain consistency and make it easy to overhaul designs.

    https://developer.android.com/develop/ui/views/theming/themes#Styles

    https://developer.android.com/develop/ui/views/theming/themes#CustomizeTheme

    Update 1

    As per new android studio update

    1. Widget styles are added into styles.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="NewsTabTextSize" parent="TextAppearance.AppCompat.Widget.ActionBar.Menu">
            <item name="textSize">24sp</item>
        </style>
    
    
        <style name="NewsRadioButton" parent="Theme.AppCompat.Light">
            <item name="colorControlNormal">#000000</item>
            <item name="colorControlActivated">#af2c2c</item>
        </style>
    </resources>
    

    And Use it like this

    <RadioButton
        android:id="@+id/never"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:checked="false"
        android:theme="@style/NewsRadioButton"
        app:core_content_text_size="@{contentTextSize}"
        app:core_font_name="@{contentFont}"
        app:core_font_style="@{`normal`}"
        app:core_text_color="@{contentNeverTextColor}" />
    

    2. Activity styles are added into themes.xml

    <resources xmlns:tools="http://schemas.android.com/tools">
        <!-- Base application theme. -->
        <style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.NoActionBar">
            <item name="windowNoTitle">true</item>
            <item name="windowActionBar">false</item>
            <item name="android:windowFullscreen">true</item>
            <item name="android:windowContentOverlay">@null</item>
    
            <!-- Primary brand color. -->
            <item name="colorPrimary">@color/purple_500</item>
            <item name="colorPrimaryVariant">@color/purple_700</item>
            <item name="colorOnPrimary">@color/white</item>
            <!-- Secondary brand color. -->
            <item name="colorSecondary">@color/teal_200</item>
            <item name="colorSecondaryVariant">@color/teal_700</item>
            <item name="colorOnSecondary">@color/black</item>
            <!-- Status bar color. -->
            <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
            <!-- Customize your theme here. -->
        </style>
    </resources>
    

    And use it like this

    <activity android:name=".activity.CustomGalleryActivityNew"
        android:theme="@style/Theme.MyApplication"
        android:exported="false">
        <intent-filter>
            <action android:name="chatBot.ACTION_PICK" />
            <action android:name="chatBot.ACTION_MULTIPLE_PICK" />
    
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>