I have an application in which I am using a GridView to show some image of different number. I want to set the GridView in the center of screen horizontally that is to be run perfect on every phone of different sizes or density pixels.
Thanks in advance.
code
<GridView
android:id="#+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp" >
</GridView>
your 'GridView' must be in some parent layout for example Linearlayout.Set following two properties of that parent layout
android:layout_gravity="center"
android:gravity="center"
Note:You must use both attributes at the same
Related
i'm using the gridview to display the images.
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="96dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >
</GridView>
and the imageview is
<ImageView
android:id="#+id/imageView1"
android:layout_width="96dp"
android:layout_height="96dp"
android:background="#drawable/image_bg"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher" />
Note : I dont want to hardcode the numbers of images to display in single row of gridview because if the screen orientation changed the number changes
When i'm loading m getting the space bewteen the images in the gridview as the below picture
But i dont need the space i want the gridview to appear like the native gallery
Change layout_width
android:layout_width="match-parent" instead of "96dp"
Try using below tag in GridView
android:verticalSpacing="10dp" // changed this value
android:horizontalSpacing="10dp"
For reference HorizontalSpacing and VerticalSpacing
add this to your gridview xml :
android:listSelector="#android:color/transparent"
and also try myGridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
I'm trying to put inside a LinearLayout Horizontal some buttons, but I have the next result.
When they get to the right of the screen, they are not sorted under, they go on. I want that if the screen is small, they are ordered as follows.
Without cut any button.
Thank you. If you need my XML, tell me.
Try setting the LinearLayout width as match parent
android:layout_width="match_parent"
android:layout_height="wrap_content"
Hope it helps :)
Update:
Use GridView and set num_coulums to auto_fit and keep adding buttons as you desire,
set width & height as per your requirements
<GridView
android:id="#+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >
</GridView>
I have a GridView , which displays pictures of alphabets
what happens is , every time in the last row there are less no of alphabets.
and the last row is left aligned , which doesn't look good
so I want make the last row to be in centre,
any suggestion
My GridView code:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_view"
android:layout_width="fill_parent"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="70dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="20dp"
android:gravity="center"
android:stretchMode="spacingWidthUniform" >
</GridView>
I have the same problem, but I managed this using two gridviews, with first gridview displaying the rows except the last row, with the second gridview displaying the last row. This is the simplest way to achieve what I need.
I have not find a better solution. Looking forward to see a nice and simple way also.
you could try using invisible elements, it not the most efficient solution but is simple, you can put two elements, one at the start, one at the end of the row and make them invisible
For those who are still looking for a solution for this here's my take for this, create a TableLayout and, and for each TableRow set its weigth to android:weightSum="4" and dont forget to add android:gravity="center"
For complete code check this gist for reference.
You could center the grid row inside of a relative layout and then add android:layout_centerHorizontal="true" to the alphabet ImageView's. Assuming that they're ImageView's.
So your GridView would be wrapped in something like this:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_cotent"
android:orientation="horizontal" >
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_view"
android:layout_width="fill_parent"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="70dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="20dp"
android:gravity="center"
android:stretchMode="spacingWidthUniform" >
</GridView>
</RelativeLayout>
And the individual alphabet images would need android:layout_centerHorizontal="true" like this:
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
</ImageView>
Following is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridviewgallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="#dimen/gridview_column_width"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:background="#444444"
/>
When I use android:stretchMode="columnWidth" , it creates a gap between the columns(though it fills the screen width)
And if I use android:stretchMode="none" , it gets left aligned and there is a gap left on right side of screen(though now there is no gap between columns)
Problem is I want both : To fill the screen width as well no gap between columns.
How to achieve it?
A combination of android:numColumns="auto_fit" android:stretchMode="columnWidth" and android:columnWidth="60dp" will give you a layout that adjusts for different screen sizes. Much better than fixing the number of columns. See example below...
<GridView
android:id="#+id/tablegrid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffff7965"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:gravity="center"
android:columnWidth="60dp"
/>
It's very simple dude, just remove the line
android:columnWidth="#dimen/gridview_column_width"
you can specify any number of columns, i am given number of column = 4
android:numColumns = "4"
This is working fine for me
I know this question is a little old but here is an answer I found hoping this helps someone else, just for android:stretchMode use spacingWidthUniform as it says in documentation here it will stretch the spacing between columns equally.
please refer to above link for more information.
Is it possible to set borders to grid view in xml. Like, i want each grid cell to contain borders. If so, kindly let me know how to do it. I have set this way. But i am not finding any specific thing to set border.
<GridView
android:layout_marginTop="95px"
android:layout_marginLeft="32px"
android:id="#+id/gridview"
android:layout_width="340px"
android:layout_height="250px"
android:columnWidth="35px"
android:numColumns="7"
android:scrollbars="none"
android:verticalSpacing="14px"
android:horizontalSpacing="18px"
android:stretchMode="columnWidth"
android:gravity="center_horizontal"
android:background="#color/white">
</GridView>
Thank you
Swathi