Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grid Layout support in android API 10

I had developed an app with target API as 15. The layout includes a grid layout. Now when i am changing the Target API to 10 I am getting issues with the grid Layout and another element 'space'. Eclipse prompts me to download a supporting library for 2.3.3, Which i did... But still having the issue.

like image 762
Madhav Kishore Avatar asked Apr 23 '12 09:04

Madhav Kishore


People also ask

Is GridView deprecated in android?

Both GridView and GridLayout are safe for use in production code. They are obsolete, and I would not recommend that anyone use them, but they are safe. You are contradicting yourself. First you say GridView and GridLayout are not deprecated, then you say they are both obsolete, but safe.

What is the grid layout in android?

A layout that places its children in a rectangular grid. The grid is composed of a set of infinitely thin lines that separate the viewing area into cells. Throughout the API, grid lines are referenced by grid indices. A grid with N columns has N + 1 grid indices that run from 0 through N inclusive.

What can I use instead of grid layout in android?

Some of the most commonly used layouts are LinearLayout, Constraint Layout, RelativeLayout, and GridLayout. Suppose you need to display the elements linearly, whether horizontally or vertically; you can use LinearLayout.

What is the difference between GridLayout and GridView in android?

Let us not get confused with GridView and GridLayout to be the same. GridView simply gives us a two-dimensional view to display the items on the screen, under ViewGroup. On the other hand, GridLayout is a layout manager that arranges the views in a grid.


3 Answers

GridLayout has indeed been backported to be compatible with API level 7 and up. It's (sort of) part of the support library. After you've downloaded the support library, you'll find an Android library project in your local sdk folder located at:

<sdk_folder>\extras\android\compatibility\v7\gridlayout

Set it up as dependency of the project you're working on. After that, you'll need to change the references throughout your project from the level 15 version to this compatibility one in order to support pre-ICS devices. In the Layout files change your xml from GridLayout to android.support.v7.widget.GridLayout. Usage should be similar, if not identical.

Regarding the Space widget: I haven't actually used this one (yet), but it appears to be something that could be replaced by either setting margin/padding on existing views or adding in your own generic View that is completely transparent.

like image 70
MH. Avatar answered Oct 22 '22 11:10

MH.


If using Android Studio, once you've downloaded the support library (as @MH. mentioned) you must use File->Import Module..., then select the location of GridLayout support, which for me was located here: <sdk_folder>\extras\android\support\v7\gridlayout.

Next, instead of using <Gridlayout> in your xml layout file, you use <android.support.v7.widget.GridLayout>. Here is an example:

<android.support.v7.widget.GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:useDefaultMargins="true"
    app:alignmentMode="alignBounds"
    app:columnOrderPreserved="false"
    app:columnCount="4"
>

Elements in the layout will reference app instead of android for GridLayout properties as seen above and in child elements:

    <TextView
        android:textSize="32dip"
        app:layout_columnSpan="4"
        app:layout_gravity="center_horizontal"
    />

Finally, the GridLayout support library reference must be added to the dependencies section of \src\build.gradle:

dependencies {
    compile 'com.android.support:appcompat-v7:+'
    compile 'com.android.support:gridlayout-v7:+'
}
like image 21
Jason Hartley Avatar answered Oct 22 '22 12:10

Jason Hartley


Or another alternative is this third-party backwards compatible implementation:

This is compatible back to API level 3, if you need to go that far back, and it also includes the Space widget.

like image 4
kingraam Avatar answered Oct 22 '22 11:10

kingraam