Francisco Romero
Francisco Romero

Reputation: 13199

How can I make my image bigger on Android with XML?

I have an image on my ImageView but I don't know how to make it bigger with XML. My image it's the following:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/img"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:src="@drawable/image"/>

I tried changing android:layout_width and android:layout_height attributes but if I put them with a number (for example, 200dp width and 100dp height) the ImageView it's on the left of the screen and not center.

I want to center the image and makes it a little bit bigger than the original. I couldn't find anything to makes it bigger at the same time it is displayed on the center of the screen.

Is it possible? How can I do that?

Thanks in advance!

Upvotes: 1

Views: 3328

Answers (4)

George Rappel
George Rappel

Reputation: 7206

There are 3 attributes you can add to your imageview:

android:scaleType=""
android:scaleX=""
android:scaleY=""

ScaleType types can be seen here. Basically, you'll want to use CENTER_CROP.

scaleX and scaleY uses float as parameters, defining the scale for the two axis. See the reference here.

Upvotes: 1

Francisco Romero
Francisco Romero

Reputation: 13199

Both codes above works (Andrew Brooke and IntelliJ Amiya, Thanks!) but now I found a "trick" to avoid both of these methods. It's to create the ImageView using android:minWidth and android:minHeight so the final ImageView will be like:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/img"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:minWidth="100dp"
    android:minHeight="70dp"
    android:src="@drawable/image"/>

With that, you can center your ImageView with the size that you want.

Upvotes: 0

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75798

Use scaletype and add android:adjustviewbounds="true" in your image view section.

Upvotes: 2

Andrew Brooke
Andrew Brooke

Reputation: 12173

Keep the size you want as the width and height of the ImageView and change the gravity of its parent to center

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/img"
        android:layout_marginTop="40dp"
        android:src="@drawable/image"/>

</LinearLayout>

EDIT:

If this is what you want your layout to look like

enter image description here

then all you would need to do is add

android:layout_gravity="center_horizontal"

to your ImageView, and specify the width and height in dp

Upvotes: 1

Related Questions