Search code examples
androidxmlandroid-buttonandroid-themematerial-components-android

Black button action on android studio


I changed a button color to black on android studio like this: In colors.xml:

<color name="colorBlack">#000000</color>

In styles.xml:

 <style name="AppTheme.Button" parent="Base.Widget.AppCompat.Button">
        <item name="colorButtonNormal">@color/colorBlack</item>
    </style>

In layout_frag.xml:

 <Button
   android:id="@+id/button"
   android:theme="@style/AppTheme.Button"
   android:layout_width="match_parent"
   android:layout_height="75dp"
   android:text="@string/hello_first_fragment"
   android:textColor="@color/design_default_color_background" />

My black button works properly, although the tint background color actions when pressing on the button doesn't work anymore since the button is super dark and the tint is normally dark grey. ![Button2 tint in grey onClick][1] I would like to know where can I change the tint action when clicking on the button?


Solution

  • You can override the colorControlHighlight attribute with:

    <androidx.appcompat.widget.AppCompatButton
        android:theme="@style/ThemeOverylay.Button"/>
    

    with:

    <style name="ThemeOverylay.Button">
        <item name="colorControlHighlight">@color/....</item>
        <item name="colorButtonNormal">@color/....</item>
    </style>
    

    If you are using a Material Components theme you can use a MaterialButton and the rippleColor attribute:

    <com.google.android.material.button.MaterialButton
        app:rippleColor="@color/my_selector"
        ../>
    

    With a selector like this:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
      <item android:alpha="..." android:color="?attr/colorOnPrimary" android:state_pressed="true"/>
      <item android:alpha="..." android:color="?attr/colorOnPrimary" android:state_focused="true" android:state_hovered="true"/>
      <item android:alpha="..." android:color="?attr/colorOnPrimary" android:state_focused="true"/>
      <item android:alpha="..." android:color="?attr/colorOnPrimary" android:state_hovered="true"/>
      <item android:alpha="..." android:color="?attr/colorOnPrimary"/>
    
    </selector>