ScrollView steals focus when scrolling - android

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.

Related

How To Scroll To A Desired Position In ScrollView?

I have an EditText with number input, a button and a ScrollView one below the other.
Inside the ScrollView I have a Linear Layout with vertical orientation and inside the Layout I have about 50 TextViews each with id 1,2,3,4....
What I want is that when the user enters a number in EditText and clicks the button,it should read which number is entered (say 28),then the ScrollView should scroll Textview with id 28 without disturbing the others.
I have no problem if I have to type the same code for each and every TextView even 500 times but it should be a working code like I mentioned above.
I have been trying to achieve this for a while without any success.Please Help!!!
Find the view at that position in LinearLayout and scroll to that view
scrollView.post(new Runnable() {
public void run() {
scrollView.scrollTo(0, parentLinearLayoute.getChidAt(position).getBottom());
}
});
You will have to replace Scrollview and Linearlayout with RecylerView. And you can use smoothScrollToPosition to scroll to a particular position. I think this should be the best approach

Scrolling is delayed when inflating view

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

View in scrollview gets hidden when requestFocus() is called

I have a ScrollView where there are a number of EditText fields.
When i call requestFocus() on any of those fields, the EditText fields comes into view as the scroll view scrolls by itself, even when the soft keyboard is up.
The problem lies in the fact that i have had to add a separate view above the soft keyboard to accommodate a clear button, and it is that custom view that is hiding the edit text field in focus.
Now, i have to manually scroll the scroll view to see the edit text field in focus.
Please help me to show the edit text field in focus above the custom view which is placed on top of the soft keyboard.
Is there any way to do so ?
EDIT:: This is how i solved this problem.
scrollView.post(new Runnable() {
public void run() {
scrollView.scrollTo(0, scrollView.getBottom()-200);
}
});
and gradually went on decreasing the amount i am subtracting from the scrollView.getBottom() call. This worked fine for me.
In your manifest.xml file, for your activity just add this line:
android:windowSoftInputMode="adjustPan"

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.

Categories

Resources