Search code examples
javaandroidxmlgridviewoverlapping

Android GridView Text overlapping


I try now for two days to create a working GridView, but it won't work, this is how it looks like:

enter image description here

You see, my problem is overlapping text. I already found out, that the height of a GridView Row is set by the last item. My XML looks so:

<RelativeLayout 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=".Startbildschirm">


    <GridView
        android:numColumns="2"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:horizontalSpacing="5dp"
        android:verticalSpacing="5dp"
        />
</RelativeLayout>

My idea to fix this was to get the Height of the longest view in one Row and then set it as MinHeight to the last view. My problem was, that views are only saved as long they are visible, so on down and upscrolling the height was different again. Does anyone have an idea how to fix this?


Solution

  • your problem lies with the gridViewItem layout (the one you inflate in the adapter) not with the gridview, i belive it has

    android:layout_height="wrap_content" 
    

    somewhere, just set the layout height there to have a fixed amount of dp

    (android:layout_height ="10dp" for instance) 
    

    then you will not have a problem.

    edit based on comment: 2 options - 1)if the list isnt really long just use tableLayout and populate the rows in code, (the minus is that it doesn recicle views and therefor takes more memory than gridview) but it should get the effect you want (each row uses wrap content and adjusts to the length of text)

    2)the adapter is using a list of some sort - run through it before using the grid and get the max height, now create a custom adapter that uses that (the max height) in your getView() to return approprite views (all of which big enough for max length)