Android RelativeLayout height does not match GridView height

I am having a problem with a GridView in RelativeLayout, which is again in ScrollView. The problem is that the height of the RelativeLayout does not match the height of the contents of the GridView. When there are multiple rows, the GridView is cropped and a scroll bar appears, which is undesirable. I tried to illustrate my problem using a screenshot from the Android hierarchy viewer. You can see how the red RelativeLayout field cropped the second row of the GridView. I am inserting an XML page layout (page.xml) and a separate grid element (griditem.xml). I used the following code to inflate mesh elements in gridAdapter code:

/ *********** The beginning of the gridAdapter fragment *********************** /

public View getView(int position, View convertView, ViewGroup parent) { View v; if(convertView==null){ li = LayoutInflater.from(mContext); v = li.inflate(R.layout.griditem, null); //code for fetching textView and imageUrl content TextView tv = (TextView)v.findViewById(R.id.icon_text); tv.setText(textContent); ImageView iv = (ImageView)v.findViewById(R.id.icon_image); //code for fetching and attaching image to imageView } else { v = convertView; } return v; } 

/ *********** The end of the gridAdapter fragment *********************** /

/ *********** Start page .xml *********************** /

 <TextView android:id="@+id/title" android:layout_weight="1" android:layout_width="320dip" android:layout_height="wrap_content" android:singleLine="false" android:textStyle="bold" android:textSize="14dip" /> <ImageView android:id="@+id/image" android:layout_below="@+id/title" android:adjustViewBounds="true" android:layout_width="128dip" android:layout_height="96dip" android:layout_marginRight="4dip" /> <TextView android:id="@+id/name" android:layout_weight="1" android:layout_below="@+id/title" android:layout_toRightOf="@+id/image" android:layout_width="192dip" android:layout_height="wrap_content" android:paddingTop="2dip" android:textSize="12dip" android:paddingLeft="2dip" /> <TextView android:id="@+id/location" android:layout_weight="1" android:layout_below="@+id/name" android:layout_toRightOf="@+id/image" android:layout_width="192dip" android:layout_height="wrap_content" android:paddingTop="2dip" android:textSize="12dip" android:paddingLeft="2dip" /> <TextView android:id="@+id/date1" android:layout_weight="1" android:layout_below="@+id/location" android:layout_toRightOf="@+id/image" android:layout_width="192dip" android:layout_height="wrap_content" android:paddingTop="2dip" android:textSize="10dip" android:paddingLeft="2dip" /> <TextView android:id="@+id/date2" android:layout_weight="1" android:layout_below="@+id/date1" android:layout_toRightOf="@+id/image" android:layout_width="192dip" android:layout_height="wrap_content" android:paddingTop="2dip" android:textSize="10dip" android:paddingLeft="2dip" /> <Button android:id="@+id/button1" android:text="buttonText1" android:layout_weight="1" android:layout_below="@+id/image" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="96dip" /> <Button android:id="@+id/button2" android:text="buttonText2" android:layout_weight="1" android:layout_below="@+id/image" android:layout_toRightOf="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content" /> <Button android:id="@+id/button3" android:text="Text" android:layout_weight="1" android:layout_below="@+id/image" android:layout_toRightOf="@+id/button2" android:layout_height="wrap_content" android:layout_width="wrap_content" /> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@+id/button1" android:layout_weight="1" android:numColumns="auto_fit" android:verticalSpacing="10dip" android:horizontalSpacing="10dip" android:stretchMode="columnWidth" android:gravity="center" /> 

/ *********** End page.xml *********************** /

/ *********** Run griditem.xml ********************** /

Android: Gravity = "center_horizontal">

/ *********** End griditem.xml *********************** /

Can you tell me what I have to do so that the height of the RelativeLayout matches the entire length of the gridView?

Thank Kuntal [Alt text] [1]

Here is a screenshot: http://tinypic.com/r/98rs4n/4

+7
android
source share
4 answers

So far, the only way I could get around this is to make a little fiction.

Once you know how many items you upload to your grid, you will manually set the height:

 final int gridWidth = myGrid.getWidth(); final int cellWidthDP = 50; final int cellHeightDP = 80; final Resources r = getResources(); final double cellWidthPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, cellWidthDP, r.getDisplayMetrics()); final double cellHeightPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, cellHeightDP, r.getDisplayMetrics()); final int itemsPerRow = (int) Math.floor((double) gridWidth / cellWidthPx); final int rowCount = (int) Math.ceil((double) itemCount / itemsPerRow); final LayoutParams prm = myGrid.getLayoutParams(); prm.height = (int) Math.ceil((double) rowCount * cellHeightPx); myGrid.setLayoutParams(prm); 
+4
source share

Sorry, but the last code snippet does not display properly. So I reinsert it:

Android: Gravity = "center_horizontal">

0
source share

As mentioned in the stream http://code.google.com/p/android/issues/detail?id=1394

using this

convertView = mInflater.inflate(R.layout.my_row, parent, false);

fixes a problem in the froyo file.

0
source share

The best solution (extending the height of the GridView) is here: https://stackoverflow.com/a/2208772

0
source share

All Articles