John Ruban Singh
John Ruban Singh

Reputation: 1326

How to show the Scrollbar in a NestedScrollView Programatically.

NestedScrollView nestedScrollView  = (NestedScrollView) findViewById(R.id.content);
nestedScrollView.setVerticalScrollBarEnabled(true);

setVerticalScrollBarEnabled is not working in above code.

Upvotes: 2

Views: 2100

Answers (3)

Sagar BHomkar
Sagar BHomkar

Reputation: 67

first in xml add attribute in: android:scrollbars="vertical" for NestedScrollView and then in java code:

mNestedScrollView.setVerticalScrollBarEnabled(true);
mNestedScrollView.setScrollBarFadeDuration(0);

if remove that: android:scrollbars="vertical" from xml then it does not work

Upvotes: 0

John Ruban Singh
John Ruban Singh

Reputation: 1326

After some research found workaround for this problem. In android sdk view level scroll invoked from xml attributes. Do the following steps.

Step 1: create a xml file with android:scrollbars="vertical" and place it inside xml folder(inside res create xml folder and place the file)

<?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:scrollbars="vertical"/>

Step 2: In Java file, add the following code snippet

NestedScrollView nestedScrollView  = new NestedScrollView(getBaseContext(),getAttributeSet());

private AttributeSet getAttributeSet() {
        AttributeSet attr = null;
        try {
            XmlPullParser parser = getResources().getXml(R.xml.xml);
            try {
                parser.next();
                parser.nextTag();
            } catch (Exception e) {
                e.printStackTrace();
            }

            attr = Xml.asAttributeSet(parser);
            return attr;
        } catch (Exception e) {
            return attr;
        }
    }

Upvotes: 0

Fenil Patel
Fenil Patel

Reputation: 1546

There are 2 ways:

from Java code: NesteadScrollView.setScrollbarFadingEnabled(true);

from XML code: android:fadeScrollbars="true"

Simple as that!

Upvotes: 3

Related Questions