Hayk Nahapetyan
Hayk Nahapetyan

Reputation: 4550

How to set image in ImageView?

I need to put my own image on some imageView. So that's why I created folder named "drawable" in res folder , and copy my Image in this folder , but now I don't know how to set this image to my imageView in XML code. Can anybody help? and sorry for English :)

Upvotes: 1

Views: 9668

Answers (5)

Ramani Hitesh
Ramani Hitesh

Reputation: 214

<ImageView
 android:layout_width="150dp"
 android:layout_gravity="bottom"
 android:layout_height="150dp"
 android:id="@+id/image1"
 android:layout_marginTop="11dp"
 android:background="@drawable/image1"
          />

Upvotes: 0

Hemant
Hemant

Reputation: 759

Try this.

<ImageView
    android:id="@+id/YourImageViewID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/Your_Image_Name"
   />

This may help you.

Upvotes: 2

Goldsmith
Goldsmith

Reputation: 508

In your layout XML file:

<ImageView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@drawable/your_image_name"/>

Replace your_image_name with the name of the image file without the extension (e.g. if your drawable file is called img1.png, the src attribute should have a value of @drawable/img1).

Upvotes: 3

Lazy Ninja
Lazy Ninja

Reputation: 22527

Set it using android:src=""

   <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image_name" />

Upvotes: 2

Padma Kumar
Padma Kumar

Reputation: 20041

//set background as your image by calling the android:background attributes in your xml

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/myimage"
    />

Note: you no need to create any drawable folder by default android project will contain

drawable-mdpi
drawable-hdpi
drawable-ldpi

you can put your image in that.

Upvotes: 1

Related Questions