Insert image in android and for all screen sizes - android

I want to add a image for my android app.
I want to add image like this
ImageView
android:layout_width="match_parent"
android:layout_height="321dp"
android:src="#drawable/welcome"
it need to cover full width ow screen and it height need to be 1/3 screen (i need to add few button bellow this image)
but when i simulate it i did't get my expectation.It will show middle of screen and it does't apply to full width
How can i solve this problem
Thank you

Thank you friends
I tried something like this
I got my results
ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/welcome"
Thank you

Please ensure that the image you're using is a 9-patch image. That'll get the thing done. When making a 9-patch image make sure you're enabling it to stretch horizontally.

Related

Remove whitespace from image in Android Studio

As one can see in the above image there is a lot of white space around the small image in this ImageView layout. How do I remove the white space and make the image view the same size as the actual image it is displaying? If I make the ImageView smaller, it makes the view smaller but still has white space around the image itself. How do I remove it?
Thanks.
add this XML Attribute to the image view declaration:
adjustViewBounds="true"
For example, your ImageView XML would look like this:
<ImageView
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
You simply need to do
android:layout_width="wrap_content"
android:layout_height="wrap_content"
in image view.
In addition to that, u can do below things for good practices
you can use ImageView.ScaleType(FIT_XY) but this will change ration of your image and x,y axis expands independently fill your image view
u need to add android:scaleType="fitXY" in xml file image view for it.
you can use ImageView.ScaleType(FIT_CENTER) it will keep ration and expan x,y until one fill your image view
u need to add android:scaleType="fitCenter" in xml file image view for it.
you can find all of the options by navigating this https://developer.android.com/reference/android/widget/ImageView.ScaleType
I've researched some fixes and none of them worked. What did work was changing from a mipmap to a drawable image. I'm getting other errors, but at least the original problem has been fixed.

Android Full Screen Image Handling

I'm trying to display an image but for some reason it's only giving me a smaller (unreadable) version. Is there a way to display the image full screen and enable zooming in/out? I used a fragment to display the image--do I need to create another class to extend the particular fragment...? Here is the imageview:
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:scaleType="centerInside"
android:src="#drawable/rules" />
If the image is not appering in the full screen, probably there is an issue with the ImageView container. The ImageView may be "filling the parent" but the parent is not filling the screen.
Other issue may be the scaleType, are you sure your image is the correct size? If you want to stretch it you have to change the scaleType.
How about this :
Get screen dimensions in pixels
and then something like this (IF you want to resize the image):
Android image on tablets and phones
This is not a complete answer to your question but the programming should be straightforward once you know the size of the screen and the size of the image.

Android XML ImageView

I've been trying to find an answer to this all of last evening with no luck so I decided to come ask here. I just started getting into front end dev for Android apps and I'm trying to do something really simple that just doesn't work. All I want to do is add an image on the screen and be able to resize it EXACTLY what size I want regardless of proportions. This is the code I'm currently using inside a relative layout:
<ImageView
android:layout_width="400dp"
android:layout_height="200dp"
android:id="#+id/imageView"
android:scaleType="fitXY"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="#drawable/statsus_logo" />
My image is a horizontal rectangle. When I first add it, it shows up inside a small square centered with a bit of padding all around. When I make the width 400dp that square appears to strech almost 100% across the screen however the logo stays the EXACT same size, centered vertically and horizontally inside this imageview container. When I increase the height of the imageview, the logo increases its size but almost as if it was taking the height and using it as a width. I feel like what its trying to do right now is use the height as its width and the only time when its width is the value i put in, is if the height is also the same value and even then there's some extra unwanted padding.
Again, all I want is for this damn image to be the size I tell it to, so if I want it 5dp wide and 100dp tall, it does just that. Can anyone please help? Thank you very much in advance.
Try to add this attribute to your imageview
android:scaleType="fitXY"
Scale type reference
android:scaleType="fitXY" is the way to scale your image however you want without keeping your proportions however my issue was a little bit different... I'm using android studio and for some reason the settings I was using to "add a new image asset" was making my image act the way I described it above... if I just add an image manually by just click and dragging into the folder, then the image works the way I want it... so long story short, the issue was with how I added my new image asset
thanks to everyone for all of your answers and help!

how to keep aspect ratio with different android devices

So I'm trying to put an image inside a scrollview and have the image stretch to fit different sized screens. It'll stretch horizontally, but vertically it always comes out messed up.
I've read so many pages of people with similar problems, but their solutions never work for me. I've tried every different combination possible using different scaleType,layout_width and layout_height, along with trying changing other things around. The closest I've gotten to getting it to work is shown in the code below, using scaleType="centerCrop". It's the only one that stretches the image vertically in ratio, but it cuts off a big chunk of the image from the top and bottom, it'll scroll up and down the image, but only the middle part shows.
The image is a 480x5500 jpeg, if that matters. Originally before I started messing with all that, the app worked just fine on my phone, but then later I realized the image was crunched when I tried it on a tablet. There's gotta be a way to do this in the xml right? I really don't want to have to do things with the image in the java code part. Also I'm hoping to be able to do this using just one image, I don't want to have use a different image for different screen sizes. Please help, thanks.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/paper" />
</ScrollView>
may be this help you,
make both height and width wrap_content of ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
and no need ScrollView here.
Try using custom ImageView like AspectRatioImageView mentioned here:
https://stackoverflow.com/a/4688335/944070
the next sample is more than what you ask for :
http://www.androidviews.net/2012/11/photoview/
https://github.com/chrisbanes/PhotoView/tree/master/sample
https://play.google.com/store/apps/details?id=uk.co.senab.p
hotoview.sample
it fits the image to the screen , plus it allows to zoom in/out using pinching gestures.

Change image on click and make it fit in an ImageView

I currently working on my personal project in Android. I'm quite a newbie in android development well it's been a months since I've started to self study this but I just can't focus on this one. Well, so here's my problem, first I will discuss what I've made so far:
I've placed a 6 ImageView for each tableRow (six table rows) in a table layout which make it a 6x6 ImageView.
each of the imageView has a default image icon (the default icon from the android drawable).
Once I clicked the ImageView I will change it's Image using setImageDrawable.
the default icon of the android is sized smaller than the images I've placed in which I think 36x36 pixels is the default icon (the android default) wherein my images are in size of 80x80 pixels.
Now here's the problem:
Once I've clicked the image it will successfully changes the image but will also resize the imageView into the size of my image(80x80 px).
well how do I get over this? I've tried setting the ImageView's scale type into fitXY but it has done nothing still my the imageView is resized.
I've also tried changing the wrap_content into fill_parent but nothing also happens (well I just think that it may solve the problem lol)
Here's the property of my ImageView:
<ImageView android:scaleType="fitXY"
android:id="#+id/imageView1"
android:src="#drawable/image0"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</ImageView>
Any help please? Thanks in advance!
you can set the fix size for the image view into the xml file or by coding through for e.g. here in xml file
<ImageView android:scaleType="fitXY"
android:id="#+id/imageView1"
android:src="#drawable/image0"
android:layout_height="90dp"
android:layout_width="120dp">
</ImageView>
so all image are display with the size 120*90 so when you change the picture it will fit into this imageview

Categories

Resources