Set Fixed Row Height in GridView - android

I have images that I want to render in a GridView. These images are all the same size (250x250). However, I want to display them in 100x100 squares in the GridView.
<GridView
android:id="#+id/sectionListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="100dp"
android:stretchMode="none"
>
</GridView >
The above is the grid view. Where I am setting the columnWidth to 100. This actually works but for some reason, the height is always 120, leaving horizontal black bars at the top and bottom of my 250px image. Somehow there are getting 10 pixels on the top and bottom. Changing the vertical and horizontal spacing doesn't help because that affects the spacing between the elements. These black bars on top and bottom are inside the gridView item.

I would look into the ImageView which I assume you would be using in your adapter to show the images. You should set android:layout_width and android:layout_height to 100dp and make sure that android:scaleType is set to centerCrop.

In the CustomAdapter
view.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, rowHigh));
rowHigh is the dimension you must to change

If you are using gridview, then I think you are also using another item_layout for displaying each item, I'm guessing it is an ImageView as you are trying to display images. Hence you can limit the size of the items to 100 x 100 in that item layout(ie, android:layout_width="100dp" android:layout_height="100dp"in the item_layout.) Hope it helps.

Related

Set dynamic width and height of item in recyclerview

I am working on a android project in which I am using Recyclerview withgridlayoutmanger and Items are Images. I want to display four items in single row so I have set spancount to 4 and a gap of 2dp is set between each item with Itemdecoration.
ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(getContext(),R.dimen.offset);
fRecyclerView.addItemDecoration(itemDecoration);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),4);
Each Item height and width are currently 90dp.
Item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/imagevideo_layout"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/image_video_pic"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
/>
</RelativeLayout>
Problem is as I am fixing the height and width of item, in some devices due to the screen size differences there is more gap between each item.And in some devices items are overlapping because of small screen sizes.I need only 2dp and only 4 items in single row for all the screen sizes. Is there any way to do this? Is it possible to dynamically set the relativelayouts height and width depending on the screen size? I have searched in SO, questions are on setting the spancount dynamically by fixing the height and width of each item, but I need reverse of it.

Linearlayout background image

I have a problem with Linearlayout background. The issue is that I am setting LinearLayout background as image. Lets say image size is 300 px height. This LinearLayout holds other views lets say rows. One row is 100 px height. The problem is that when I am adding a row to this LinearLayout, row fill only 100 px of 300 px. The question would be is it possible to strech or scale LinearLayout depending on how much it's children views take place?
The code of LinearLayout:
<LinearLayout
android:id="#+id/companies_list_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:background="#drawable/firm_list_background"
android:orientation="vertical" />
Your question is not so clear so:
First of all when you set an image as the background of a layout it is scaled to fit or cover the whole size of the layout.
Another method to do the same think is to add an imageview in your layout with android:layout_width="match_parent"
android:layout_height="match_parent"
and set its background resource to android:scaleType:"fitXY"
So:
1: You have set your companies_list_holder height to wrap_content so if a row is 100px height. it also has 100px height so the background is scaled to fit your 100px height layout.
2: If you had set your layout to have a 300dp height it would be scaled to fit your 300dp layout etc etc. then you could add your 100dp height child rows.
Finally i think that if you want to add multiple and unknown number of rows in a layout, the best solution is a ListView like in this example:
http://www.mkyong.com/android/android-listview-example/
and your listView would handle many thinks like scrolling etc
You can first get the height of your inner row and then set this height as height of your LinearLayout using LayoutParams

Equal sizes of Pictures GridView in Android

I want to display 2 images in a Row in GridView.
I have tried to get the screen size in different ways. For example:
final int h = getResources().getDisplayMetrics().densityDpi;
and set size of the image..
convertView.setLayoutParams(new GridView.LayoutParams(h, h));
But this is not sufficent. If screen is large, then 2 images look very small - like peanut, on a big screen.
Can anyone guide me that for example. Device's screen size is 4 inches in Portrait and I can create 2 images of almost 2 inches each.
here is my gridview xml file
<GridView
android:id="#+id/grid_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alwaysDrawnWithCache="true"
android:clipChildren="true"
android:gravity="center_horizontal"
android:horizontalSpacing="2dp"
android:verticalSpacing="5dp"
android:numColumns="auto_fit"
android:padding="0dp"
android:scrollingCache="true"
android:smoothScrollbar="true"
android:stretchMode="columnWidth"
>
</GridView>
add these attributes to your grid
android:columnWidth="200dp"
android:stretchMode="columnWidth"
It sounds like you're trying to change the images size based on the screen size, if so you should take a look at the supporting multiple screen sizes tutorial. Android already offers that functionality.
Also if you're only displaying two images horizontally you could use a LinearLayout instead, using a GridView is overkill.
Just set the number of columns of the GridView in XML.
To make equal sized images, make the ImageView like this SquaredImageView
and if you want Layout to be of equal size then follow the same method describe on onMeasure of SquaredImageView

Scrollview + scaling images = doesn't scroll properly, why not?

I have an image that I scale to the width of the phone/tablet. It is 224x1632. The imageview is within the scrollview, and I need to be able to scroll up and down on that image.
The issue is that the scrollview sets according to the image size (1632 in length) on create, but when the image scales/stretches it is 3x taller than the original. Now the scroll-view is too small to scroll the entire image.
Any way to make the scrollview fit the image?
Note: the image length will differ per phone, so I can't set it to a predetermined size.
XML Code:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:scrollbars="vertical"
android:src="#drawable/tiles" />
</ScrollView>
You set your ScrollView's height to wrap_content which means "be as big as my content." You also set your ImageView's height to fill_parent which means "be as big as my parent." Either one of these statements is enough to prevent scrolling. Your ImageView should be wrap_content and your ScrollView should be fill_parent.
Try scaleType="center" instead of "centerCrop"

How to remove the space between rows of grid view in android

In android grid view there is extra space between the rows of grid view. I am not adding any space to the gridview.
here is my xml files
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:horizontalSpacing="5px"
android:numColumns="3"
android:columnWidth="50dip"
android:verticalSpacing="0dip"
android:gravity="center"
/>
and the other xml for the imageview which is adding view to the gridview is
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/singlePhoto"
android:layout_gravity="top"
android:layout_width="145dip"
android:layout_height="145dip"
android:layout_marginTop="0dip"
android:layout_marginBottom="0dip"
>
</ImageView>
In Adapter class the code is
if(mView == null){
mView = mLayoutInflater.inflate(R.layout.newsgridthumbpic, null);
}
ImageView mImage = (ImageView)mView.findViewById(R.id.singlePhoto);
PhotoGridInfoSet mInfo = (PhotoGridInfoSet)details.get(position);
if(mImage != null){
mImage.setTag(mInfo.getPhotoThumbNailString());
mImage.setVerticalScrollBarEnabled(true);
mImage.setVerticalFadingEdgeEnabled(true);
}
}!
The space is between the rows of gridview. I have not added any space anywhere in the code Still the there is vertical spacing.I am not able understand why there is space in between gridview rows. This space is not appearing in the hdpi device/emulator. This problem is in the mdpi and ldpi devices/emulator.
I got the solution for this problem.
To solve this i need to add 1 line in my xml file
android:adjustViewBounds="true"
and after adding this. the space is not appearing now
Just put last 2 lines in gridView
<GridView
android:numColumns="2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/gridView"
android:horizontalSpacing="0dip"
android:verticalSpacing="0dip"/>
Put the following line in ur getView() method...
mImage.setPadding(0, 0, 0, 0);
I hope this will help you.
It looks like your picture is being scaled to fit the column width of 50dp while maintaining the aspect ratio. This is introducing a bunch of unfilled space in the vertical layout. If you want your column to be 50dp wide, the easiest way to work out these spacing issues will be to adjust your imageview to have a width of 50dp and adjust the imageviews height proportional to that.
just check that your "newsgridthumbpic" layout does not contain the extra padding or any background image having a large height that your grid view element.Also double check that you are supplying column_width as 50dip but your "imageview's" width is 145 dip.
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:horizontalSpacing="5px"
android:numColumns="3"
android:columnWidth="50dip"
android:verticalSpacing="0dip"
android:gravity="center"/>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/singlePhoto"
android:layout_width="145dip"
android:layout_height="145dip"
android:scaleType="fitXY">
</ImageView>
Setting android:verticalSpacing in the gridview to negative values.
will remove the vertical space.
(Eg: android:verticalSpacing="-10dp")
test with some number to get the right value
I have another way as a fixing for the same issue while one using RecyclerView.
Just pass the number of SpanCount like I have given 6 in GridLayoutManager(this, 6) and change the SpanCount to reduce or increase the gap between the items in the RecyclerView.
Check this sample code-
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 6);
rvSizes.setLayoutManager(gridLayoutManager);
SizeSelectorAdaptersizeChooserAdapter = new SizeSelectorAdapter(this, sizes);
rvSizes.setAdapter(sizeChooserAdapter);
Hope this will be useful.
Thank you

Categories

Resources