Question

I am building a webview for an Android app.

For desktop browsers, I can add overflow-y: scroll on the <body> tag to force a scrollbar track onto the page, but this does not force an actual scrollbar.

I'm being asked to "force a scrollbar" in the app, but I'm concerned it will do just that, put a track in place but not the actual scrollbar.

Is there a way to force the scrollbar to be visible no matter what version of Android the webview is loaded into?

What are the options on the Android side and on the HTML+CSS side?

Was it helpful?

Solution

As of now the best way is to use android:fadeScrollbars="false" in xml which is equivalent to ScrollView.setScrollbarFadingEnabled(false); in java code.

or

Setting the android:scrollbarFadeDuration="0" will do the trick.

Eg: in your onCreate() method : try this :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_web_view_layout); 

    browser = (WebView)findViewById(R.id.your_webview);
    WebSettings mWebSettings = browser.getSettings();
    mWebSettings.setBuiltInZoomControls(true);
    browser.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    browser.setScrollbarFadingEnabled(false);
    browser.loadUrl("file:///android_asset/index.html");
}

OR

Try using WebView inside a ScrollView and use android:fadeScrollbars="false" for the ScrollView

Eg:

<ScrollView android:layout_width="fill_parent" 
            android:layout_height="86px" 
            android:id="@+id/scrollTxtDescription"
            android:fadeScrollbars="false"
            android:layout_below="@id/txtLieuPromo1" 
            android:layout_alignLeft="@id/txtLieuPromo1">

            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/layoutTxtDescription"
                >

                <WebView android:id="@+id/txtDescription" 
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    />
            </LinearLayout>
        </ScrollView>

OTHER TIPS

In your code you can try this:

webView.setScrollbarFadingEnabled(false);

This will disable the scrollbars from fading away.

Just Add these lines in your activity to the webview, and no need to add anything in xml,this will lead both horizontal and vertical scroll.

     webview.getSettings().setLoadWithOverviewMode(true);

                    webview.getSettings().setJavaScriptEnabled(true);

                    webview.loadData("Your Content to show in the webview,
                            "text/html; charset=UTF-8", null);

try with this code, hope you will get the solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top