moDevsome
moDevsome

Reputation: 209

Android GUI - How to apply a Common Style to Several Elements

As a beginner in Android development, I'm currently creating a calculator application.

The purpose I would like to create a specific style applicable to several elements has we can do with CSS class in web development. The Style will must be applicable to the nine numerics buttons of the calculator (1 to 9).

To avoid repeating the same attributes 9 times (once time per "Button" element), I've created a "Style" block where I've put all the attributes commons of each button.

I've added this <style> in a XML resource file named "styles.xml", the content of this file is well loaded by the application because don't have any problems with the properties "dimen" and "colors" declared in this file.

Here's the code:

<!-- Style of the calculator numerics buttons -->
<dimen name="calculatorNumberWidth">60dp</dimen>
<dimen name="calculatorNumberHeight">60dp</dimen>
<style name="calculatorNumberStyle" parent="TextAppearance.AppCompat">
    <item name="android:layout_width">90dp</item>
    <item name="android:layout_height">90dp</item>
    <item name="android:gravity">center</item>
    <item name="backgroundTint">#F1FAEE</item>
    <item name="android:textColor">#000000</item>
    <item name="android:textSize">36dp</item>
    <item name="layout_constraintBottom_toBottomOf">parent</item>
    <item name="layout_constraintRight_toRightOf">parent</item>
    <item name="layout_constraintTop_toTopOf">parent</item>
</style>

My problem:

I can't apply this style. I've tried two methods which aren't functional:

  1. By adding the attribute android:style="@style/calculatorNumberStyle" in the "Button" tag of each button. In this case, the compiler stops the build and raises this error: AAPT: error: attribute android:style not found

  2. By adding the attribute android:theme="@style/calculatorNumberStyle" in the "Button" tag of each button. In this case, the application is well compiled but the style is not applied.

I've tried to find my way throw this documentation: https://developer.android.com/guide/topics/ui/look-and-feel/themes, but without success.

Does anybody know which is the good method?

SDK platform version: 11.0, AndroidStudio version: 4.1

Thanks by advance and good day at all,

Mickaël

Upvotes: 1

Views: 391

Answers (1)

Zain
Zain

Reputation: 40810

Just try to use style without android directive

 style="@style/calculatorNumberStyle"

Upvotes: 1

Related Questions