42

In order to use Chip and ChipGroup, I set Application style extends Theme.MaterialComponents.Light.NoActionBar int manifests.xml, then I set Button "android:background" attr, but it does not effect! why? and what can I do?

This is my style:

  <style name="AppBaseTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="colorPrimary">@color/primary_material_light</item>
    <item name="colorPrimaryDark">@color/header_color</item>
    <item name="actionBarSize">48dp</item>
    <item name="android:screenOrientation">portrait</item>
    <!--<item name="buttonStyle">@style/AntButton</item>-->
    <!--<item name="materialButtonStyle">@style/AntButton</item>-->
    <!--<item name="android:button"></item>-->
    <!--<item name="android:progressTint">@color/ffc000</item>-->
    <item name="colorAccent">@color/ffc000</item>
</style>


<style name="AntButton" parent="android:Widget">
    <item name="android:background">@drawable/abc_btn_default_mtrl_shape</item>
    <item name="android:textAppearance">?android:attr/textAppearanceButton</item>
    <item name="android:minHeight">48dip</item>
    <item name="android:minWidth">88dip</item>
    <item name="android:focusable">true</item>
    <item name="android:clickable">true</item>
    <item name="android:gravity">center_vertical|center_horizontal</item>
    <!--<item name="android:colorButtonNormal">@color/white</item>-->
</style>

I have tried to change buttonStyle and materialButtonStyle, but not effect too!

this is my layout XML:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/ll_popup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/white"
        android:divider="@drawable/shape_divider_05dp"
        android:orientation="vertical"
        android:showDividers="beginning|middle">

        <!--only can use backgroundTint to change background color-->
        <Button
            android:id="@+id/item_popupwindows_Photo"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:backgroundTint="@color/white"
            android:text="Pictrue"
            android:textColor="@color/c_666666"
            android:textSize="16sp" />

        <!--background not effect !!-->
        <Button
            android:id="@+id/item_popupwindows_cancel"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:background="@color/white"
            android:text="cancel"
            android:textColor="@color/c_666666"
            android:textSize="16sp" />
    </LinearLayout>
</LinearLayout>

this is result :

enter image description here

Because I have used Chip and ChipGroup in APP, I have to user theme extends MaterialComponents! do you know how to resolve it ?please tell me, thanks!

in this page Can't use android:background with button from the new material components, the author wants to change the default padding, but I want to change the backgroundDrawable, so, it does not work for me.

Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
CnPeng
  • 563
  • 1
  • 4
  • 8
  • 1
    Possible duplicate of [Can't use android:background with button from the new material components](https://stackoverflow.com/questions/52673053/cant-use-androidbackground-with-button-from-the-new-material-components) – Ben P. Oct 10 '18 at 16:19
  • 1
    The important thing to understand is that using that theme changes the `LayoutInflater` that's used to inflate your layouts. This special layout inflater replaces generic ` – Ben P. Oct 10 '18 at 16:23

4 Answers4

87

If you want a true Button, but one that you can modify like the framework Button (instead of the MaterialButton), then you can explicitly specify the framework version in your layout file. Replace this:

<Button
    android:id="@+id/item_popupwindows_cancel"
    ... />

with this:

<android.widget.Button
    android:id="@+id/item_popupwindows_cancel"
    ... />

This will give you what it says on the tin: an android.widget.Button, which should respond to styling the way you expect.

Similarly, you could use a <androidx.appcompat.widget.AppCompatButton> if you want support library features but not material components features.

Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • thanks very much , It worked for me . But i din't know why can not user ? – CnPeng Oct 11 '18 at 08:03
  • 18
    When you use a non-namespaced tag, like ` – Ben P. Oct 11 '18 at 14:29
  • @BenP., yes. Not only `LinearLayout`, but other containers too (ConstraintLayout, FrameLayout, etc). – CoolMind May 12 '20 at 12:43
2

In this particular case, the LinearLayout holding your second Button seems to have a white background color. That means you don't need to explicitly specify a white background for your Button; you can just let the LinearLayout's background show through.

This can be accomplished by using the "Text Button" style of MaterialButton:

style="@style/Widget.MaterialComponents.Button.TextButton"
Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • 1
    Thanks, i want to change the button background drawable, not only change the background color . When i use TextButton style , also can't change background drawable, it's only change the default background color to transparent. And now i had to use TextView instead of Button, it's a temp solve. If you have some other msg , please tell me , Thanks. – CnPeng Oct 11 '18 at 01:57
0

Use this code for change the color

app:backgroundTint="@color/primaryColor"
0

You can also use ImageButton instead of Button from material components. android:background is working on ImageButton.

<ImageButton
    android:id="@+id/item_popupwindows_cancel"
    ... />
bigant02
  • 176
  • 3
  • 16