Search code examples
androidmaterial-designandroid-buttonmaterial-components-androidmaterial-components

Button with icon and text centered


I want to have button with icon and text near icon both in center. I managed to do that with padding but since android has a lot of different screen sizes, it is not working well on small screen sizes. This is my code:

 <Button
        android:id="@+id/scanbtn"
        android:text="@string/scan_btn_text"
        android:textAlignment="center"
        android:fontFamily="@font/open_sans_light"
        android:drawableStart="@drawable/ic_photo_camera"
        android:paddingStart="100dp"
        android:paddingEnd="120dp"
        android:textAllCaps="false"
        android:layout_width="match_parent"
        android:layout_height="65dp"
        android:layout_marginEnd="15dp"
        android:layout_marginStart="15dp"
        android:layout_marginTop="25dp"
        android:background="@drawable/border"
        android:textColor="@color/white"
        android:textSize="19sp"
        android:transitionName="use/scanbtn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toTopOf="parent" />

this is how my button looks. I want it to look that way but it doesnt work on smaller devices with this code

This is my button. Please show me other (better) way to make this button be same on all screen sizes


Solution

  • Just wrap a TextView by FrameLayout and use the Framelayout as a button.

    <FrameLayout
            android:background="#E06666"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:background="@null"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/icon"
                android:text="Scan"
                android:layout_gravity="center"
                android:gravity="center"
                />
     </FrameLayout>
    

    Bellow is my result:

    enter image description here