How to display multiple columns of CardView? - android

I wish to achieve a layout like this one that can dynamically display albums and change the number of columns according to orientation (vertical or horizontal). Here is an example of what I am trying to achieve:
As of now my code looks something like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="160dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:layout_weight="1"
android:layout_margin="8dp"
app:cardCornerRadius="0dp"
app:elevation="5dp">
<ImageView
android:id="#+id/avatar"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_gravity="top|center"
android:scaleType="centerCrop"
android:src="#drawable/wp" />
<TextView
android:id="#+id/album_name"
android:layout_width="match_parent"
android:layout_height="25dp"
android:alpha="0.85"
android:background="#color/light"
android:layout_above="#+id/detail"
android:layout_below="#+id/avatar"
android:gravity="bottom|center"
android:layout_alignParentBottom="true"
android:text="Test"
android:textColor="#color/cardview_dark_background" />
<TextView
android:id="#+id/detail"
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_gravity="bottom|center"
android:alpha="0.85"
android:background="#color/light"
android:gravity="bottom|center"
android:layout_alignParentBottom="true"
android:layout_below="#+id/album_name"
android:text="Test"
android:textColor="#color/dim_foreground_disabled_material_dark" />
</android.support.v7.widget.CardView>
</LinearLayout>

It's a GridView. Inside GridView you can set CardView of your choice. You can set how many Cards you want to show vertical or horizontal.
Sample layout:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
In your Activity now you can set it's adapter
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new YourCustomAlbumAdapter(this));
For complete example you can see official Example http://developer.android.com/guide/topics/ui/layout/gridview.html

Related

Can not inflate button widget after/before GridView

I want to add a button widget just above the GridView,But it seems not happy with my code, i mean its not showing up on the screen, no matter what i am doing,by the way am on 7.1.1, here is my layout,
<?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:orientation="vertical">
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/images_grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:columnWidth="180dp"
android:gravity="center"
android:horizontalSpacing="8dp"
android:numColumns="3"
android:padding="16dp"
android:stretchMode="columnWidth"
android:verticalSpacing="8dp">
</GridView>
<Button
android:text="Next"
android:id="#+id/next_Button"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
So this is it, layout is insanely simple, but i can not figure it out, any help would be highly appreciable !
Use this:
<?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="match_parent">
<Button
android:id="#+id/next_Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Next" />
<GridView
android:id="#+id/images_grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/next_Button"
android:columnWidth="180dp"
android:gravity="center_horizontal"
android:horizontalSpacing="8dp"
android:numColumns="3"
android:padding="16dp"
android:stretchMode="columnWidth"
android:verticalSpacing="8dp">
</GridView>
</RelativeLayout>
Use RelativeLayout to let the Button stick to the parent's bottom, and set the GridView to stay above the button
You are trying to use Linear layout weight attribute.If you are trying to use it grid layout layout height attribute should also be 0dp and you should add layout weight attribute for grid layout too.
<?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:orientation="vertical">
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/images_grid_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
android:adjustViewBounds="true"
android:columnWidth="180dp"
android:gravity="center"
android:horizontalSpacing="8dp"
android:numColumns="3"
android:padding="16dp"
android:stretchMode="columnWidth"
android:verticalSpacing="8dp">
</GridView>
<Button
android:text="Next"
android:id="#+id/next_Button"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
Hope it helps u.

Make a GridView expand vertically

I have a GridView in a vertical LinearLayout, that I want to have take up all remaining vertical space. The grid will always have 5 columns and 7 rows. The grid items are just a TextView. I want the grid items to expand proportionally (so that each item is one fifth of the width and one seventh of the height) to fill remaining space on any device. I tried setting layout weights, but that didn't do anything.
How can I make it fill the space?
Here's my activity:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<TextView
android:text="#string/zero"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tv0"
android:gravity="end"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:paddingEnd="8dp"
android:paddingStart="8dp"
android:textColor="#e4e4e4"
android:background="#color/colorPrimaryDark"/>
<!-- A few more TextViews -->
<GridView
android:id="#+id/grid_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:horizontalSpacing="0dp"
android:verticalSpacing="0dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:numColumns="5"/>
</LinearLayout>
And here's the layout for the grid item:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/colorPrimary"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"/>
</FrameLayout>
I didn't understand exacly what you meant by "proportionally", but to make the GridView "fill the space" in your example you don't need to set layout weights:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/text" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/text" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/text" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/text" />
<GridView
android:id="#+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:horizontalSpacing="0dp"
android:numColumns="5"
android:stretchMode="columnWidth"
android:verticalSpacing="0dp" />
</LinearLayout>
Generates the layout below:

Unwanted Gridview spacing

My grid design is as such. But i want to remove the spaces on both sides of the grid elements. (Black marked)
I want the image to be expanded somewhat like
My XML layouts for the gridview
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dip"
android:gravity="center"
android:numColumns="2"
android:stretchMode="spacingWidthUniform" />
</LinearLayout>
My grid element layout
<?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:orientation="vertical" >
<ImageView
android:id="#+id/grid_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:contentDescription="#string/im" >
</ImageView>
<TextView
android:id="#+id/grid_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLength="23"
android:gravity="center"
android:layout_marginTop="10sp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textSize="12sp" >
</TextView>
</LinearLayout>
Try Changing
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:numColumns="2"
android:background="#abcdef"
android:stretchMode="columnWidth" />
You can replace GridView with GridLayout and use weights to make the images fill the screen. Here's the reference: http://developer.android.com/reference/android/widget/GridLayout.html, (See the section: Excess space distribution)

Gridview last row being cut off

I am using a gridview to display a collection of items in the form of an image with a textview beneath them for their title.
The final item of collection of items is unique in the fact that it only has an image, no textview (this might be relevant).
At the moment I am finding that as soon as a row is created that has a normal item and an image only item in it, the textview is cutoff.
I have tried setting the min height and height of the items but it doesn't seem to affect the grid at all.
Grid:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#drawable/Background"
android:id="#+id/root_layout"
android:padding="10dip"
android:layout_height="match_parent">
<GridView
android:id="#+id/connection_grid"
android:listSelector="#android:color/transparent"
android:horizontalSpacing="25dip"
android:stretchMode="columnWidth"
android:verticalSpacing="25dip"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Grid Item:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp">
<FrameLayout
android:id="#+id/screencap_layout"
android:layout_height="wrap_content"
android:background="#drawable/RowBorderBackground"
android:layout_width="wrap_content"
android:padding="1dip"
android:layout_centerHorizontal="true">
<ImageView
android:id="#+id/connection_icon"
android:layout_width="200dip"
android:layout_height="147dip"
android:background="#drawable/grid_view_selector"
android:scaleType="centerCrop" />
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/devicetype_layout"
android:background="#drawable/RowBorderBackground"
android:layout_gravity="right|bottom"
android:layout_marginBottom="-2dip"
android:layout_marginRight="-2dip"
android:padding="2dip">
<TextView
android:text="NA"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/device_type" />
</RelativeLayout>
</FrameLayout>
<TextView
android:id="#+id/connection_name"
android:text="Machine 1"
android:gravity="center"
android:textSize="18dip"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Set some paddingBottom to gridview gridview class.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#drawable/Background"
android:id="#+id/root_layout"
android:layout_height="match_parent">
<GridView
android:id="#+id/connection_grid"
android:listSelector="#android:color/transparent"
android:horizontalSpacing="25dip"
android:stretchMode="columnWidth"
android:verticalSpacing="25dip"
android:paddingBottom="20dip"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

above i want image in layout

Hi in my application I am displaying image and text by using grid view for that i used frame layout inside linear layout.
Now i want above the list view i want to display some image.
can anyone please help me.
xml
<?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:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<com.agilerise.hotel.SquareImageView
android:id="#+id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:paddingTop="50dp"
android:paddingBottom="50dp"
/>
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_gravity="bottom"
android:textColor="#android:color/black"
/>
</FrameLayout>
<GridView
android:id="#+id/buttom"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:numColumns="3"
android:layout_weight="1">
</GridView>
</LinearLayout>
add list view in your xml
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1.68" >
</ListView>
and create item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="#drawable/ic_avatar"
android:scaleType="centerCrop"/>
</LinearLayout>
Then you should use ArrayAdapter in your activity class to create your listview...

Categories

Resources