1

When I was using AppCompat this thing was working fine, but when I changed to MaterialComponents, it looks bad.

See the current image:

logo

See the 'W' it is not showing properly. for 'H' Also same problem.

Note: I tried using Button, AndroidXbutton and MaterialButton, nothing is working in MateriaComponents case.

Code:

<androidx.cardview.widget.CardView
            android:id="@+id/circle_card_payment"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:elevation="12dp"
            android:innerRadius="0dp"
            android:shape="ring"
            android:thicknessRatio="1.9"
            app:cardCornerRadius="20dp">

            <ImageView
                android:id="@+id/icon"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:visibility="gone"></ImageView>

            <com.google.android.material.button.MaterialButton
                android:id="@+id/logo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:text="A"
                android:textColor="@color/colorWhite"
                android:textSize="16sp"
                android:visibility="gone"></com.google.android.material.button.MaterialButton>


        </androidx.cardview.widget.CardView>

I'm setting programatically drawable background, because in material components, it does not giving effect on XML.

logo.setBackgroundResource(R.drawable.bg_circle_btn);

bg_circle_btn.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid android:color="?attr/toolbar_color" />

    <size
        android:width="120dp"
        android:height="120dp" />
</shape>

Please note if I change cardview size 40dp to 50dp, this problem will solve, but size will be bigger that looks bad.

Complete XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/cardbackgroundColor"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp">


        <androidx.cardview.widget.CardView
            android:id="@+id/circle_card_payment"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:elevation="12dp"
            android:innerRadius="0dp"
            android:shape="ring"
            android:thicknessRatio="1.9"
            app:cardCornerRadius="20dp">

            <ImageView
                android:id="@+id/icon"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:visibility="gone"></ImageView>

            <com.google.android.material.button.MaterialButton
                android:id="@+id/logo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:text="A"
                android:textColor="@color/colorWhite"
                android:textSize="16sp"
                android:visibility="gone"></com.google.android.material.button.MaterialButton>


        </androidx.cardview.widget.CardView>

        <TextView
            android:id="@+id/tv_category_item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="12dp"
            android:layout_toLeftOf="@+id/next"
            android:layout_toRightOf="@+id/circle_card_payment"
            android:text="Category"
            android:textColor="?attr/day_colorDarkGray_night_colorWhite"
            android:textSize="16sp" />


        <ImageView
            android:id="@+id/next"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:padding="6dp"
            android:tint="@color/colorVeryLightGray"
            android:visibility="visible"
            app:srcCompat="@drawable/ic_navigate_next_black_24dp" />


        <ImageView
            android:id="@+id/hamburger_menu"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/next"
            android:padding="6dp"
            android:tint="?attr/day_colorBlack_night_colorWhite"
            android:visibility="gone"
            app:srcCompat="@drawable/ic_more_vert_black_24dp" />
    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_marginLeft="70dp"
        android:background="@color/colorVeryLightGray" />


</LinearLayout>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Priyanka Singh
  • 193
  • 1
  • 9
  • Check your logcat. You should find: `W/MaterialButton: Do not set the background; MaterialButton manages its own background drawable.` – Gabriele Mariotti Oct 09 '19 at 18:17
  • @Prianka, You should mark the answer as accepted there. https://stackoverflow.com/questions/58301863/what-is-alternative-of-androidbackground-in-materialcomponents – Md. Asaduzzaman Oct 09 '19 at 18:17
  • @GabrieleMariotti but I want to use my own drawable background. What should I do? – Priyanka Singh Oct 09 '19 at 18:22
  • @Md.Asaduzzaman What about my solution? I'm still trying dear. – Priyanka Singh Oct 09 '19 at 18:22
  • I know but check your question, two questions are different and solution should be different also. accept one and then ask other. It will motivate others to help you. Thanks – Md. Asaduzzaman Oct 09 '19 at 18:24
  • @Md.Asaduzzaman Okay, I'll. but right now just help me with below answer. I tried it , it's working. but when I use this inside any layout. same problem happens. – Priyanka Singh Oct 09 '19 at 19:00

1 Answers1

1

In the MaterialButton you shouldn't use setBackground* methods.
You can check the logcat, you should find something like:

W/MaterialButton: Do not set the background; MaterialButton manages its own background drawable

Instead in the MaterialButton you can define a custom shape in different way. For example you can do:

<com.google.android.material.button.MaterialButton
            android:id="@+id/logo"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:text="W"
            android:padding="0dp"
            app:strokeWidth="0dp"
            android:insetLeft="0dp"
            android:insetTop="0dp"
            android:insetRight="0dp"
            android:insetBottom="0dp"
            android:textColor="#FFFFFF"
            android:textSize="16sp"
            app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay_ButtonCircle50"
            />

where the ShapeAppearanceOverlay_ButtonCircle50 is defined by:

  <style name="ShapeAppearanceOverlay_ButtonCircle50">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">50%</item>
  </style>

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • One last request. Can you please adjust with me inside cardview? because I'm setting text android:layout_toRightOf="@+id/circle_card_payment" to right of the cardview. And in cardview I've image and this material button. – Priyanka Singh Oct 09 '19 at 18:40
  • And what is the problem about it? – Gabriele Mariotti Oct 09 '19 at 19:00
  • when I use this button inside relative layout, it giving same problem. – Priyanka Singh Oct 09 '19 at 19:01
  • If you'll carefully observe the screenshot and complete XML, I set the category name to the right of that cardview. Now suppose I remove the cardview then I've to do any other layout like relative layout, so that I can do android:layout_toRightOf="@+id/circle_card_payment" but because of relative layout width which should be 40 do, again it is giving same problem. – Priyanka Singh Oct 09 '19 at 19:08
  • 1
    I think this is a separate problem.You may wish to consider asking a separate question. – Gabriele Mariotti Oct 09 '19 at 19:11