I use match_parent for height,but I don't want to use wrap_content or fill_parent for width. I want to set height as same as width. How can I do that in layout.xml?
Your question largely depends on whether you are in portrait or landscape.
If you are trying to make a layout width to match the height in portrait, then your layout will be going off screen, which does not seem possible in xml. And vice-versa, matching the height to the width in landscape would make the layout go offscreen as well.
If you are trying to make a square layout the same dimension as the smallest dimension of the screen, then a round about way to do that in xml is to make a shape drawable that is square, transparent and larger than your screen size. Then set the drawable to be an imageview inside a relativelayout and set your width and heights to the proper settings (match_parent or wrap_content). Then add views to the relativelayout as needed and reference them to the parent and not the image, making the new views reside "on top" of the imageview.
In portrait, the following relativelayout is square:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:src="#drawable/square_draw" />
</RelativeLayout>
</LinearLayout>
In landscape, the following relativelayout is square:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:src="#drawable/square_draw" />
</RelativeLayout>
</LinearLayout>
Also, make sure that adjustViewBounds is set to true. For some reason, this solution does not allow the image to grow larger than the screen size and it also uses more resources due to the overdraw.
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 am struggling with newbie question.
I have a layout defined like this: (it is used as listview item)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="horizontal" >
<ImageView
android:id="#+id/articleSmallImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:adjustViewBounds="true"
android:scaleType="fitStart" />
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_weight="2"
android:orientation="vertical" >
<TextView
android:id="#+id/newsIntroText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="TextView"
android:textColor="#color/black" />
</LinearLayout>
</LinearLayout>
What i want to achieve is this:
Parent layout height to resize according to the highest child. In this case if TextView is higher then ImageView, ImageView aspect ratio should be intact, with max width possible, and if ImageView i higher then TextView, again, width should be maximal, and parent should have height as ImageView.
This is actually screenshot from my app, running on JB 4.3. and is looks as expected. But on ICS 4.0.3, 4.0.4 it looks like this.
I cannot see any reason why ImageVIew is not filling that blank space.
Help..
Try the value fitXY for scaleType. Btw unless I'm missing something, I think your LinearLayout1 is useless. Also, like YouYou said.. If a weight has been set in a vertical layout, the height should be 0dp and in a horizontal linear layout, the width should be set to 0dp for better performance.
I have a vertical linearlayout that won't stretch beyond a certain limit.
This is the layout with centerCrop in the imageview
http://tinypic.com/r/20nm6s/5
This is the layout with no crop set (so it should be full width and huge)
http://tinypic.com/r/vzk7kw/5
So my thought is that there is some implicit max height that I'm not seeing in my layout but I can't see where it is, can you spot my error?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/dropshadow"
>
<TextView
android:id="#+id/heading"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
<ImageView
android:id="#+id/featuredimage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="1000dp"
android:scaleType="centerCrop"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
</LinearLayout>
</RelativeLayout>
The "implicit" max height of the layout is the height of its parent. Since you're using wrap_content on both layouts, that means the parent is effectively the screen area, minus whatever other views you're using (such as the TextView). Unless you place it in a scrolling container, such as a ScrollView, it won't ever exceed the size of the screen.
The reason your ImageView isn't showing up "full width and huge" when you remove the crop is because the default scaleType for an ImageView is fitCenter. This particular view is bounded by the layout's width, so it shrinks the image while maintaining aspect ratio.
I have an activity where a user can choose a photo through an intent. The expected behavior is as follows:
The image is sized to fill the parent width (minus margins).
This sizing should never increase the image, just scale down when needed.
Aspect ratio should ALWAYS be accurate.
Height should be scaled exactly as width is to maintain correct aspect ratio.
In portrait mode the maximum height is however big it can get before scrolling appears (just fill the screen).
This means that the width may not fully stretch to parent width if limited by the height.
In landscape mode the maximum height is irrelevant because scrolling is allowed, width should always be parent width.
Here's the portrait XML layout I am using:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/gray_background"
android:weightSum="1" >
<!-- Other controls here -->
<ImageView android:id="#+id/imgView"
android:layout_gravity="center"
android:layout_margin="5dip"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:contentDescription="#string/ImageToSend"
android:adjustViewBounds="true"
android:layout_weight="1"
android:scaleType="fitCenter" />
<!-- Other controls here -->
</LinearLayout>
Here's the Landscape XML layout I am using:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<!-- Other controls are here -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/gray_background" >
<ImageView android:id="#+id/imgView"
android:layout_gravity="center"
android:layout_margin="5dip"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:contentDescription="#string/ImageToSend"
android:adjustViewBounds="true"
android:layout_weight="1"
android:scaleType="fitCenter" />
<!-- Other controls are here -->
</LinearLayout>
</ScrollView>
This works perfectly fine no matter what view I am in. The problem I am having is that when the user rotates the device, the ImageView no longer has the correct size. If I start by selecting the image in landscape mode, rotations look fine. If I start by selecting an image in portrait mode, then switch to landscape mode, the image now displays as 1px by 1px.
Any ideas anyone?
After a while of failed expiraments, I found that calling setContentView() and redoing the layout fixed the issue. Not sure if this is what I was supposed to do, but it has fixed it.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_x="0dip"
android:layout_y="0dip"
android:background="#drawable/mlview1" />
I'm using android emulator 2.2 . My image occupies the screen horizontally but vertically shows a large gap from the Bottom. The image size is 800X600 . should it be re-sized and then used?
Make sure that:
1. Both height and width on both parent and child is fill_parent
2. You are setting the src property and not the background one on the image
Note: You also don't need the layout_x nor layout_y