sandev
sandev

Reputation: 41

Show splash screen until webview load

I want to show my splash screen until the webview loading complete. Otherwise it shows empty white page and load the page in 3-4 sec delay. I am beginer. Thanks

public class SplashActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    Window window = getWindow() ;
    window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_splash);
    Thread splashTread = new Thread(){



        @Override

        public void run() {

            try {
                sleep(3000);
                startActivity(new Intent(getApplicationContext(),MainActivity.class));
                finish();
            } catch (InterruptedException e) {

                e.printStackTrace();
            }
            super.run();

        }

    };

    splashTread.start();

xml file

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SplashActivity">


<ImageView

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:padding="50dp"
    android:scaleType="centerCrop"
    android:src="@drawable/splash"/>

Upvotes: 0

Views: 385

Answers (1)

Enes Zor
Enes Zor

Reputation: 1220

My question is with Kotlin. You can use Kotlin if you are new. Firstly you can show your splash as a default. While doing that, you can listen to your web view call-back function. When the page is loaded, you can change your view visibility. I think you do not need to thread. If you wanna use it, you can use Kotlin Coroutines instead. It is easy to implement.

webView.webViewClient = object : WebViewClient() {
        override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
            println("Load Started")
        }
        override fun onPageFinished(view: WebView, url: String) {
            println("Load Finished")
        }
    }

Upvotes: 0

Related Questions