webview is forcing my scrollview to the bottom - android

I have a webview in a scrollview, when the Activity loads, it forces my scrollview to the bottom (where the webview is) once the webview finishes "loadData".
How do I keep this from happening?
I've tried this, but it jumps the screen up and down, which I don't want:
ScrollView scroll = (ScrollView)findViewById(R.id.detailsScroll);
scroll.post(new Runnable()
{
#Override
public void run()
{
ScrollView scroll = (ScrollView)findViewById(R.id.detailsScroll);
scroll.fullScroll(ScrollView.FOCUS_UP);
}
});

The answer of fhucho is OK for stoping the webview from scrolling to bottom. But you will lose all accessibility with trackball, etc. So I found an improvement :
public class MyScrollView extends ScrollView {
#Override
public void requestChildFocus(View child, View focused) {
if (focused instanceof WebView )
return;
super.requestChildFocus(child, focused);
}
}
Hope it helps !

Create a custom ScrollView with this method:
#Override
public void requestChildFocus(View child, View focused) {
if (child instanceof WebView) return;
}

For focus related issues, I found this worked better, adding these to your WebView in XML:
android:focusable="false"
android:focusableInTouchMode="false"
Although for the actual question asked, just adding another view that gets the focus by default, instead of the WebView, should have resolved that issue. I think others like me might find this when trying to deal with other general issues with a WebView inside a ScrollView taking the focus. Adding the above to the XML does not stop hyperlinks in the WebView from working.

add this to your main layout
android:descendantFocusability="blocksDescendants"

Related

Android - Scrollview start at the bottom

Is there anyway I can make a Scrollview start on bottom? I tried to:
post(new Runnable() {
#Override
public void run() {
fullScroll(ScrollView.FOCUS_DOWN);
}
});
And also setting the android:focusableInTouchMode="true" on the lowest element in the layout.
But I can see it scrolling to the bottom sometimes. The idea is to make it imperceptible to the user.
Thanks a lot.
try with scrollView.fullScroll(View.FOCUS_DOWN)
Try this
scrollView.scrollTo(0, scrollView.getBottom());
Since I was adding Views dynamically, I also had to allow Android to complete the layout on the content view inside the ScrollView. I just added a post at the end of the View's queue, that will run after it has completed its layout. Otherwise, Android was trying to use the old size of the content view, and wasn't scrolling all the way down.
mMessagesLayout.addView(messageLayout);
mMessagesLayout.requestLayout();
mMessagesLayout.post(new Runnable() {
#Override
public void run() {
mScrollView.scrollTo(0, mScrollView.getBottom());
}
});

Adjust scroll position when soft keyboard opens

I have a form within a ScrollView. When I tap into an EditText the soft keyboard appears and the ScrollView scrolls the now focused EditText so that it just comes into view.
However, I have hint information just below the EditText that I also would like to show, so the scrolling should go just a bit further up, like this:
The EditText is embedded in a form element and actually I'd like to scroll to the bottom of that. I've checked the source code of ScrollView and it will just scroll to the bottom of the currently focused view. Maybe there's a way to tell the ScrollView that the form element is the currently focused element?
Of course I could write my own ScrollView sub class and override the scroll behavior, but I wonder if there's a more elegant way of doing this.
Any other suggestions (with adjust scrolling with a fixed offset or so) are also appreciated.
I have not really found any way to configure the scrolling behavior of the ScrollView from the outside. So I ended up to define my own sub class of ScrollView:`
/**
* {#link ScrollView} extension that allows to configure scroll offset.
*/
public class ConfigurableScrollView extends ScrollView {
private int scrollOffset = 0;
public ConfigurableScrollView (final Context context, final AttributeSet attrs) {
super(context, attrs);
}
public void setScrollOffset (final int scrollOffset) {
this.scrollOffset = scrollOffset;
}
#Override
protected int computeScrollDeltaToGetChildRectOnScreen (final Rect rect) {
// adjust by scroll offset
int scrollDelta = super.computeScrollDeltaToGetChildRectOnScreen(rect);
int newScrollDelta = (int) Math.signum(scrollDelta) * (scrollDelta + this.scrollOffset);
return newScrollDelta;
}
}
computeScrollDelta(...) is the only protected method that can be targeted for overriding, apart from onSizeChanged(...).
The signum function in the example above ensures that scrolling is only increased, if the ScrollView really thinks that scrolling is necessary (e.g. when keyboard pops up).
I can now set the extra scroll offset once from the outside, as calculated from the height of the hint.
It's not hard to use the extended ConfigurableScrollView instead of the standard ScrollView, I only had to replace the ScrollView XML tag with the FQN of the new class.
Considering that you are using ScrollView you have the possibility to use the method ScrollTo as follow:
scrollView.post(new Runnable() {
#Override
public void run() {
sv.scrollTo(x-value, y-value);
}
});
where the first argument is the scroll value for X, while the second argument is the scroll value for Y. So you just have to set your scrollView offset when the keyboard is displayed.
Hope it helps;)

Multi-View Android ScrollView scrollTo Popover not working

My app opens up in View A. In View B, I have made a custom popover who's view contains:
LinearLayout
ScrollView
LinearLayout1
LinearLayout2
.
.
.
LinearLayoutN
What I wish to do is From View A, Move Into View B having set the Vertical Scroll position to a specific LinearLayout from the ScrollView within the Popup.
I have the scrollview being assigned in code to a variable and
variable.scrollTo
is NOT working.
I have also tried to put
sView.post(new Runnable() {
#Override
public void run() {
sView.scrollTo(0, myNum);
}
});
within the routine I run to setup View B, Tried it Outside in the OnCreate routine. Nothing seems to be working to scroll the ScrollView.
The
sView.post(new Runnable() {
#Override
public void run() {
sView.scrollTo(0, scrollYPos);
}
});
was EXTREMELY useful, however its placement was even more important. Do to the scrollview being inside of a popover view, the code above actually had to be placed AFTER the code that truly brought that popover onto screen.

Android: Hiding a view off the top edge of the screen

Would it be possible to hide a view off the top edge of the screen, and only have it appear if the user scrolls upwards?
My first attempt used a scrollview, but it seems that scrollTo() doesn't work unless I used postDelayed (it doesn't even work with Post()). I tried adding it to the scrollview's view tree observer onPreDraw() event and it still doesn't work unless I delay it, so there is an ugly glitch when the activity is first launched.
The second issue is that if the onscreen keyboard is minimized, the view no longer needs to scroll so hiding things by using a scroll offset no longer works. I thought about manipulating the height in code, but this seems pretty hackish.
Is there a better way to do this than by using a scrollview? Alternatively, Does anyone have any tips on the best place to place the scrollTo (the end of onCreate does not work nor the other places I have tried) so I don't need to use postDelayed? That would at least eliminate one glitch.
Thanks!
This is the code I'm using right now, which is the least glitchy but I don't understand why it doesn't work until the third time onPreDraw() is called.
mScrollView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener()
{
#Override
public boolean onPreDraw()
{
final int fieldYStart = mFieldIWantAtTheTop.getTop();
if (mFieldIWantAtTheTopYStart != fieldYStart
|| mScrollView.getScrollY() < 10)
{
mFieldIWantAtTheTopYStart = fieldYStart;
mScrollView.post(new Runnable()
{
#Override
public void run()
{
Log.v("Testing", "scrolling!");
mScrollView.scrollTo(0, mFieldIWantAtTheTopYStart);
Log.v("Testing", "scroll is now=" + mScrollView.getScrollY());
}
});
}
return true;
}
});
I also tried using a custom scrollview as mentioned below, but this does not solve the issue of the graphical glitch:
#Override
public void onMeasure(int measureWidthSpec, int measureHeightSpec) {
super.onMeasure(measureWidthSpec, measureHeightSpec);
Log.v("Testing", "Scrolling");
post(
new Runnable()
{
public void run()
{
scrollTo(0, 100);
Log.v("Testing", "ScrollY = " + getScrollY());
}
});
}
This code works as does the onPreDraw() code above but there is still a glitch when the activity is launched because the activity is first drawn with the scroll at 0.
I haven't tried this, but you may want to create a custom ScrollView and override onMeasure:
ScrollView scroll = new ScrollView(this) {
#Override
public void onMeasure(int measureWidthSpec, int measureHeightSpec) {
super.onMeasure(measureWidthSpec, measureHeightSpec);
scrollTo(...);
}
};
It seems like this would be the earliest point that scrollTo would be valid.
Edit - I found this answer, which apparently worked for the asker. Is this the method you tried?

Reset scroll in ScrollView

I have a ScrollView (and a LinearLayout within it) set as main content. When the user scrolls the view further down, and then if I replace it's child (LinearLayout) with another LinearLayout, the view remains in the scrolled positioned.
How do I reset the ScrollView back to coordinate 0?
Why not just use scrollTo(0, 0);?
Link: http://developer.android.com/reference/android/view/View.html#scrollTo(int%2C%20int)
If u want to clear the scrollview u can use
removeAllViews();
Its all in the documentation... http://developer.android.com/reference/android/widget/ScrollView.html search for fullScroll(int)
When scrollView.scrollTo(0, 0) & scrollView.fullScroll(View.FOCUS_UP) doesn't work, you can accomplish the intended with:
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.scrollTo(0, 0);
}
});
yourScrollView.scrollTo(0,0) will do the job just fine.
Just use
ScrollView hscrollViewMain = (ScrollView)findViewById(R.id.scrollViewMain);
hscrollViewMain.scrollTo(0, 0);

Categories

Resources