3

I have a image view and grid view in my .xml file. When I am executing this file its getting some space between grid view and image view. How to avoid this space...can any one help me to solve this problem. thanks in advance... my xml code is:

    android:layout_width="0px"
    android:layout_height="0px">
</Button>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/prevMonth"
        android:src="@drawable/cal_left_arrow_off2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </ImageView>
    <Button
        android:id="@+id/currentMonth"
        android:layout_weight="0.6"

        android:textAppearance="?android:attr/textAppearanceMedium"
        android:background="@drawable/calendar_bar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </Button>
    <ImageView
        android:id="@+id/nextMonth"
        android:src="@drawable/cal_right_arrow_off2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </ImageView>
</LinearLayout>
<LinearLayout
    android:layout_gravity="bottom|center_horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/calendarheader"
        android:src="@drawable/blue_bg_with_text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        >
        </ImageView>
</LinearLayout>
<GridView
    android:id="@+id/calendar"
    android:numColumns="7"
    android:layout_gravity="top"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
</GridView>

Shekhar
  • 1,767
  • 3
  • 16
  • 19
  • Which layout do you use for these two to as parent? Would you share it please, to see their attributes as well? - to avoid that an actually good answer eventually mess up your layout. – rekaszeru Apr 16 '11 at 05:05
  • I am using Linearlayout...I have updated question with code. – Shekhar Apr 16 '11 at 05:21

1 Answers1

2

You should try setting the android:gravity attribute correctly, and it has to work:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:gravity="bottom|center_horizontal" 
        android:layout_width="fill_parent" android:layout_height="wrap_content">
        <ImageView android:id="@+id/calendarheader" android:src="@color/blue_start"
            android:layout_width="wrap_content" android:layout_height="wrap_content" 
             android:minWidth="250dp" android:minHeight="30dp">
        </ImageView>
    </LinearLayout>
    <GridView android:id="@+id/calendar" android:numColumns="7"
        android:layout_gravity="top" 
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:background="@color/blue_start">
    </GridView>
</LinearLayout>

i'm using here simple color background, and there is no gap:

no gap

If you still having the problem, then you should see your blue_bg_with_text2 image, whether it has any empty pixels at the bottom.

ps: ignore the android:minWidth="250dp" android:minHeight="30dp" part, it is there to imitate an image with that size.

Update

Here is the same view using RelativeLayout (it's a bit clearer at least for me):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button android:id="@+id/selectedDayMonthYear"
        android:textColor="#000000" android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="0px" android:layout_height="0px" />
    <ImageView android:id="@+id/prevMonth" android:src="@drawable/cal_left_arrow_off2"
        android:layout_width="wrap_content" android:layout_height="wrap_content" />
    <ImageView android:id="@+id/nextMonth" android:src="@drawable/cal_right_arrow_off2"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_alignParentRight="true" />
    <Button android:id="@+id/currentMonth" android:layout_weight="0.6"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:background="@drawable/calendar_bar2" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/prevMonth"
        android:layout_toLeftOf="@id/nextMonth"
        android:layout_alignBottom="@id/prevMonth"
        android:layout_alignTop="@id/prevMonth" />
    <ImageView android:id="@+id/calendarheader" android:src="@drawable/calendarheader"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:minHeight="25dp"
        android:layout_centerInParent="true"
        android:layout_below="@id/currentMonth"
        android:layout_margin="0dp" 
        android:scaleType="fitXY" />
    <GridView android:id="@+id/calendar" android:numColumns="7"
        android:layout_gravity="top" android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/calendarheader"
        android:layout_margin="0dp" 
        android:background="@color/white_fader_end">
    </GridView>
</RelativeLayout>

I've added to it my resources as background (two of them are actually screenshots from yours). What i observed is, you must

  • either use a static image for your calendar header background, or,
  • if you use an xml drawable, you have to drop the stroke element from it / set its android:width to 0

otherwise it will add an unnecessary gap around your view's drawable, that is as wide as the stroke width you've specified.

my output using static images as backgrounds looks this way:

no gap 2

Update 2

The problem is not between your calendar header and gridView, but inside your gridView.
It has a 'padding' of 5 px inside (not real padding, see how it gets there):

The default selector for the GridView is a 9x9 patch rectangle with a border of 5px.
That 5 px is visible all around your gridView. You can specify a different listSelector for this grid either by xml:

    android:listSelector="@drawable/grid_selector"

or from code (onCreate):

    gridView.setSelector(R.drawable.grid_selector);

Now if your grid_selector is an image or is a drawable xml without any margins, then the gap that causes the problem is gone.

You can also hide that gap, by setting a background color to your gridView, that matches or grandients the calendar header's background color.

rekaszeru
  • 19,130
  • 7
  • 59
  • 73
  • its not working for me. and I don't have empty pixels in my image. – Shekhar Apr 16 '11 at 09:04
  • Then please share a screenshot too, maybe we'll be able to figure out what is going on there. Does your GridView also have a background image/drawable? Otherwise how do you know about the gap? (this is why i applied a background to the grid too) – rekaszeru Apr 16 '11 at 09:07
  • I have screen shot but how to add it as comment or something. could you please send me your mail address then I can mail you my screen shot – Shekhar Apr 16 '11 at 09:41
  • You should edit your question, click on the `add image` button, and upload it. It will then be a part of your question. But if you have links to your screeshots, it's ok too. – rekaszeru Apr 16 '11 at 09:46
  • http://www.4shared.com/photo/wtGIqbOl/Picture1.html this is the link for screen shot – Shekhar Apr 16 '11 at 09:59
  • Thanks for sharing! it looks like your GridView has some margins (you should clear at least the top margin). Also, try giving your calendar header image an exact max-height value, and see if it works. I'm getting to think that what i'm seeing is not from the layout that you've posted and neither from my version... – rekaszeru Apr 16 '11 at 10:04
  • Ya I have posted only two layouts. I have tried max-height value but it has a same problem.... – Shekhar Apr 16 '11 at 10:10
  • I can't figure out a better way to help you, than you post the layout that you have problem with. No code of your component is needed, neither the layout of your cell data, just the one that has the actual gap. – rekaszeru Apr 16 '11 at 10:15
  • Hi Shekhar, are you drawing your calendar header using an xml, or you have it as an image? I've rewritten the layout using RelativeLayout. The thing is, when i use an image (png) for that resource, it lays out correctly, but when i use an xml drawable resource, it adds a gap with a width i've specified as stroke width. When i set the stroke width to 0 or remove that tag from the xml drawable, the problem is gone. – rekaszeru Apr 16 '11 at 17:59
  • I've updated the answer, please let me know if you have any success with that (or if not, then too; but i keep hoping). – rekaszeru Apr 16 '11 at 18:23
  • thanks for your answer. I have updated my xml file with your answer but I have same problem... – Shekhar Apr 18 '11 at 07:13
  • again: are you using an xml drawable, or an image file for the calendar header? if xml: you should remove the stroke tag from it; if a file, you should make sure that you don't have any extra pixels in the margin of it. This should be verified on your GridView too. – rekaszeru Apr 18 '11 at 10:54
  • I am using only image...but there are no extra pixels in the margin of it. – Shekhar Apr 19 '11 at 08:56
  • Would you please update your layout xml, and share the backgrounds for the header and the grid view you use? The output above i've generated using no-margin-image files as backgrounds for both the header and the grid, and i'd like to try out with your exact settings, because something clearly differs there. – rekaszeru Apr 19 '11 at 09:25
  • xml file is same as you provided. You can find images at this link 4shared.com/file/s4dsldrQ/Images.html – Shekhar Apr 19 '11 at 10:36
  • please see my **Update 2** for the description of the problem, and two possible solutions. – rekaszeru Apr 19 '11 at 11:54
  • thanks a lot its working...you have taken so much care about this problem... thank you very much – Shekhar Apr 19 '11 at 15:55
  • very welcome, i've learned a lot from it too (had no idea about this issue in `Update 2`!) – rekaszeru Apr 19 '11 at 15:57
  • Can you post the grid_selector.xml file that is used here! – Anand Sainath Jul 29 '11 at 16:57