Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which layout should be used to show a "footer" depending on the rest of the screen content

Tags:

android

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,

like image 818
kha Avatar asked Nov 10 '22 21:11

kha


1 Answers

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 :)

like image 84
Kiril Aleksandrov Avatar answered Nov 14 '22 22:11

Kiril Aleksandrov