Issue
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?
Solution
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.
Answered By - Darshan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.