0

I want to have my buttons to have the colors I specified, but otherwise still look and behave like the standard buttons. So I searched and found this. I followed the advice given there, but the background color stays the standard grey. Any Idea where I´m going wrong?

In my styles.xml I put this:

<style name="AppTheme.Button" parent="Widget.AppCompat.Button.Borderless.Colored">
<item name="android:colorButtonNormal">@color/colorButtonDark</item>
<item name="android:textColor">@color/colorAccent</item>
</style>

My colors are these:

<resources>
[...]
<color name="colorAccent">#ffffff</color>
<color name="colorButtonDark">#300a03</color>
</resources>

And one example Button looks like this:

<Button
        android:text="exampleButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/button7" android:layout_marginTop="8dp"
        app:layout_constraintTop_toTopOf="@+id/guideline19" android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent" 
        app:layout_constraintStart_toStartOf="@+id/guideline"
        android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="8dp" android:theme="@style/AppTheme.Button"/>
BraveSentry
  • 339
  • 4
  • 19

2 Answers2

0

Try to use android.support.v7.widget.AppCompatButton instead of Button in xml.

<android.support.v7.widget.AppCompatButton
    android:text="exampleButton"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:id="@+id/button7" android:layout_marginTop="8dp"
    app:layout_constraintTop_toTopOf="@+id/guideline19" 
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintStart_toStartOf="@+id/guideline"
    android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
    android:layout_marginEnd="8dp" android:theme="@style/AppTheme.Button"/>
Cagri Yalcin
  • 402
  • 4
  • 13
0

Instead of

android:theme="@style/AppTheme.Button"

use

android:style="@style/AppTheme.Button".

EDIT1:

Theme vs Style

So what exactly is the difference? Well they are both declared in exactly the same way (which you already know), the difference comes in how they’re used.

Themes are meant to be the global source of styling for your app. The new functionality doesn’t change that, it just allows you to tweak it per view.

Styles are meant to be applied at a view level. Internally, when you set style on a View, the LayoutInflater will read the style and apply it to the AttributeSet before any explicit attributes (this allows you to override style values on a view).

Values in an attribute set can reference values from the View’s theme.

TL;DR: Themes are global, styles are local.

https://chris.banes.me/2014/11/12/theme-vs-style/

Community
  • 1
  • 1
drilonrecica
  • 327
  • 1
  • 4
  • 19
  • I don´t know about default behaviour, but it did not work, and in the link people are told explicitly to use `android:theme`. – BraveSentry Jan 08 '19 at 14:09