im trying to get images displayed in a GridView and get the columns automatically set, so far I've had to manually set the number of columns which is not what i want to do as this will affect how big the images are on different sized screens. I ahve tried setting it to auto_fit but it only displays 2 columns in the middle of the screen. This is what im trying to achieve: (Each red square represents an image)
then when turned to landscape mode i want the columns to auto fit so that its all even.
Any help would be much appreciated, thank you :)
Experiment with the GridView attributes, specifically android:numColumns="auto_fit" and android:stretchMode. The following works for me:
<GridView
android:id="#+id/myGrid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
I'm not sure why someone voted down the answer above me (so I voted it up again): I had to solve a similar problem programatically (with a GridView of ImageView children). Some code removed for clarity:
int iDisplayWidth = getResources().getDisplayMetrics().widthPixels ;
iImageWidth = iDisplayWidth
/ iNumberOfColumns ;
gridview.setColumnWidth( iImageWidth );
gridview.setStretchMode( GridView.NO_STRETCH ) ;
/* wrap_content in the xml file is supposed to do this, but it didn't seem to work */
Best regards,
Piesia
A better solution is to measure the width programmatically, so you won't end up with a hard-coded column width:
Android: How does GridView auto_fit find the number of columns?
Related
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.
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
I want my main menu be a GridView of icons - 6 icons to be specific.
When the phone is in the portrait layout i want them to be in 2 columns, when it is in horizontal layout - 3 columns --- have sorted that out.
What I didnt sort out is how to make them occupy merely the whole screen.
Fitting it to the screen width is not a problem - it is fitting it to the screen height that makes me stumble.
I want it to stretch/shrink my cells depending on the screen size. I have tried counting screen height and dividing it by 3 - but there are certain problems and I dont think that that is the most elegant solution.
So basically how do I occupy my screen (say in a portrait layout) with a gridview of two columns (3 rows) solely? Are there any xml attributes that could help me?
This is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:gravity="center"
/>
I suggest creating the two layouts in xml first, then during the onCreate method you can do the re-sizing of the rows and columns.
For example
if layout is portrait
set column width = screen width / 2
else if layout is landscape
set column width = screen width / 3
Android Dashboard Pattern the answer to my question is somewhere there. Basically I used nested ListViews
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
i'm using gridview in android and i want to set the first column's width but i could not. while i am trying to find the answer to the problem i came face to face with a comment as you can see below. My question is that is description below really true?
All columns in a GridView have the same size.
On Sat, May 23, 2009 at 9:25 PM, solid wrote:
I am having trouble controlling the size of the columns in my
GridView. The first column is only 3-4 chars and the other 4 columns
are 5-8 chars. For some reason, all the columns are the same width,
leaving a huge amount of spacing after the first column and wrapping
the text inside the others. Is there a way, I can set the width of
individual columns in the grid view? In an ideal world, gridview
would work like an html table, where the smaller columns are shrunk to
fit the data and larger columns expand as necessary. Any ideas would
be helpful.
You can directly write in your layout resource file.
android:columnWidth="100dp"
https://developer.android.com/guide/topics/ui/layout/gridview.html
try this may this helps you
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>