Andy Johnson
Andy Johnson

Reputation: 8169

Get GridView horizontal spacing on Android 4.0

Is there any way to determine the horizontal spacing used by a GridView on Android 4.0.3 (API level 15)?

I have some working code targeting 4.1.x that uses GridView.GetRequestedHorizontalSpacing(), but this doesn't exist prior to API 16. Is there an equivalent to this, or a way to calculate it, on API level 15?

Upvotes: 0

Views: 707

Answers (1)

Gopal Gopi
Gopal Gopi

Reputation: 11131

you can get it using Reflection API.

    GridView gridView = (GridView) findViewById(R.id.gridView);
    try {
        Field field = gridView.getClass().getDeclaredField("mHorizontalSpacing");
        field.setAccessible(true);
        int hSpacing = field.getInt(gridView);

        field = gridView.getClass().getDeclaredField("mRequestedHorizontalSpacing");
        field.setAccessible(true);
        int reqHSpacing = field.getInt(gridView);
    } catch (Exception e) {
        // TODO: handle exception
    }

Upvotes: 3

Related Questions