How to dynamically filled cardviews according to device width - android-cardview

I am making an app where I want to maintain cards of fixed width. So How to dynamically filled cardviews according to device width.
For Ex. for a normal size phone there should be two column of cards and for big size screen there are three column of cards when app in portait mode in both phones.
Like this :
3 column cards example
2 column cards example

Got the solution,
need to use the GridView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="app.appmax.fest1.Dialogs.CustomGridView.MenuHome">
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:stretchMode="columnWidth"
android:id="#+id/gridview1"
android:numColumns="auto_fit"
>
</GridView>
</RelativeLayout>

Related

Imageview and gridview in Android

I'm trying to develop an Android application, I need a particular layout.
I want realize an activity composed by a imageview at the top of the screen and bottom a gridview.
The layout is static, can't change.
How i can realize it ?
This is my code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="info.MainActivity"
tools:showIn="#layout/activity_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ovada"/>
</LinearLayout>
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="2"
android:columnWidth="300dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="5dp"
android:gravity="center"
android:stretchMode="columnWidth" >
</GridView>
</RelativeLayout>
Actually this code don't work as i want.
**** EDIT 1 ****
Ok, i have one problem:
1)What that i what obtain is:
Image "logo" at the top of the screen
Image "logo" above the Gridview
Image and logo scrollable
when I scroll down the "page" I want scroll ALL the content of my page not only the grid view...
add this to your LinearLayout :
android:id="#+id/linear_id"
android:alignParentTop="true"
And GridView
android:layout_below="#id/linear_id
I also suggest that you get rid of the linear layout,because i don't see any use in adding it.
For ListView and also for GridView, it is usually not a good idea to set the layout_height to wrap_content. "wrap_content" means make me as big as my children. This is hard to figure out for a listView and for GridView. It maybe better to set the height to match_parent.

GridView Layout android:horizontalSpacing is not working

I am using android:horizontalSpacing attribute in my GridView layout to give horizontal spacing between two columns. But nothing getting change only it give space between the vertical columns.
<?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">
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:stretchMode="columnWidth"
android:numColumns="7" />
</FrameLayout>
This is the above code but i don't understand what is the actual mistake i am doing. You can see in above picture that , there are only black vertical lines but i also want horizontal black lines.
Please help me out , suggest me some solution.

Android Studio layout-w600dp doesn't work on landscape Nexus 5

this is my first question on StackOverflow =)
I am using Android Studio 1.0.2 (top update) and I want to create a grid layout that has one column on Nexus 5 portrait and two on Nexus 5 landscape. Nexus 5 is 640 x 360 dp, so I have three folders "layout" and "layout-w600dp" and "layout-w600dp-land" (to be extra sure).
My "fragment_main.xml" in "layout" folder looks like this:
<FrameLayout 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$PlaceholderFragment">
<GridView
android:id="#+id/gridview_products"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="1"/>
</FrameLayout>
In "layout-w600dp" and "layout-w600dp-land" folders the only difference is
android:numColumns="2"
In Android Studio it shows me that "layout-w600dp-land" is used. Screen is here:
https://pp.vk.me/c625530/v625530960/19a4d/G7irG51csn8.jpg
And on real device when I rotate the screen it doesn't make two columns. Screen is here: https://pp.vk.me/c625530/v625530960/19a57/mmCdluXWSQw.jpg
What can cause this problem?
You can just rename "layout-w600dp" to "layout-w595dp" and it will work on Nexus 5.
To get more responsive design you can also create layouts with more than two columns for bigger screens.
When you have multiple columns in a grid view, you must define a column width. This is a grid view from one of my apps.
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:numColumns="2"
android:columnWidth="100dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:background="#android:color/transparent"
android:stretchMode="columnWidth" >
</GridView>
If needed you can also use android:numColumns="auto_fit" to have it create as many columns as possible of size columnWidth.

Adjust GridView (Android)

I have a problem with grid view layout on Android. I can't find solution to eliminate extra space in grid view.I mention the code fro gridview as below :-
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:alwaysDrawnWithCache="true"
android:background="#android:color/background_dark"
android:clipChildren="true"
android:columnWidth="100dp"
android:gravity="center_horizontal"
android:horizontalSpacing="2dp"
android:numColumns="auto_fit"
android:padding="0dp"
android:scrollingCache="true"
android:smoothScrollbar="true"
android:stretchMode="columnWidth"
android:verticalSpacing="2dp" />
now my application's grid view look like this
As shown there is extra space which i want to remove it and it can look like this:-
So please provide me solution..
Thanks in advance..
You need to alter the value for android:columnWidth attribute for all layout types [hdpi,mdpi,xhdpi etc.] supported by your application.
You can use :
android:listSelector="#null"
to eliminate extera space
and try this , for 3 column cell:
android:numColumn = "3"

GridView item position

I have GridView:
<?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:id="#+id/frag_books_grid_view_layout"
android:orientation="vertical">
<GridView
android:cacheColorHint="#color/bg_default"
android:listSelector="#drawable/list_selector_background"
android:id="#+id/frag_books_grid_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnWidth="120px"
android:gravity="center"
android:horizontalSpacing="15dp"
android:scrollbars="none"
android:stretchMode="columnWidth"
/>
</LinearLayout>
And single item (placed in cell of this grid):
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:cacheColorHint="#color/bg_default"
android:scrollbars="none"
android:listSelector="#drawable/list_selector_background"
android:layout_gravity="center_vertical|left"
android:gravity="center_vertical|left"
android:padding="15dp"
android:id="#+id/frag_book_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="5dp"/>
How it looks: http://i.imgur.com/rhZiu.png
Everything is fine with blue selection, but the contents of the cell is not centered inside of it.
This picture explains what I want: http://i.imgur.com/OumWu.png
Besides it's not recommended to have nested scrollable components, you have the dimensions of list view set to match_parent. So it's not possible to center, since it's already occupying all available space.
If you want to center, you first have make the listView smaller (setting fixed size). And then use gravity="center" on the container. You also can apply a uniform padding to the container, this will also make the listView look centered.

Resources