Scrolling is delayed when inflating view - android

Hope you can help me, this has been bugging me for quite some time now-
I have an activity that at some point inflates a view which has its own layout and logic behind. This view contains a ScrollView which I want to be scrolled down x dp when its shown to the user (I don't want user to actually see the scrolling, I want ScrollView to be at the right position as the view displays).
Here's what I tried so far:
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.scrollTo(0, someInnerView.getBottom());
}
});
I've tried using this code in different parts of my view logic such as onFinishInflate() but that doesn't work as the data isn't binded to the view at that point. So I added OnGlobalLayoutListener and called the Runnable inside onGlobalLayout() which does work but with a short delay which can clearly be detected.
How do I solve this problem? All I want to do is to have some part of my ScrollView hidden when it's presented to the user.
All help appreciated!
EDIT : to make my intentions clearer- I want to create something similar to this, only that I'm using ScrollView

Related

Setting android view location has no effect

I have a simple view inside of a RelativeLayout that I animate up & down so it slides off and on screen. This works every time except the very first time, where the animation does nothing.
This lead me to trying to set the Y location manually in the activity's onCreate() method. Oddly, that had no effect either.
Is there some reason a view's Y location can't be set until after the activity has been fully displayed? If not, why am I seeing this weird behavior, and is there a way to fix it?
The first time you're calling it's before the view has laid out.
You can get around this by listening for the view to be laid out.
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// Perform whatever actions you want here
}
});

Programatically scroll to the last view in android

I want to scroll to the end of the view. Currently i am trying to scroll the scrollview to the end of the latest added view. I tried with setscrolly, smoothscrollto. both works but the problem is both wont scroll to the end, it just scrolls to the last-1 view. Please suggest me how to overcome this.
I got the reason why it was not scrolling to the last view and was stopping at the last-1 view. I was not smoothScrollTo(x,y) in a post thread of the view. Since my scrollview is not refreshed when the last view is added it was not scrolling to the last view.
view.post(new Runnable() {
public void run() {
scrollview.smoothScrollTo(x,y);
});
Try to use transcript mode param of list view. It provides scrolling to new items automatically:
getListView().setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);

TabHost inside a ScrollView forces it it to scroll to the bottom

My question is very similar to this unanswered one, with some small differences that I will explain:
TabHost inside of a Scrollview: always scrolls down when a Tab is clicked
My ScrollView initiates scrolled to the bottom, showing the TabHost content instead of the ones on top of it (you can see the screenshots in the linked question, it's very similar).
If I manually scroll up, clicking in different tabs doesn't affect the ScrollView, the problem is only in the first time it's shown. This is the difference between the other question.
Things I tried with no success:
Make the top components inside the ScrollView focusable, so they would get the focus instead of the tab.
Force the ScrollView to scroll to the top in onResume() method with sv.fullScroll(View.FOCUS_UP); or sv.scrollTo(0, 0);. Also, no luck in the initialization, but subsequent calls to onResume() effectively scrolls it to the top.
Any ideas on why this is happening? Tips on how to further investigate are also very welcome.
Probably too late to help you Pedro, but I did figure out a solution that worked for me. I noticed in the TabHost source that it requests Focus if nothing else in the view has focus. So I made sure that the first component in the Scroll view was focusable and requested focus for it.
// Get the first component and make sure it is focusable. Note you have to use setFocusableInTouchMode and not setFocusable for this to work.
TextView v = (TextView) view.findViewById(R.id.first_component_in_view);
v.setFocusableInTouchMode(true);
v.requestFocus();
Hopefully this will help someone.
I have a similar problem and haven't solved it. However, to force the scroll view to scroll you need to update it's progress through a separate thread to post on the UI. e.g.
quickScroll.post(new Runnable() {
#Override
public void run() {
quickScroll.scrollTo(0, 54);
}
});
Where quickScroll is my ScrollView. I believe the reason for this is that you have diagrammatically defined your layout, or inflated it from a custom viewgroup. So setContentView doesn't hold a reference to the scrollView, so you have to force the UI to update off the variable allocated to the ScrollView.

How do you find the bottom scrollable position in a ScrollView?

In an android scrollView, how do you find the lowest position in the scrollview so you can then use the scrollTo(x,y) method to scroll right to the bottom. I need the actual numeric value. Using the getHeight() method does not ever return the right value. The scrollbar will sometimes scroll to the end, and sometimes not reach it. There is a linearlayout with a textview in the scrollview. There has to be something that I am missing?
If you want to just scroll to the end, you should be able to just use fullScroll(ScrollView.FOCUS_DOWN). If you need a particularly numerical value based on the height of the scroll view, you will have to subclass your scroll view and override onSizeChanged, which will give you the actual numerical height. Keep in mind onSizeChanged will only be called after onResume, and will not necessarily be called every time your activity resumes.
Part of the reason why you sometimes scroll to the end and sometimes don't is probably because of a race condition. Sometimes your scroll happens before other important things (like populating the view) have happened, so it doesn't appear to do anything.
Try posting your calls to your scroll view's handler:
yourScrollView.post(new Runnable(){
#Override
public void run(){
yourScrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
};
Sometimes even adding a short delay can help:
yourScrollView.postDelayed(new Runnable(){
#Override
public void run(){
yourScrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
}, 200);
Thanks for the response. I found a quick hack if anyone is interested.
sView.fullScroll(View.FOCUS_DOWN);
sView.fullScroll(View.FOCUS_DOWN);
y = sView.getScrollY();
sView.fullScroll(View.FOCUS_UP);
This will jump down to the bottom of the scrollable, collect y, then jump up again. It happens so fast it is not visible and it will get you the bottom value of y every time. You have to call focus down twice or else y will return 0.

ScrollView steals focus when scrolling

I am trying to make a chatting window.
There's a TextView wrapped in a ScrollView in the upper side of the screen; and a EditText in the bottom of the screen.
I use following code to make the ScrollView scroll automatically when the new message is out of view.
scroll.post(new Runnable(){
#Override
public void run() {
scroll.fullScroll(View.FOCUS_DOWN);
}
});
Something annoying is when the ScrollView scrolls, it steals my focus from the EditText.
I can't input my message when the ScrollView is scrolling.
Can anybody give me some light? Thanks a lot : )
I know a workaround. Call requestFocus on the view that should keep the focus.
It would be nice to know if you solved it in a better way.

Categories

Resources