Android : Child layouts occupying the whole width of screen - android

I have a linear layout as a parent layout with three child layouts and I am trying to show the images in child layouts by fetching from webservice.
Some of my images are occupying the whole width of the screen and It should not happen. how to avoid my child layouts occupying the whole width of the screen??
here is my code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:layout_weight="9">
<LinearLayout
android:id="#+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="left"
android:layout_alignParentLeft="true"
android:orientation="vertical"/>
<LinearLayout
android:id="#+id/two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:layout_toRightOf="#id/one"
android:orientation="vertical"/>
<LinearLayout
android:id="#+id/three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="right"
android:layout_alignParentRight="true"
android:orientation="vertical"/>
</LinearLayout>

you have applied weight to your Linear layout,for weight to be applicable change the layout_width to 0dp

There are multiple ways of doing this
Add the imageviews in the layout and assign them width+height in dp
like so
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:layout_weight="9">
<LinearLayout
android:id="#+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="left"
android:layout_alignParentLeft="true"
android:orientation="vertical">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/imageView"
android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
.....
<LinearLayout
.....
</LinearLayout>
Play around with the scale values in
ImageView.ScaleType
CENTER
Center the image in the view, but perform no scaling.
CENTER_CROP
Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding).
CENTER_INSIDE
Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding).
FIT_CENTER
Scale the image using CENTER.
FIT_END
Scale the image using END.
FIT_START
Scale the image using START.
FIT_XY
Scale the image using FILL.
MATRIX
Scale using the image matrix when drawing.
find more details here

Related

Android: Layout proportional scale

I want to simulate microcontrolers screen (800x480) on android devices. Screen content is buttons, labels, graphic primitives(line, rect, ...), images.
What to do to fit layout (scale up/down) on all screens?
Here is layout xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="800px"
android:minHeight="480px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rootLayout"
android:gravity="center">
<LinearLayout
android:orientation="horizontal"
android:minWidth="800px"
android:minHeight="480px"
android:layout_width="800px"
android:layout_height="480px"
android:id="#+id/mainContainer">
<NSU.Droid.MyWindow
android:minWidth="714px"
android:layout_width="714px"
android:layout_height="match_parent"
android:id="#+id/mainWindow"
android:background="#drawable/window_shape"
android:layout_margin="1dp" />
<LinearLayout
android:minWidth="86px"
android:layout_width="86px"
android:layout_height="match_parent"
android:background="#drawable/sidewindow_shape"
android:layout_margin="1px"
android:scrollbars="none">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<NSU.Droid.SideWindow
android:orientation="vertical"
android:minWidth="86px"
android:layout_width="86px"
android:layout_height="wrap_content"
android:id="#+id/sideWindow"
android:scrollbars="none" />
</ScrollView>
</LinearLayout>
</LinearLayout>
NSU.Droid.MyWindow is RelativeLayout.
If px is used, simulated layout is too small, about 1/3 on my device screen. Changing px to dp does not help - view is too big, I can see about 2/3 of layout content.
How to scale layout and content proportionally?
Use match_parent for android:layour_width or android:layout_height attributes to fit screen with layout.
To make items sizes proportional, you can use android:layout_weight attribute in LinearLayout.
For example:
<LinearLayout
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="horizontal">
<FrameLayout android:layout_width="0dp" android:layout_height="match_parent"
android:background="#ff0000"
android:layout_weight="3" />
<FrameLayout android:layout_width="0dp" android:layout_height="match_parent"
android:background="#0000ff"
android:layout_weight="1" />
</LinearLayout>
If you have horizontal orientation of your LinearLayout, you should set android:layout_width attribute to 0dp to elements of this LinearLayout to which you want apply android:layout_weight attribute. If orientation of your LinearLayout is vertical then android:layout_height attribute should be set to 0dp.
The XML above should look like this:

Scale my item image to fit the screen properly

So I got a problem scaling my image to fit my home screen appropriately.
Here are some screenshots.
Height problem
Width problem
I want to achieve something like this
XML Item code
<?xml version="1.0" encoding="utf-8"?>
<com.inthessaloniki.cityguide.view.SelectorRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_poi_list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="#drawable/selector_clickable_item_bg_inverse">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/fragment_poi_list_item_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"/>
</RelativeLayout>
</com.inthessaloniki.cityguide.view.SelectorRelativeLayout>
I want the images to match the parent width but scale their heigth accordingly.
The scaleType="fitXY" forces image to fix to screen and does not keep aspect ratio.
Try this instead
<ImageView
android:id="#+id/fragment_poi_list_item_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:adjustViewBounds="true"/>
In the end I found out a solution.
Here it is in case someone had the same problem.
I used scaleY and padding to fit the images accordingly.
<ImageView
android:id="#+id/fragment_poi_list_item_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="7dp"
android:paddingBottom="7dp"
android:scaleType="fitXY"
android:scaleY="1.25"/>

Android layout_height to wrap smallest child

As part of a larger project, I'm attempting to create a custom image gallery which will hold images from the web. I have no control over their size and their layout (order, total number [up to ten], and number per row [up to three]) is defined by the API I'm working with. I'm using nested LinearLayouts to build the loose grid programmatically but this is an XML proof of concept...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:divider="#drawable/photoset_vert_divider"
android:showDividers="middle">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:divider="#drawable/photoset_horz_divider"
android:showDividers="middle">
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:layout_height="wrap_content"
android:src="#drawable/tall" />
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:layout_height="wrap_content"
android:src="#drawable/tall" />
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:layout_height="wrap_content"
android:src="#drawable/wide" />
</LinearLayout>
</LinearLayout>
This is 60% of what I'm looking for and gets me
…but this is my target:
What I want is a android:layout_height="wrap_smallest_content" sort of thing that scales all the ImageViews to the correct size and then crops them all down to match the "shortest" child.
Since you say you're going to eventually do this programmatically, you could always just subclass LinearLayout to find the smallest height of the containing ImageViews as they load and then set that as the layout's height. Then all you'd have to do with the images is set their height to match_parent.
It's really better to set the fixed layout_height to inner LinearLayout. You can just do this in XML
If you're focusing on variety of devices then give a id to the inner LinearLayout then set the LinearLayout size dynamically by measuring the screen size of the device. To measure the screen size programmatically
By using the DisplayMetrics you can get height and width of the screen of any device.
here is the code.
DisplayMetrics metrics;
int width = 0, height = 0;
In your onCreate Method
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
xheight = metrics.heightPixels;
xwidth = metrics.widthPixels;
Try this to get the Screen height.
Then set the inner LinearLayout programmatically like this
linearLayout.getLayoutParams().height = xheight - 50;
Depending upon the xheight change the - 50 value
the your xml will change to something like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:divider="#drawable/photoset_vert_divider"
android:showDividers="middle">
<LinearLayout
android:id="#"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:divider="#drawable/photoset_horz_divider"
android:showDividers="middle">
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:layout_height="match_parent"
android:src="#drawable/tall" />
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:layout_height="match_parent"
android:src="#drawable/tall" />
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:layout_height="match_parent"
android:src="#drawable/wide" />
</LinearLayout>
</LinearLayout>

Dynamic re-sizing of images according to the sizes(Same width but different height)

I would like to dynamically resize my images taken from camera in android. I want an output like the pinterest images.
You can set the ImageView's width to your custom size, its height to wrap_content and the important thing is to set the ImageView's scaleType to fitXY
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/yourImage1"/>
<ImageView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/yourImage2"/>
</LinearLayout>

How to view images in GridView with its original dimensions

I want to display a set of images in GridView (They have the same dimensions).
Below the layout for the GirdView and the GirdView Item ..
Teh problem is that the image height is stretched. why ?
GridView layout :
<GridView
android:id="#+id/gvRecipes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_below="#+id/include1"
android:layout_margin="0dp"
android:background="#drawable/bg"
android:gravity="right"
android:horizontalSpacing="10dp"
android:padding="0dp"
android:verticalSpacing="5dp" >
</GridView>
GridView item is :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/include21"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="#drawable/btn"
android:padding="5dp" >
<ImageView
android:id="#+id/imgRecibeImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/img1" />
<TextView
android:id="#+id/txtRecibeName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_gravity="bottom"
android:background="#80000000"
android:gravity="right"
android:padding="5dp"
android:text="Crispy Chicken and Sandwich"
android:textColor="#ffffff"
android:textSize="17sp" />
</FrameLayout>
In your scale type you have used fitXY:
fitXY: This one should only be used if disproportionate scaling does not affect the graphic. In the case of bitmap graphics, this is almost always bad to use. This sets the width of the source image to the width of the view, and the height of the source image to the height of the view, without maintaining the aspect ratio of the source image.
Now your layout and imageview width is match_parent, so the image will be shown in full width of your parent view, but for height it is wrap_content so the imageView will adjust the actual height of your each and every image.

Categories

Resources