I am making app with Android Studio and want to auto set width and height of image that I am showing on front page. my code is like this
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:id="#+id/linearLayout1"
android:orientation="vertical"
android:layout_height="fill_parent">
<TextView
android:id="#+id/textView"
android:layout_width="375dp"
android:layout_height="220dp"
android:layout_marginBottom="5sp"
android:adjustViewBounds="true"
android:background="#drawable/japuji"
android:clickable="true"
android:focusable="true"
android:linksClickable="true"
android:scaleType="fitXY"
android:textStyle="bold" />
this is the scrollview and when i set fill_parent, wrap_content in TextView its not working either, when i test it on big screen images are small ,
i have tried all like changing width to fill_parent and wrap_content
You should not display an image as a background of a view, instead, you should use ImageView
<ImageView
android:contentDescription="#string/imagedesc"
android:id="#+id/my_image"
android:src="#drawable/your_image"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
src : sets the image drawable
scaleType : sets the desired scale type, in this case fitCenter with adjustViewBounds set to true and width or height (one of them) to wrap_content will show the full image.
EDIT :
if you have a fixed view size and you want to cover it all with that image drawable, you should use centerCrop as a scaleType instead of fitCenter
You're apparently displaying the image (android:background="#drawable/japuji") in the TextView, but the TextView has its height and width hard-coded: android:layout_width="375dp" android:layout_height="220dp"
Instead set the TextView's height and width to match_parent.
Related
im my code
<ScrollView
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".3">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/json"
android:onClick="album"
android:scaleType="fitXY"
android:layout_width="fill_parent"
android:layout_height="140dp" />
<ImageView
android:id="#+id/json1"
android:layout_marginTop="3dp"
android:onClick="live"
android:scaleType="fitXY"
android:layout_width="fill_parent"
android:layout_height="140dp" />
</LinearLayout>
</ScrollView>
how can i set height of the imageviews independent for all size screens...
now it looks fine in mobile phones but on tabs, the imageviews insdie scrollview get stretched.
Those are stretched because of scaleType,
Set it to centerInside and it will work fine.
Three XML attributes are defining how your image appears on the screen.
Height
Width
Scale Type
You've set the height to 140dp, but set the width to be fill_parent and the scale type to fitXY.
Set the Image width to be 140dp or some other predefined value based on the Image Aspect Ratio or change the scale type.
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!
Use scaletype and add android:adjustviewbounds="true" in your image view section.
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
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
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.
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.
I am working on application which shows images, and I need to create a view which shows big images in a 1:1 aspect ratio.
First, I have thumbnails which are at a 72x72 resolution. I have put them in an ImageView but even if I add the parameters android:width="fill_parent" and android:height="wrap_content", the image would not be not the size of the ImageView but is only centered in the mid.
I tried adding the parameter android:scaleType="centerCrop" which resized the image well horizontally but the height remained the same.
How could I set up my ImageView so that it would be on all devices as width as the parent view and it should also be as height.
I can do this programatically, but I need to be able to do it in XML.
My XML File:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
// The ImageView I am talking about above
<ImageView
android:id="#+id/invitation_imageview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:scaleType="centerCrop"
android:src="#drawable/face_woman_smile" />
<ImageView
android:id="#+id/invitation_avatar_view_1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:src="#drawable/face_woman_smile"
android:scaleType="center"
android:layout_margin="1dp" />
</LinearLayout>
I think you need to add this to your ImageView
android:adjustViewBounds="true"
Use:
android:scaleType="fitXY"
To make image stretch to parent's width while keeping aspect ratio
Set
1- android:layout_width to match_parent
2- layout_height to wrap_content
3- adjustViewBounds to true
Like this:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:src="#drawable/unknown"
android:adjustViewBounds="true" />
Note: This might not render correctly in Preview but would work on runtime.
I usually use android:width="match_parent" and android:height="wrap_content", as you mentioned, but I use android:scaleType for my ImageView layouts.
You can read more about it here.
I have a list of Imageviews that have their width set by the weight of a linear layout. How can I set the height of the Imageviews to ensure that they keep the original aspect ratio of the bitmaps being loaded into the image.
Here is a portion of my xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ll_auction"
android:weightSum="100"
android:paddingBottom="10dp">
<ImageView
android:layout_width="0dp"
android:layout_weight="30"
android:layout_height="#dimen/auction_image_height"
android:layout_gravity="center"
android:background="#drawable/image_view_background_unselected"
android:layout_margin="2dp"
android:adjustViewBounds="true"
android:id="#+id/niv_auctionImage1" />
You can add an attribute called scaletype to the imagevie and set it to fitxy
Sample
android:scaleType="fitXY"
It will auto scale your image base on the wieght you specify in your attribute
If you are specifying a width through the layout weight, AND you want the picture the keep its aspect ratio, you need the height to then be wrap_content. Adjust view bounds is also correct, and I would use the scaleType fitCenter.
You also want to make sure you set the image to be scaled as the src, not as the background, which will always be stretched.
Give this a go.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ll_auction"
android:weightSum="100"
android:paddingBottom="10dp">
<ImageView
android:layout_width="0dp"
android:layout_weight="30"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/image_view_background_unselected"
android:layout_margin="2dp"
android:src="#drawable/example_image"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:id="#+id/niv_auctionImage1" />
I added an ImageButton into my relative layout, my xml code is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomePageActivity" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="132dp"
android:background="#null"
android:scaleType="fitXY"
android:src="#drawable/book" />
</RelativeLayout>
As log as the width and height are wrap_content, the image is clear, but when I specify a larger width and height like:
android:layout_width="80dp"
android:layout_height="80dp"
then, when running on the AVD the image button loses clarity.
Is there any solution??
By default ImageView/ImageButton will scale the image to fit center.
You can change that by using android:scaleType = "center". This will make image retain its original size and keep it centered.
If the image is not a picture of an object but just a rectangular shape, convert it to a 9-patch drawable.