Search code examples
androidandroid-buttonmaterial-components-androidmaterial-components

Why does button text not showing in bydefault center in MaterialComponents?


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>

Solution

  • 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