Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Android ScrollView fading edge always visible?

By default scrollview's fading edge is visible only if it is possible to scroll in that direction. How can I make it visible at all times?

I don't want to put any drawables on top or something like that. I want to accomplish it using the builtin fading edge, probably by overriding some scrollview functions.

like image 936
Sebastian Nowak Avatar asked Jul 31 '11 14:07

Sebastian Nowak


1 Answers

Yes, extend ScrollView and override these methods (based on Donut-release2):

@Override
protected float getTopFadingEdgeStrength() {
    if (getChildCount() == 0) {
        return 0.0f;
    }
    return 1.0f;
}

@Override
protected float getBottomFadingEdgeStrength() {
    if (getChildCount() == 0) {
        return 0.0f;
    }
    return 1.0f;
}

For comparison's sake, this is the original code, which shortens the fading edge as you get close to the end of the list:

@Override
protected float getTopFadingEdgeStrength() {
    if (getChildCount() == 0) {
        return 0.0f;
    }

    final int length = getVerticalFadingEdgeLength();
    if (mScrollY < length) {
        return mScrollY / (float) length;
    }

    return 1.0f;
}

@Override
protected float getBottomFadingEdgeStrength() {
    if (getChildCount() == 0) {
        return 0.0f;
    }

    final int length = getVerticalFadingEdgeLength();
    final int bottomEdge = getHeight() - mPaddingBottom;
    final int span = getChildAt(0).getBottom() - mScrollY - bottomEdge;
    if (span < length) {
        return span / (float) length;
    }

    return 1.0f;
}
like image 111
cdhabecker Avatar answered Nov 05 '22 04:11

cdhabecker