Issue
Hello I have a simple android application in which I have a button. This is the default theme of my application: themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.BaseAppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- 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>
<style name="Theme.WalkTest" parent="Theme.BaseAppTheme">
<item name="android:textColor">@android:color/black</item>
</style>
</resources>
In a separate file, button.xml
I've defined the theme for my button:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.WalkTest.ButtonSecondary" parent="Theme.WalkTest">
<item name="android:backgroundTint">@android:color/holo_green_dark</item>
<item name="android:textColor">@android:color/black</item>
</style>
</resources>
and here is the button I have in my fragment: fragment_buildings.xml
(I'm using navigation).
<Button
android:id="@+id/create_building_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="10dp"
android:drawableLeft="@drawable/ic_add"
android:paddingLeft="15dp"
android:paddingTop="20dp"
android:paddingRight="15dp"
android:paddingBottom="20dp"
android:text="@string/new_building"
android:textAlignment="textStart"
android:theme="@style/Theme.WalkTest.ButtonSecondary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar" />
Here is android studio's preview:
I can see that the background change is working, however the textColor
doesn't seem to change?
I can use the attributes on the button element to change its colour, but I wanna have reusable styles.
This is my Android Studio version:
Android Studio 4.1
Build #AI-201.8743.12.41.6858069, built on September 23, 2020
Runtime version: 1.8.0_242-release-1644-b3-6222593 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.15.7
GC: ParNew, ConcurrentMarkSweep
Memory: 2014M
Cores: 4
Registry: ide.new.welcome.screen.force=true
Non-Bundled Plugins: IdeaVIM, org.jetbrains.kotlin
Solution
use
style = "@style/Theme.WalkTest.ButtonSecondary"
instead of
android:theme="@style/Theme.WalkTest.ButtonSecondary"
Answered By - DevKazonovic
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.