3

I am trying to make a layout which resembles a Grid Layout, but I am restrained to Android 2.0

Does anyone have any ideas?

<?xml version="1.0" encoding="UTF-8"?>
<GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="2"
    android:rowCount="13" >

    <TextView
        android:layout_gravity="fill_horizontal"
        android:text="SN : " />

    <TextView
        android:id="@+id/snTextView"
        android:layout_gravity="fill_horizontal" />


    <TextView
        android:layout_gravity="fill_horizontal"
        android:text="Ver : " />

    <TextView
        android:id="@+id/verTextView"
        android:layout_gravity="fill_horizontal" />

    <TextView
        android:layout_gravity="fill_horizontal"
        android:text="Type : " />

    <TextView
        android:id="@+id/typeTextView"
        android:layout_gravity="fill_horizontal" />

    <TextView
        android:layout_gravity="fill_horizontal"
        android:text="OD : " />

    <TextView
        android:id="@+id/odTextView"
        android:layout_gravity="fill_horizontal" />

    <TextView
        android:text="Closing Mode"
        android:layout_gravity="fill_horizontal" />

    <TextView
        android:id="@+id/closingModeTextView"
        android:layout_gravity="fill_horizontal" />

    <TextView
        android:text="CT : "
        android:layout_gravity="fill_horizontal" />

</GridLayout>

EDIT:

hmm, I had began coding about half an hour ago in the .xml file, I'll post the code above. To see how the first part of it looked, I went to the Graphical Layout, and the following error came up...

"com.android.layoutlib.bridge.MockView cannot be cast to android.view.ViewGroup Exception details are logged in Window>Show View> Error Log The following classes could not be found: - GridLayout( Fix Build Path, Edit XML )"

I guess it may be that something else is wrong, but in the Graphical Layout pallete GridLayout is not listed, while the rest of them are( Linear(vert/hoz), Relative, Frame, Fragment, Table, etc. )

JuiCe
  • 4,132
  • 16
  • 68
  • 119
  • GridView has been in android since API 1. You should be able to pull this off. http://developer.android.com/reference/android/widget/GridView.html http://developer.android.com/resources/tutorials/views/hello-gridview.html – petey Jun 20 '12 at 19:19
  • 2
    @forgivegod: `GridLayout` != `GridView`. ;) – MH. Jun 20 '12 at 19:26
  • Ah, I did not notice that either, thank you MH. I was getting a little nervous. – JuiCe Jun 20 '12 at 19:28

2 Answers2

9

GridLayout has 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 make sure you point any references throughout your project to this one, and not the level 15 version, in order to support pre-ICS devices. Usage should be similar, if not identical.

See also: Grid Layout support in android API 10

Community
  • 1
  • 1
MH.
  • 45,303
  • 10
  • 103
  • 116
  • I have gone to the website provided in the other thread, downloaded the support package from the SDK, added it to the libs folder in my project, right-clicked the .jar file and clicked Add to Build Path, but now don't know what to do. The directions stop there on the other website saying that my application is now ready to use the library APIs. Any ideas? I am using it strictly for a XML file. – JuiCe Jun 21 '12 at 12:58
  • @JuiCe: As mentioned above: `GridLayout` is *not* part of the support library jar, but it's set up as a library project. Hence, you'll need to add a reference to it, on top of including the jar. The location of the `GridLayout` library project is given by the path above. If you're still having problems setting it up, you may want to [read through the steps here](http://stackoverflow.com/q/10133078/1029225). – MH. Jun 21 '12 at 19:45
2

The GridView is one option if you want to have a layout that has equal number of columns in each row. The good side of this is that you can define a single adapter to assign it to the gridView which will hold all of your views. You can find a ton of examples on this on android.

Now the alternative is that you want a layout that will not actually have an equal number of columns in each row (same as google play app has). In this case you can still use the GridLayout. Its available in the compatibility library downloaded with the the latest one. it can be added as a library project to your application and use it exactly the same way as you use it with android 4.0+. This i think also requires the compatibility library to be added as well. I don't think you will find much documentation at this point on how to achieve adding the compatibility grid layout to your project but it is the same as adding any library project. You can find the project code in the android sdk folder under compatibility v7.

DArkO
  • 15,880
  • 12
  • 60
  • 88