Android: .png to Imageview? Most efficent way? - android

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

Related

How to use Glide with ImageButton?

I have an ImageButton that doesn't have any background.
I have a png to be used as an Image for the button. How can I do it with glide (my image is in the drawable folder)?
This is my code:
activity_main.xml:
<RelativeLayout
<ImageButton
android:id="#+id/ib_main_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:scaleType="fitCenter"
/>
</RelativeLayout>
MainActivity.java:
ImageButton ib_main_home;
Glide.with(MainActivity.this)
.load(R.drawable.home)
.placeholder(R.drawable.home)
.centerCrop()
.into(ib_main_home);
If the image is in drawable directory, you don't need Glide.set src property in xml. remove glide code from activity
<ImageButton
android:id="#+id/ib_main_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:scaleType="fitCenter"
android:src="#drawable/home"/>
You may also set image through activity like below
ib_main_home.setImageDrawable(R.drawable.home)
You should use custom targets, in particular a ViewTarget. Take a look at the wiki.
in your case:
Glide.with(this)
.fromResource(R.drawable.home)
.into(ib_main_home);
If the image is in drawable directory, you don't need Glide. Just do:
imageButton.setImageResource(R.drawable.home)

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.

the picture is stretched in imageview android

i want to make animation with two pictures, but they are steretched. When i open this image like src in imageView- all is ok(image 1) , but when i run animation - it steretced(image 2).
<ImageView
android:id="#+id/iv_exercise_info"
android:layout_width="fill_parent"
android:layout_height="238dp"
android:layout_gravity="center" />
I try to set scaleType fitXY, but it does not help me. Sory for my english.
I think layout defined for imageview is where the issue is. Try this.
<ImageView
android:id="#+id/iv_exercise_info"
android:layout_width="wrap_content"
android:layout_height="238dp"
android:layout_gravity="center" />
Hope this solves your issue
Use following :-
android:id="#+id/iv_exercise_info"
android:layout_width="fill_parent"
android:layout_height="238dp"
android:ScaleType="CENTER"

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

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