Search code examples
androidandroid-layoutgridviewgravitylayout-gravity

How to align items at the top of a GridView in Android?


I try to make my own gallery application. The problem is that my image views appear in the bottom of the gridview that contains them. How to align them at the top? I've tried to set the gravity but it's not working.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/crud_layout" >

    <GridView
        android:id="@+id/album_grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnWidth="100dp"
        android:gravity="center"
        android:horizontalSpacing="2dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="2dp" />

</FrameLayout>

<LinearLayout
    android:id="@id/crud_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >

    <Button
        android:id="@+id/button_share"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="@string/new_album" />

    <Button
        android:id="@+id/button_delete"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="@string/delete_album" />

    <ImageButton
        android:id="@+id/button_camera"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" >
    </ImageButton>
</LinearLayout>


Solution

  • Edit your code as shown below:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gallery"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        **android:layout_alignParentTop="true"**//change only here
        android:layout_above="@+id/crud_layout" >
    
        <GridView
            android:id="@+id/album_grid"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:columnWidth="100dp"
            android:gravity="center"
            android:horizontalSpacing="2dp"
            android:numColumns="auto_fit"
            android:stretchMode="columnWidth"
            android:verticalSpacing="2dp" />
    
    </FrameLayout>
    
    <LinearLayout
        android:id="@id/crud_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >
    
        <Button
            android:id="@+id/button_share"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="zee" />
    
        <Button
            android:id="@+id/button_delete"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="khan" />
    
        <ImageButton
            android:id="@+id/button_camera"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" >
        </ImageButton>
    </LinearLayout>
    </RelativeLayout>