GridView and a central column - android

What I want is a central column using GridView, but everything I try does not work. It always aligns to the left. I have tried messing around with layout_gravity and gravity to no avail.
I have a GridView in a LinearLayout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<GridView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:numColumns="1"
android:columnWidth="100dp"
android:stretchMode="spacingWidth"
/>
</LinearLayout>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical">
<GridView
android:layout_width="5px"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:numColumns="1"
android:columnWidth="100dp"
android:stretchMode="spacingWidth"
android:layout_gravity="1"/>
</LinearLayout>
This works fine for me. I changed the gravity to center_vertical and set the gravity of the gridview to 1. I also changed the gridview width just to see if it worked or not. With fill_parent it was hard to see if it was centered or not.

I ended up doing this instead:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<GridView
android:id="#+id/grid_accounts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:verticalSpacing="25dp"
android:listSelector="#color/black"
/>
</FrameLayout>
And made time set the gravity of the images to center

Related

Center gridlayout in linearlayout

I have a Gridlayout inside a Linearlayout and want the Gridlayout to be horizontally centered (ie. same distance to the left and right):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/outerLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10pt"
android:paddingRight="10pt"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:columnWidth="200px"
android:horizontalSpacing="0dp"
android:numColumns="auto_fit"
android:stretchMode="none"
android:verticalSpacing="0dp" />
<Button
android:id="#+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start..."/>
</LinearLayout>
The Gridlayout is centered, but does not appear to be so, since it does not tightly wrap its content (there is still "empty space" inside the Gridlayout on the right side), even though layout_width="wrap_content" is set.
you need to use stretchMode as spacing width uniform instead of none. Set gravity as center.
There are two more options in StretchMode. Check this link
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:columnWidth="200px"
android:gravity="center"
android:horizontalSpacing="0dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="0dp" />

I can't set Horitonzal scrollbar in gridview in android

this is my code ,
i want visiable horitonzal scrollbar in gridview , so i tried to fix it , my gridview have 1row and 3 Columns , icant use vertical scroll because content page have scroll
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_below="#+id/linearLayout2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:id="#+id/linearLayout3"
android:background="#android:drawable/dialog_holo_light_frame">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="طرح های پیشنهادی"
android:id="#+id/textView3"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:layout_gravity="right"
android:textColor="#cc0066"
android:textStyle="bold" />
<GridView
android:id="#+id/gridview1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:verticalSpacing="5dp"
android:numColumns="auto_fit"
android:scrollbars="horizontal" />
</LinearLayout>
</HorizontalScrollView>
you can either enable nestedscrolling in xml or you can enclose your gridview inside a nestedScrollView . Try that , if that's not working let me know.
Did you try to add ScrollView?
HERE is that people already talked about that. If i didn't understand you wrong.
I adding example codes for vertical and horizontal.
How to scroll gridview to position?
How can I add a vertical scrollbar to a gridview?
Firstly, end of the LinearLayout, get rid of the slash: /> to >
Here is direct answer for your question:
Put into ScrollView:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="vertical">
<!-- your codes here -->
</ScrollView>
For horizontal:
<HorizontalScrollView>
<ScrollView>
<!-- your codes here -->
</ScrollView>
</HorizontalScrollView>

How to set the screens size of Grid view to match all screen sizes?

Here i have used a gridview but it is not changing according to the screen size.I think there is a problem with height.the xml used for gridview is given below.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg"
android:orientation="vertical" >
<GridView
android:id="#+id/gridView1"
android:numColumns="3"
android:gravity="center"
android:paddingTop="20dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:stretchMode="columnWidth"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/linearLayout1"
android:layout_centerVertical="true"
>
</GridView>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<com.assuretech.ku.HorizontalMenuBar
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
From what I can see. You need to close the RelativeLayout at the end. I've included my screenshot of your code with it closed. I changed the background to red to see the text.
I guess you are looking for
numColumns = "auto_fit"
as GridView attribute
Firstly you must be using some layout to put your gridview in. Do the following:
Make height and width of the layout to fill parent:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
then, set height and width of your gridview to fill_parent as well.
Well, this is just a supposed solution that you might have been working this way
Hope this works for you.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="30dp">
<GridView
android:id="#+id/grid_images"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="15dp">
</GridView>
</LinearLayout>

Adding two views in Android

So what I want to do is use a gridview at the top of my screen and a table view at the bottom half of my screen. I thought this would be easy but apparently I am wrong. And yes I did my usual search for 1 hour then post something on stackoverflow.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:numColumns="4"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_height="130dp"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:text="Name:"
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:hint="Name"
android:id="#+id/projectName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView
android:text="Start"
android:id="#+id/startText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Push"
android:id="#+id/startdate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="startDate"/>
</TableRow>
<TableRow>
<TextView
android:text="Finish"
android:id="#+id/finishText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/finishdate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="finishDate"/>
</TableRow>
</TableLayout>
</LinearLayout>
Maybe this is easy. Maybe I should just go to bed and do this in the morning.
Thanks for your help.
1) Add android:orientation="vertical" to LinearLayout.
2) Use android:layout_height="fill_parent" for BOTH Grid n Table layout AND also add android:layout_weight="0.5" in both GridLayout and TableLayot. This will equally divide both layouts in screen.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:stretchColumns="1" android:orientation="vertical">
<GridView android:id="#+id/gridview" android:layout_width="fill_parent"
android:numColumns="4" android:verticalSpacing="10dp"
android:horizontalSpacing="10dp" android:stretchMode="columnWidth"
android:gravity="center" **android:layout_height="fill_parent"**
**android:layout_weight="0.5"** />
<TableLayout android:layout_width="fill_parent"
**android:layout_height="fill_parent"** android:stretchColumns="1"
**android:layout_weight="0.5"**>
Rest part remains same...
You can try different combinations of layout_weight like 0.4 & 0.6 or 0.8 & 0.2 etc if you don't want the layouts equally dividing the screen. But if you use layout_height to some fixed pixel or dip value, it will appear different on different devices screens... So I recommend use layout_weight parameter.
The default orientation for the LinearLayout is horizontal.
Add android:orientation="vertical" to your LinearLayout.
And don't set android:layout_height="fill_parent" to your TableLayout otherwise it takes the full screen.

Center elements in GridView

I have GridView with 6 images in 2 columns. I want to display this 6 images in the middle of the screen. I set gravity properties to center but this center elements only horizontally. GridView takes whole screen.
<GridView
android:id="#+id/gridview"
android:layout_above="#id/ad" android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:background="#ffffff"
android:gravity="center"
/>
Here is what I did to get the items in the gridview to center (notice the stretchmode, columnwidth, gravity and horizontal and vertical spacing):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp" >
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="10dp" />
</LinearLayout>
It took me a while to figure it out, but messing with those different values I was able to get them to center.
Try wrapping your GridView in a LinearLayout that has it's child elements centered and change your gridview's layout_height to "wrap_content". ex:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_gravity="center">
<GridView
android:id="#+id/homeGridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</LinearLayout>
Need to set the following attributes:
android:stretchMode="columnWidth"
...and...
android:gravity="center"
well, in case you are using a custom adapter thing to inflate single rowview inside your gridview then try to add android:gravity="center" to the layout of your single_row.. this will do the trick hopefully :)
i.e,
<?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:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/ivClrBlock"
android:layout_width="88dp"
android:layout_height="88dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
/>
<TextView
android:id="#+id/tvClrName"
android:layout_width="88dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="#string/color_name"
android:textSize="15sp" />
</LinearLayout>
Issue occurred when I updated the gridview adapter from:
gridView = inflater.inflate(R.layout.mobile, null);
to
gridView = inflater.inflate(R.layout.mobile, parent, false);
so I just reversed back using a try catch if the old (null) version failed.
Try to change this:
android:layout_height="fill_parent"
into
android:layout_height="wrap_content"
You are now telling your grid view to fill it's parent height and width fully.
Here is my answer:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<GridView
android:id="#+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dip"
android:gravity="center"
android:numColumns="3"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="10dip" />
</LinearLayout>
Don't use padding or horizontalSpacing if you don't need it as it may cause problems with other views.
Note also that fill_parent is already deprecated.
I hope this will solve your problem:
<LinearLayout
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<GridLayout
android:id="#+id/gridlayou4x3"
android:rowCount="4"
android:columnCount="3"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="80"
android:minWidth="25px"
android:minHeight="25px" />
</LinearLayout>
I found that you just have the gridlayout wrap content on height and width.
android:layout_centerHorizontal="true"
It will center itself for the size it needs but if you have it matching the parent. You are technically centering it based with a lot of parent empty space offsetting your children.

Categories

Resources