Reputation: 13
I am trying to create a button with centered icon and text as the example:
I've tried using a Button with drawableTop, but the centering of the icon and text doesn't scale well on different device screens. Is it better to use a constraintlayout as button? Seems like there should be a better way. Any ideas?
Upvotes: 1
Views: 850
Reputation: 363815
You can use a MaterialButton
.
Only icon:
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.App.Button.OutlinedButton.IconOnly"
app:icon="@drawable/..."/>
with:
<style name="Widget.App.Button.OutlinedButton.IconOnly" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="iconPadding">0dp</item>
<item name="android:insetTop">0dp</item>
<item name="android:insetBottom">0dp</item>
<item name="android:paddingLeft">12dp</item>
<item name="android:paddingRight">12dp</item>
<item name="android:minWidth">48dp</item>
<item name="android:minHeight">48dp</item>
</style>
or icon + text:
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
app:iconGravity="top"
android:text="BUTTON"
app:icon="@drawable/..."/>
Upvotes: 3
Reputation: 38
There are lots of way you can use it, here I am attaching you code and image please check it.
activity_main.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#10273c"
android:gravity="center"
android:orientation="horizontal"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/round_corner_purple"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@null"
android:src="@drawable/ic_call" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Contact"
android:textColor="@android:color/white"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/round_corner_purple"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@null"
android:src="@drawable/ic_dialpad" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Code"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textColor="@android:color/white"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
Drawable file : round_corner_purple.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="15sp">
<stroke android:width="1dp" android:color="#53545b"/>
<solid android:color="#10273c"/>
<corners
android:radius="10dp"/>
</shape>
Notes Please add text in string.xml for good practise.
Upvotes: 0
Reputation: 1129
There are lots of way to design this. I prefer using MaterialCardView
. Following xml can be helpful to you.
<com.google.android.material.card.MaterialCardView
android:id="@+id/btnCantacts"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="7dp"
app:strokeWidth="1dp"
app:strokeColor="#DD7B3B"
app:cardBackgroundColor="#1E1E1E">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_phone"/>
<com.google.android.material.textview.MaterialTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contacts"
android:textColor="#FFFFFF"
android:layout_gravity="center"/>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
Upvotes: 0