17

I want to create a Button like this:

Button Example

Here is my code:

<Button
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:drawablePadding="10dp"
android:id="@+id/button"
android:text="Play Game"
android:background="@drawable/ronaldo"
android:drawableRight="@drawable/messi" />

But the messi.jpeg is too large, and it doesn't show the text in this situation. How to decrease the image size to fit the button?

May anyone help me to solve this problem?

Twitter khuong291
  • 11,328
  • 15
  • 80
  • 116

9 Answers9

35

If you are using API >= 23, you can put the drawable resource within a XML file like this messi_smaller.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/messi"
        android:width="smaller_width_in_dp"
        android:height="smaller_height_in_dp">
    </item>
</layer-list>

You must make sure to keep the drawable ratio when you set the width and the height. And just use this drawable in your button:

<Button
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_gravity="center"
    android:drawablePadding="10dp"
    android:id="@+id/button"
    android:text="Play Game"
    android:background="@drawable/ronaldo"
    android:drawableRight="@drawable/messi_smaller" />

By this way, you can change the drawable size just in xml code

Ulysses CP
  • 449
  • 4
  • 5
24

Solution 1:

Adding a drawable to a button has very limited functions. You can't change the drawable size, so the best way to fix it is to add a button with an image view beside it in a linear layout like this:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/ronaldo"
    android:orientation="horizontal">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:background="@null"
        android:text="Play Game"
        android:textColor="#ff0000"
        android:textStyle="italic" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:src="@drawable/messi" />

</LinearLayout>

Solution 2:

You can also do the following if you prefer this one, but you will have to add onClickListener() for each one of them:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/ronaldo"
    android:orientation="horizontal">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:background="@null"
        android:text="Play Game"
        android:textColor="#ff0000"
        android:textStyle="italic" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:src="@drawable/messi" />

</LinearLayout>

Solution 3:

As you have suggested in the comments, you would do something like this:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="80dp"
    android:layout_gravity="center"
    android:background="@drawable/ronaldo">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:layout_alignLeft="@+id/buttonLayout"
        android:layout_alignRight="@+id/buttonLayout"
        android:background="@null" />

    <LinearLayout
        android:id="@+id/buttonLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:gravity="center"
            android:text="Play Game"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#ff0000"
            android:textStyle="italic" />

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:src="@drawable/messi" />

    </LinearLayout>

</RelativeLayout>
Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57
1

I deleted the button and made a layer like a button

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@android:color/background_light"
    android:gravity="center"
    android:orientation="horizontal">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:onClick="go_map"
        android:clickable="true"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/img_m0"
            style="@style/menu2img"
            app:srcCompat="@drawable/ico_m0" />
       <TextView
            android:id="@+id/textView2"
            style="@style/menu2text"
            android:text="@string/men_map" />
    </LinearLayout>

[]

ॐ Rakesh Kumar
  • 1,318
  • 1
  • 14
  • 24
0

you can use a RelativeLayout like this:

<RelativeLayout
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:background="@drawable/ronaldo">

    <Button
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="play game"
        />
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@drawable/messi"
        android:scaleType="fitXY"
        />
</RelativeLayout>
saleh sereshki
  • 2,402
  • 3
  • 17
  • 18
0

Try this :

final Button button = (Button) findViewById(R.id.button1);
ViewTreeObserver vto = button.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                button.getViewTreeObserver().removeOnPreDrawListener(this);
                //scale to 90% of button height
                DroidUtils.scaleButtonDrawables(button, 0.9);
                return true;
            }
        });
PSN
  • 2,326
  • 3
  • 27
  • 52
0

If you are handling multiple screen sizes, creating alternative version for given screen (drawable-mdpi folder and so on...) can be solution.

David Vareka
  • 252
  • 2
  • 6
0

If you are importing a vector directly you can use the following:

<vector android:height="30dp"  android:tint="#00FF41"
    android:viewportHeight="24" android:viewportWidth="24"
    android:width="30dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="@android:color/white" android:pathData="M20,6h-8l-2,-2L4,4c-1.1,0 -1.99,0.9 -1.99,2L2,18c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,8c0,-1.1 -0.9,-2 -2,-2zM20,18L4,18L4,8h16v10zM8,13.01l1.41,1.41L11,12.84L11,17h2v-4.16l1.59,1.59L16,13.01 12.01,9 8,13.01z"/>
</vector>

enter image description here

Edwin Paz ss. -
  • 177
  • 2
  • 5
0

For API >= 23 and for android.support.design.widget.FloatingActionButton(FAB) , you can put the drawable resource (SVG) within a XML file with the path: File/New/Vector Asset and save the file under the name for example: ic_water_white_24dp.xml

and add app:maxImageSize attribute to the FAB:

<android.support.design.widget.FloatingActionButton
    android:id="@+id/btnAddProduct"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:clickable="true"
    android:focusable="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:maxImageSize="40dp" // add this line to increase the icon inside the fab
    app:srcCompat="@drawable/ic_water_damage_white_24dp" />

Before After

ngock
  • 25
  • 4
0

Try using vector files instead of JPEG. Also, you can even convert jpeg to svg format too. Once you import svg file into Android Studio project, you are going to see <vector> resource then just change the size as you want by width, height attributes. viewportWidth and viewportHeight is to set size for drawing on virtual canvas.

 <vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="20dp" <!-- fix the size here -->
    android:height="20dp"
    android:viewportWidth="40"
    android:viewportHeight="40">
  <path .../> <!-- your shapes are here -->
  <path .../> 
</vector>

Mark Choi
  • 420
  • 6
  • 16