Issue
I'm supposed to create a button with an icon in the center of the button next to the text like this:
This is the solution I came up with:
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="75dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:backgroundTint="@color/tomato"
app:cardCornerRadius="70dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintWidth_percent="0.9">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="20dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal">
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginRight="10dp"
android:src="@drawable/ic_baseline_shopping_cart_24" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add This To Cart"
android:textColor="@color/white"
android:textSize="25dp" />
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
but I think it's very hard coded. So, my question is: is there any better way to design it or some library used to do it?
Solution
You can use a MaterialButton
:
<com.google.android.material.button.MaterialButton
app:icon="@drawable/ic_add_24px"
app:iconPadding="0dp"
app:iconGravity="textStart"
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.App.rounded50"
../>
with:
<style name="ShapeAppearanceOverlay.App.rounded50" parent="">
<item name="cornerSize">50%</item>
</style>
Answered By - Gabriele Mariotti
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.