Add progress bar in WebViewActivity layout

This commit is contained in:
Shinokuni 2019-10-26 16:25:10 +02:00
parent c1f77edbe1
commit 33997f071b
2 changed files with 45 additions and 11 deletions

View File

@ -2,15 +2,15 @@ package com.readrops.app.activities
import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.PorterDuff
import android.graphics.drawable.ColorDrawable
import android.net.Uri
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import android.view.View
import android.webkit.*
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
@ -33,6 +33,8 @@ class WebViewActivity : AppCompatActivity() {
setWebViewSettings()
binding.activityWebViewSwipe.setOnRefreshListener { binding.webView.reload() }
binding.activityWebViewProgress.indeterminateDrawable.setColorFilter(actionBarColor, PorterDuff.Mode.SRC_IN)
binding.activityWebViewProgress.max = 100
val url: String = intent.getStringExtra(WEB_URL)
binding.webView.loadUrl(url)
@ -51,14 +53,30 @@ class WebViewActivity : AppCompatActivity() {
return true
}
override fun onPageFinished(view: WebView?, url: String?) {
title = view?.title
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
binding.activityWebViewSwipe.isRefreshing = false
binding.activityWebViewProgress.progress = 0
binding.activityWebViewProgress.visibility = View.VISIBLE
super.onPageStarted(view, url, favicon)
}
}
binding.webView.webChromeClient = object : WebChromeClient() {
override fun onReceivedTitle(view: WebView?, title: String?) {
setTitle(title)
supportActionBar?.subtitle = Uri.parse(view?.url).host
binding.activityWebViewSwipe.isRefreshing = false
super.onPageFinished(view, url)
super.onReceivedTitle(view, title)
}
override fun onProgressChanged(view: WebView?, newProgress: Int) {
binding.activityWebViewProgress.progress = newProgress
if (newProgress == 100)
binding.activityWebViewProgress.visibility = View.GONE
super.onProgressChanged(view, newProgress)
}
}
}

View File

@ -31,10 +31,26 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/web_view"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent"
android:orientation="horizontal">
<ProgressBar
android:id="@+id/activity_web_view_progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="4dp"
android:indeterminate="false" />
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>