CardView only showing first row of GridLayout - android

For some reason, my CardView is only showing the first row of images in my grid view. (or my GridView is only showing the first row - I can't tell).
I am dynamically adding images into the gridView and using gridAdapter.notifyDataSetChanged(). Perhaps this is why the height of the CardView is not adapting? When I add android:layout_height="500dp" to the CardView, then I see all my images.
Is there a way to make the CardView wrap the content so I can see all of my images (other than hardcoding the height)?
Please note, I cannot hardcode the height as the number of rows of photos is dynamic.
Only showing one row of images, even when there are actually 2 rows of images
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="2dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/m_photos" />
<GridView
android:id="#+id/m_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:clickable="true"
android:columnWidth="90dp"
android:drawSelectorOnTop="true"
android:focusable="true"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="none"
android:verticalSpacing="2dp" />
</LinearLayout>
</android.support.v7.widget.CardView>

I faced This problem few days ago.For this case, Wrap_content not working perfectly.So Use layout height in fixed dp and use dimention for that. it works for my case.
<GridView
android:id="#+id/m_grid"
android:layout_width="match_parent"
android:layout_height="#dimen/gvHeight"
android:layout_margin="2dp"
android:clickable="true"
android:columnWidth="90dp"
android:drawSelectorOnTop="true"
android:focusable="true"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="none"
android:verticalSpacing="2dp" />

Related

Gridview scroll width

I have this Gridview
<GridView
android:id="#+id/gridview_example"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:layout_marginLeft="45dp"
android:layout_marginRight="45dp"
android:layout_marginTop="35dp"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:numColumns="3"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:fadeScrollbars="false"
android:scrollbarStyle="outsideInset"
android:scrollbarSize="30dp"
android:smoothScrollbar="true">
</GridView>
for which I'm trying to increase the scroll width. I've tried using scrollbarSize but nothing changes. Is there a way to increase the width of the scrollbar without customizing it?
I'm new in Android development so please be gentle :)
Update: Tested this possible solution and added my gridview inside the scrollview
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:layout_marginLeft="45dp"
android:layout_marginRight="45dp"
android:layout_marginTop="35dp"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_above="#+id/bottomMenu"
android:scrollbarSize="50dip">
<GridView
android:id="#+id/gridview_holes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp">
</GridView>
</ScrollView>
But the gridview is much more smaller and I'm seeing just the gridview thin scroll.
please try this
android:scrollbarAlwaysDrawVerticalTrack=true
android:scrollbars=vertical
use Scrollview for scrolling sub view
https://developer.android.com/reference/android/widget/ScrollView.html

Automatically adjusting size of cells in a gridview in android

Am developing a custom gallery using gridview in android. Now am facing the issue with the columns count ..my gridview xml is,
<GridView
android:id="#+id/gv_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:choiceMode="multipleChoice"
android:stretchMode="columnWidth"
android:gravity="center"
android:columnWidth="179dp">
</GridView>
my grid item layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="179dp"
android:layout_height="131dp"
android:padding="1dp" >
<ImageView
android:id="#+id/iv_thumbnail"
android:contentDescription="#string/iv_content_description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/tv_folder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentBottom="true"
android:text="#string/tv_folder"
android:padding="12dp"
android:textColor="#color/white"
android:background="#color/transparent_black"/>
</RelativeLayout>
of course it's dynamically maintaining columns according to device width but there is different column spacing in different devices..
how i can overcome this issue.?
To distribute the size uniformly don't use a fixed size for each grid item.
Set the width to wrap_content and you can also specify the number of columns

Android issue with image size in gridview

I am trying to create a grid layout of cards similar to what is used in google play like this
The layout i am using and the layout obtained are these:
For GridView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#e5e5e5"
>
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="60dp"
android:numColumns="2"
android:layout_marginLeft="2dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</RelativeLayout>
for GridItem:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bg_card"
>
<ImageView
android:id="#+id/imgIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:textSize="17sp"
android:layout_below="#id/imgIcon"
android:layout_marginTop="35dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
The output that I get is this:
The image size that i am using is 356x500. (I do get a somewhat well sized grid on using 300x300)
The issues that i am facing are:
1.As this image shows, the cards are too big with lots of white spaces around the image and the image does not fit like in the
google play store. How do i make the card be propotional to the
image. The amount of white space changes with change in image size.
The card is "stuck" to the left edge of the screen . How do i keep spacing between the edge of the screen and the card.
Please help me in rectifying this issue.
Thanks
Space are bacause you used
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
Also edit your
<ImageView
android:id="#+id/imgIcon"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
in gridview use
android:stretchMode="none" instead of android:stretchMode="columnWidth"
Put android:scaleType="fitStart" inside your
Use imgIcon.setAdjustViewBounds(true);
You can also do the same in the xml

Stretch image background of a linear layout which serves as cell in gridview

I have this cell item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="#+id/titleText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:background="#AA000000"
android:gravity="center"
android:textColor="#FFFFFF"/>
</LinearLayout>
and this GridView:
<GridView android:id="#+id/gridView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="100dp"
android:layout_weight="1"/>
I want to set linear layout's background image but, according to image size, I get different heigths for cells (I use different images) which is not very good looking...how can I stretch the background to a fixed heigth?
Unfortunately gridview seems not have a columnHeigth attribute...
Solved thanks to chet 's advice: I set heigth and width of linear layout.

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