Show and Hide Toolbar ActionBar on WebView Scroll in Android Java

How to Show and Hide Toolbar ActionBar when Scrolling Up/Down

I hope you are familiar with the auto hiding and showing feature of the toolbar actionbar in android, basically this is a feature that auto hides the toolbar when user scrolls up but auto shows when user scrolls down. This is a useful feature which enhances user experience by allowing user to have full screen view. This feature is kinda hard to integrate in android webview, because it caused page height issues when i tried it inside a webview with coordinator layout.

Luckily, i found a library that helps hiding and showing the toolbar or any other supported view or element in a breeze inside the webview when scrolling. This library helped me to design some cool webview apps with that feature and here is how you can implement in your WebView app. The name of the Library is observablescrollview

Webview library to Auto Show and Hide ActionBar Toolbar

First add this library into your app by adding com.github.ksoichiro:android-observablescrollview to your dependencies in build.gradle

Next, your webactivity layout file should be like this

I used a custom toolbar with a NoActionbar theme, you will have to do the same

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:override="true"
    android:animateLayoutChanges="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".WebActivity">

   <com.github.ksoichiro.android.observablescrollview.ObservableWebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="0dp">
        </com.github.ksoichiro.android.observablescrollview.ObservableWebView>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

Next, extend your Webview Activity with ObservableScrollViewCallbacks

public class WebActivity extends AppCompatActivity implements ObservableScrollViewCallbacks {

Add WebView  Scroll View Call back

webview =  findViewById(R.id.webview);
        webview.setScrollViewCallbacks(this);

Your app should use a custom toolbar and then call it

final Toolbar toolbar =  findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

Next, add the code to hide and show Toolbar

public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {
  }


  public void onDownMotionEvent() {
  }

  public void onUpOrCancelMotionEvent(ScrollState scrollState) {
      ActionBar ab = getSupportActionBar();
      if (ab == null) {
          return;
      }
      if (scrollState == ScrollState.UP) {
          if (ab.isShowing()) {
              ab.hide();
              //hide();


          }
      } else if (scrollState == ScrollState.DOWN) {
          if (!ab.isShowing()) {
              ab.show();


              //  show()
          }

This is it,if you need any help in implementing it, just contact me

Related Posts

Add Admob ads in recyclerview in android

How to add admob native ads to recyclerview in android java Adding admob native ads into android recyclerview isn’t easy like other admob ads. You will need…

Download WebView Flutter Source Code for Android and iOS

Easily create android and iOS WebView app for your websites, the much-awaited flutter WebView source code is here. Are you looking for a flutter WebView source code…

Fix uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in library [:_flutter_android]

This error can be fixed by modifying build.gradle file in the flutter In android studio: go to android>app>build.gradle and modify it as follows:

Get Your Own Android Mobile App for Your Website Today

Are you looking to expand your online presence and reach more customers? In today’s digital age, having a mobile app is an essential tool to connect with…

Why making an app for your website can help your business to grow

In today’s digital age, it’s more important than ever to have a strong online presence. Whether you’re running a business, a blog, or a personal website, having…

Fix android.content.Intent.migrateExtraStreamToClipData(android.content.Context)’ on a null object reference

Here is how to Fix Android Studio Error Attempt to invoke virtual method ‘boolean android.content.Intent.migrateExtraStreamToClipData(android.content.Context)’ on a null object reference This error is caused by a number…

Leave a Reply

Your email address will not be published. Required fields are marked *