Android: image with scrolled overlay

I got inspired with new look of Google Play Market and decided to change the way “Is it kosher?” app shows detailed information with picture.
So, I got an idea to show image with overlay, which will be scrolled down and the picture will be shown in full.

Here is the result of my work:

Solution appeared to be not the simplest one – spend several days on research.
Layout looks as following:



Java code contains the following lines:

int viewHeight = 0;
    int innerViewHeight = 0;
final View view = inflater.inflate(R.layout.itemview,
                container, false);
final LinearLayout innerView = (LinearLayout)view.findViewById(;
ViewTreeObserver vto = view.getViewTreeObserver();
            vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

                public void onGlobalLayout() {

                    viewHeight = view.getHeight();
                    innerViewHeight = innerView.getHeight();

                    innerView.setPadding(0, viewHeight, 0, 0);

                    ViewTreeObserver obs = view.getViewTreeObserver();

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    } else {

            final ScrollView scrollview = ((ScrollView) view.findViewById(;
            scrollview.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                public void onGlobalLayout() {
           Runnable() {
                        public void run() {
                            if (innerViewHeight >= viewHeight)
                                scrollview.scrollTo(0, viewHeight-100);
                            }else {

Some explanations:
– innerView gets padding dynamically. This is done to make sure that there is enough space for showing the picture in fill.
– viewHeight calculations are done in OnGlobalLayoutListener due to the reason, that whole View should be loaded first to obtain the real dimensions.
– I wanted to make overlay to be seen on the moment of page open, but not scrolled fully till the bottom. For that purpose we start new Thread with scrolling in OnGlobalLayoutListener. I haven’t found other way to solve this task and seems to be it is the best one. I want to have a small step-down, that is why there is such solution.

1 comment

Leave a Reply

%d bloggers like this: