Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically scroll to end of screen

I have implemented TapTargetView library to my app.

After passing a certain element, I need to focus on the next view which is outside the screen at this moment:

@Override
public void onSequenceStep(TapTarget lastTarget) {
  if (lastTarget.id() == 7) {
     flavorContainer.setFocusableInTouchMode(true);
     flavorContainer.requestFocus();
  }
}

Everything was fine, before I added the ad unit at the bottom of the screen. So now the necessary element is displayed behind the ads.

enter image description here

Method requestFocus() scrolls layout only to the necessary view was visible but not to the end of screen.

enter image description here

I need a method that scrolls the contents of the screen to the VERY END, not just what would the necessary view become visible on the screen. Is it possible?

enter image description here

Layout structure

<android.support.design.widget.CoordinatorLayout>
<LinearLayout>
<android.support.v4.widget.NestedScrollView> 
<LinearLayout> 
<android.support.v7.widget.CardView> 
<LinearLayout>

</LinearLayout> 
</android.support.v7.widget.CardView> 
</LinearLayout> 
</android.support.v4.widget.NestedScrollView> 
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
like image 299
Haze Avatar asked Mar 18 '17 14:03

Haze


People also ask

How do I automatically scroll to the bottom of the page?

To use you just need to press CTRL+ Left click of your mouse and drag the mouse a bit in the direction you want to scroll the page. For example, if you want to scroll up to the page automatically, click CTRL+ left click and slightly move your mouse upwards, the tool will start scrolling up the page.

How do I scroll to the end of a div?

In jQuery, we can automatically use the scrollTop() and height() methods to scroll a page from top to bottom. For example, JQuery scroll to the bottom of div set the scrollTop property of a div's JavaScript to the value of scroll height property to scroll to the bottom of div.

How do I scroll to the bottom of the page on Android?

Open a webpage in a browser on your Android device. Make sure the webpage is long enough so that the browser shows a scroll bar. Once there, to get to the bottom of the page, simply tap the top right corner on your device. In the screenshot below, I need to tap the area where the time is shown.


1 Answers

You have two possible solutions with pros and cons.

First

Use the method fullScroll(int) on NestedScrollView. NestedScrollView must be drawn before using this method and the focus will be lost on the View that gained it before.

nestedScrollView.post(new Runnable() {
    @Override
    public void run() {
        nestedScrollView.fullScroll(View.FOCUS_DOWN);
    }
});

Second

Use the method scrollBy(int,int)/smoothScrollBy(int,int). It requires much more code, but you will not lose the current focus:

nestedScrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        final int scrollViewHeight = nestedScrollView.getHeight();
        if (scrollViewHeight > 0) {
            nestedScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

            final View lastView = nestedScrollView.getChildAt(nestedScrollView.getChildCount() - 1);
            final int lastViewBottom = lastView.getBottom() + nestedScrollView.getPaddingBottom();
            final int deltaScrollY = lastViewBottom - scrollViewHeight - nestedScrollView.getScrollY();
            /* If you want to see the scroll animation, call this. */
            nestedScrollView.smoothScrollBy(0, deltaScrollY);
            /* If you don't want, call this. */
            nestedScrollView.scrollBy(0, deltaScrollY);
        }
    }
});
like image 186
Giorgio Antonioli Avatar answered Sep 18 '22 15:09

Giorgio Antonioli