Which layout should be used to display the "footer" depending on the rest of the contents of the screen

First of all, my apologies for the title. I thought about choosing a more descriptive name for a long time, but I could not find it.

I have a screen with a title (variable length - using a sliding action bar), a middle part (scrollview) and a lower part for showing ads (fixed length). I can write a screen using RelativeLayout, but I would really like:

  • If the scroll content (middle part) is not enough to fill the screen (including the title), I would like to show an ad at the bottom of the screen.
  • If the title of the scroll content + is larger than the screen, I would like to show an ad BELOW the contents of the scroll content, which means that it does not appear if the user does not scroll the bottom of the fragment.

Present the scroll content as short text or image + long text. In the first case, the scroll content will be short, and therefore I would ideally show the ad at the bottom of the screen, since I have enough space, but if it is an image + text, it will be larger than the height of the screen and as such, I would like to show the ad after scrolling user to the bottom.

The reason I want to do this is because the ad does not necessarily take up space if there is useful content on the screen (user interface).

Is there anyway to achieve this goal in android without writing my own layout? If so, what kind of performance would you recommend?

Thank you very much in advance,

+5
source share
1 answer

I don’t think there is such a layout. You can do this programmatically. ScrollView always has one child, so you can get the height of the content this way:

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

You can get the height of the window 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 math to decide where to place the footer.

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

The disadvantages of this approach are that you must do all the magic manually.

Hope this helped you :)

+1
source

Source: https://habr.com/ru/post/1208866/


All Articles