5

First, my apologies for the title. I thought long and hard to choose a more descriptive title but couldn't really find one.

I have a screen with a header (variable length - using sliding action bar), a middle part (scrollview) and a bottom part for showing ads (fixed length). I am able to write the screen using RelativeLayout but what I would really like to do is this:

  • If the scrollview's content (the middle part) is not long enough to fill the screen (including header), I would like to show the ad at the bottom of the screen
  • If the scrollview's content + header is longer than the screen, I would like to show the ad BELOW the scroll view's contents, meaning it's not visible unless the user scrolls to the bottom of the fragment.

Imagine the scroll view's content as a short text or an image + a long text. For the first case, the scroll view content will be short and therefore I'd ideally show the ad at the bottom of the screen since I have enough space but if it's image + text, it will be larger than the screen height and as such, I would like to show the ad after the user has scrolled to the bottom.

The reason I want to do this is so that the ad doesn't necessarily take space if there's useful content on the screen (user experience).

Is there anyway to achieve this in android without writing my own custom layout? If so, which view would you recommend?

Many thanks in advance,

kha
  • 19,123
  • 9
  • 34
  • 67
  • 1
    possible duplicate of [View inside ScrollView doesn't take all place](http://stackoverflow.com/questions/10211338/view-inside-scrollview-doesnt-take-all-place) – corsair992 Dec 20 '14 at 16:41
  • @corsair992 Thank you. This is what I was after. I don't know if you can put it as answer since it's a duplicate, but if you can, please do it so that I can accept your answer and give you your bounty :). Again, thank you very much. I would've never found about this otherwise. – kha Dec 22 '14 at 08:00
  • 1
    I flagged this for a moderator to close as duplicate and refund the bounty (only moderators can close bounty questions), but no one has gotten around to it yet. I am not interested in collecting the bounty, but if you like you can post a self-answer and accept it. – corsair992 Dec 22 '14 at 09:06

1 Answers1

1

I don't think there is such a layout. You can do this programmatically. The ScrollView always has a single child so you can get the content height this way:

int contentHeight = scrollView.getChildAt(0).getHeight();

You can get window height this way

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int windowHeight = size.y;

The height of the header and footer is fixed so you can do the maths decide where to put the footer.

int totalHeight = heaederHeight + contentHeight + footerHeight
if(totalHeight < windowHeight) {
    // add the footer to the scrollview
} else {
    // add the footer below the scrollview
}

The cons of this approach is that you have to do all the magic manually.

I hope this helped you :)

Kiril Aleksandrov
  • 2,601
  • 20
  • 27
  • Thank you Kiril for your prompt response. Let me check the solution and get back to you. – kha Dec 20 '14 at 14:34
  • 2
    Actually, the `fillViewport` attribute was designed for just this purpose. See Romain Guy's blog post on it: http://www.curious-creature.com/2010/08/15/scrollviews-handy-trick – corsair992 Dec 20 '14 at 16:52