Scroll at the bottom of the activity on onclick listener
Asked Answered
S

5

3

In my Android app, I have a few views (Buttons, EditTexts etc.) not visible initially. These are visible only on a particular button click event. I wish to move the scrollview to the end of these views so that these are visible to the user.

The relevant portion of the layout file:

    <ScrollView...>

    <RelativeLayout android:id="@+id/RelDevDet"
            android:layout_width="fill_parent" android:layout_height="wrap_content">
            .
            .
            .
            <ImageButton android:id="@+id/ibRateApp"
                android:layout_toRightOf="@id/tvRateApp"
                android:layout_alignTop="@id/tvRateApp"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:background="@drawable/arrow_up" android:clickable="true"
                android:layout_marginRight="5dp"
                android:paddingBottom="10dp"
                 />
            <RatingBar android:id="@+id/rtApp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tvRateApp"
                android:layout_marginLeft="5dp" android:visibility="gone"/>
            <EditText android:layout_width="fill_parent"
                android:id="@+id/etRateApp"
                android:layout_height="wrap_content" 
                android:lines="3"
                android:layout_below="@+id/rtApp"
                android:layout_marginRight="5dp"
                android:layout_alignLeft="@+id/tvDevWeb"
                android:visibility="gone" />
            <Button android:id="@+id/btSubRat" android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:layout_below="@+id/etRateApp" android:layout_margin="5dp" android:text="Submit" android:visibility="gone"/>
        </RelativeLayout>
        </ScrollView>

The onclicklistener for the button is:

public void onClick(View v) {
if(v.getId()== R.id.ibRateApp){
            Log.d("amit","scroll end");
            rtApp.setVisibility(View.VISIBLE);
            etRateApp.setVisibility(View.VISIBLE);
            //How to get the scrollview move to the end of the page here??
        }
}
Surfacetosurface answered 4/12, 2012 at 8:26 Comment(0)
S
8
scroll.fullScroll(View.FOCUS_DOWN);

This fixed it for me.

Surfacetosurface answered 6/12, 2012 at 5:7 Comment(0)
Z
3

If the layout has a hidden view it's safe to use post delay so that the method will only be called after making the view visible so that it work scroll down to the end of the page by considering the hidden view

int SPLASH_TIME_OUT = 10;

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                invisibleview.setVisibility(View.VISIBLE);
                scrollView.fullScroll(View.FOCUS_DOWN);
                if (invisibleview.getVisibility() == View.VISIBLE){

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            // This method will be executed once the timer is over
                            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
                        }
                    }, SPLASH_TIME_OUT);
                    scrollView.fullScroll(View.FOCUS_DOWN);
                }
            }
        });
Zuckerman answered 27/6, 2019 at 10:11 Comment(0)
S
0

use

 mScrollViewLayout.scrollTo(0, mScrollViewLayout.getMaxScrollAmount ());
Shwalb answered 4/12, 2012 at 8:30 Comment(5)
Thanks. Do I need to do anything else here. Adding this line gives me a null pointer exception, although I have used defined and used ScrollViewLayout in other parts of code as well. Anything to do for getMaxScrollamount()?Surfacetosurface
No, this should be sufficient. Try to find why you are getting the null pointer exception on mScrollViewLayout if you have initialised it properlyShwalb
The null pointer exception is not an issue. The problem was that the variable was duplicated in an inner block of code and was not initialized. But still, getMaxScrollAmount() method does not lead me to the end of the scrollview. Perhaps it is somewhat related to this problem #7636403 I ll try to fix this and post a solution. In case you come up with something, do let me know. ThanksSurfacetosurface
Does the scrollview scroll a little or none at all?Shwalb
It scrolls but only around to the middle of the page and not the end.Surfacetosurface
D
0
 await Task.Delay(500);
 scrollView1.FullScroll(FocusSearchDirection.Down);

FOR XAMARIAN ANDROID IT WORKS FOR ME

Demetricedemetris answered 24/8, 2023 at 12:21 Comment(0)
M
-2

//Hey sir, i would urge you instead of writing to much code , just call the ScrollView with findviewByid on the onclickLIstener method add.

    scrollview.fullScroll(View.FOCUS_UP); Or
    scrollview.fullScroll(View.FOCUS_DOWN); 
Monolatry answered 12/3, 2020 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.