How to set image in ImageView? - android

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 :)

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).

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" />

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.

//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.

<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"
/>

Related

Resource is not a Drawable Resources$NotFoundException

Getting this error since I started changing device for testing. Did some searching and none of it help because they are outdated. Any help is appreciated!
xml file
<ImageView
android:id="#+id/loginpanel"
android:layout_width="491px"
android:layout_height="210dp"
android:layout_marginStart="41dp"
android:layout_marginTop="146dp"
android:layout_weight="1"
android:background="#drawable/loginpanel"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/numpanel2"
android:layout_width="388px"
android:layout_height="452px"
android:layout_marginStart="80dp"
android:layout_marginTop="368dp"
android:layout_weight="1"
android:background="#drawable/numpanel1"
android:alpha="0.95"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
These are my folders, there's no drawable-v24 folder
Make sure your loginpanel and numpanel1 images is inside drawable folder not inside drawable-v24 folder
move your both images loginpanel and numpanel1 inside drawable
just copy-paste from drawable-v24 to drawable folder it will work

Android image can't convert to drawable

I am not able to add image. I downloaded the image and added it to res directory under drawable-mdpi. Then when I try to add the image on the view using imageView, it gives me this error
Couldn't resolve resource #drawable/ball01
Failed to convert #drawable/ball01 into a drawable
Here is my xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:layout_gravity="center"
android:textSize="32sp"
android:layout_above="#+id/button"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enlighten Me"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignParentRight="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_above="#+id/textView"
android:layout_centerHorizontal="true"
android:src="#drawable/ball01" />
</RelativeLayout>
What am I doing wrong here.
The directory name should be 'drawable-mdpi'. Using a hyphen instead of an underscore.
image name must Digital or lowercase 。 ide only identification.
I had this same problem, I had this image which I named Barcode and then saved it into my drawable folder, then I decided it would be better to have it named barcode with a small b, so I renamed it.
Then I checked if it would work, and I got the same errors as you, after changing the name in my xml to 'Barcode' with a big B, it worked.
solution for me: delete barcode image from drawable and add it again with the name I'm going to use for it, don't change the name while it is in drawable folder.
I had this problem on android studio.
if you close and open project and it continues, all you need to do is RIGHT CLICK on the particular image you put in your "res" folder (that's giving the problem) and refactor-RENAME. Remove spaces in the name of the file. that should do it.

background image to app giving me error

I wanted to put background image to my app.
I kept image in res>drawable-hdpi folder.
Seted my image view as:
<LinearLayout
android:id="#+id/tv_un"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10pt"
android:textColor="#444444"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="res/drawable-hdpi/capture.PNG"/>
But its giving me error in the form of tooltip on line:
error: Error: String types not allowed (at 'src' with value
'res/drawable-hdpi/capture.PNG').
Am i pasting image in wrong folder?
Or code is wrong??
Please help me.
try this, put capture.png in any drawable folder
android:src="#drawable/capture"
Replace
android:src="res/drawable-hdpi/capture.PNG"
with
android:src="#drawable/capture"
Hope it helps.
change
android:src="res/drawable-hdpi/capture.PNG"
with
android:src="#drawable/capture"
android:src="#drawable/capture"
put this.

Android: .png to Imageview? Most efficent way?

I've got a .png file in my Drawable folder. How do I make it into an Imageview?
imageView.setImageResource(R.drawable.your_drawable);
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_image"
/>

How to add image for button in android?

How can I add an image to a button, rather than text?
Rather humorously, considering your tags, just use the ImageButton widget.
Simply use ImageButton View and set image for it:`
<ImageButton
android:id="#+id/searchImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:src="#android:drawable/ic_menu_search" />
As he stated, used the ImageButton widget. Copy your image file within the Res/drawable/ directory of your project. While in XML simply go into the graphic representation (for simplicity) of your XML file and click on your ImageButton widget that you added, go to its properties sheet and click on the [...] in the src: field. Simply navigate to your image file. Also, make sure you're using a proper format; I tend to stick with .png files for my own reasons, but they work.
You should try something like this
<Button
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/qrcode"/>
the android:background="#drawable/qrcode" will do it
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="News Feed"
android:icon="#drawable/newsfeed" />
newsfeed is image in the drawable folder
The new MaterialButton from the Material Components can include an icon:
<com.google.android.material.button.MaterialButton
android:id="#+id/material_icon_button"
style="#style/Widget.MaterialComponents.Button.TextButton.Icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/icon_button_label_enabled"
app:icon="#drawable/icon_24px"/>
You can also customize some icon properties like iconSize and iconGravity.
Reference here.
Put your Image in drawable folder. Here my image file name is left.png
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="118dp"
android:layout_y="95dp"
android:background="#drawable/left"
android:onClick="toast"
android:text=" " />
You can create an ImageButton in your android activity_main.xml and which image you want to place in your button just paste that image in your drawable folder below is the sample code for your reference.
<ImageButton
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="49dp"
android:layout_weight="1"
android:onClick="prev"
android:src="#drawable/prev"
/>
Use android:drawableright or android:drawabletop,... attributes for buttons in xml file.
For example: in xml file add this attribute for a button:
Android:drawableleft="#drawable/ic_person"
or
Android:drawableright="#mipmap/ic_launcher"
you can use ImageView as Button. Create an ImageView and set clickable true after in write imageView.setOnClickListener for ImageView.
<ImageView
android:clickable="true"
android:focusable="true"`
android:id="#+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
and in Activity's oncreate:
imageView.setOnClickListener(...

Categories

Resources