I want to make button with image like this:
This button has image above its text and image is resized from 512x512.
When I searched google, there were some methods to achive this. But I thought putting Text
and Image
inside LinearLayout
and register onClick
doesn't look nice so I decided to use drawableTop
instead.
<Button
android:id="@+id/btn_gps"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="8dp"
android:drawableTop="@drawable/main_icon_gps"
android:padding="20dp"
android:text="@string/main_btn_gps"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="@+id/guideline_h60"
app:layout_constraintEnd_toEndOf="@+id/guideline_v50"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline_h40" />
Original image is 512x512 so I made another XML file(main_icon_gps
) inside drawable directory.
<!-- @drawable/main_icon_gps -->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/drawable_gps"
android:width="64dp"
android:height="64dp"
/>
</layer-list >
It looked nice when inspecting in Android Studio preview but when I launched the app in AVD, image resize doesn't work properly.
To sum up, I tried to set image size with android:width
and android:height
inside separate XML drawable file and it worked nicely in Android Studio preview. But Android doesn't respect width and height that I defined in XML drawable file. How to make Android respect the width and height that I've defined at XML?
I have to use same image as different size so resizing actual image or have multiple image don't seem to be best idea.
Nesting things inside LinearLayout
isn't good idea to me but if I have to achive what I want to make, then I will use that method. Before that, I want to fix the problem that image doesn't get resized.