How to remove the space between rows of grid view in android - 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

Related

How to resize nestedlinearlayout in xml

I am trying to resize layout(l2) which is already in other layout(l1).Please let me know how to achieve it.
I want to do it xml.I dont want hardcode dp values such as 50 dp for l1 and 30 dp2.
Generally we use width 0r height as 0 dp and layout weight to partition according to our requirement.Is there any similar way like it.
One possible approach is nested linearlayouts with transparent views inside them (they do not show anything, just take place); layout_width="0" layout_weight="1" will do the job.
is this ok?
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="10dp"
></RelativeLayout>

Alignment of grid view in a layout to match all screen sizes

I'm making a hangman game and i have A to Z letters in my grid view in order to guess the word. The grid view is not aligning properly it is appearing in different sizes in different mobiles what i need is the grid view should match the height of the layout. Please help me to solve this
.This is the problem i'm facing Please see the image below
I have took a relative layout for placing the grid view in it and it appears like shown in image,for small screen mobiles its automatically providing scroll option to scroll,and in big screen mobiles it is just going above the margin.
Here is the code
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="270sp"
android:layout_marginBottom="0sp">
<GridView
android:id="#+id/letters"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:horizontalSpacing="5sp"
android:numColumns="9"
android:padding="5sp"
android:stretchMode="columnWidth"
android:verticalSpacing="5sp" />
</RelativeLayout>

Set Fixed Row Height in GridView

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.

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

Position of imageview

I want to place an image at the right of the screen width minus 20 dp on a android device.
The pseudo code for this could be:
Get Image from res/drawable folder.
Set position of image - this has to be the screen width minus 20 dp,it should calculate the screen width and then set the position ( Is this possible in xml? )
Render the image.
Parts 1 & 3 are clear, but the second part I guess is tough & is where I need some help.
You can use a RelativeLayout as a container. Set android:layout_alignParentRight="true" and android:layout_marginRight="20dp" on an ImageView within this layout.
You can get more information here.
My understanding is that you will need to do this in a Java class where you get the dimensions of the window, do your calculations and then set the position of the image within the onCreate() Method.
Yes, you can get it with xml too:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:layout_marginRight="20dp"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon" />
</LinearLayout>
Note that android:layout_marginRight="20dp" sets padding from right

Categories

Resources