Skip to content

Instantly share code, notes, and snippets.

@stevdza-san
Created June 22, 2022 10:16
Show Gist options
  • Save stevdza-san/55a7f3bad490216e750bc36b3544a8bc to your computer and use it in GitHub Desktop.
Save stevdza-san/55a7f3bad490216e750bc36b3544a8bc to your computer and use it in GitHub Desktop.
Web Browser Application using Jetpack Compose
// Using WebView Accompanist library
// implementation "com.google.accompanist:accompanist-webview:<version>"
import android.graphics.Bitmap
import android.util.Log
import android.webkit.WebView
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import com.google.accompanist.web.*
@Composable
fun WebBrowser() {
var url by remember { mutableStateOf("https://stevdza-san.com") }
val state = rememberWebViewState(url = url)
val navigator = rememberWebViewNavigator()
var textFieldValue by remember(state.content.getCurrentUrl()) {
mutableStateOf(state.content.getCurrentUrl() ?: "")
}
Column {
TopAppBar {
IconButton(onClick = { navigator.navigateBack() }) {
Icon(
imageVector = Icons.Default.ArrowBack,
contentDescription = "Back"
)
}
IconButton(onClick = { navigator.navigateForward() }) {
Icon(
imageVector = Icons.Default.ArrowForward,
contentDescription = "Forward"
)
}
Text(
text = "Web Browser", style = TextStyle(
color = Color.White,
fontSize = MaterialTheme.typography.h6.fontSize,
fontWeight = MaterialTheme.typography.h6.fontWeight
)
)
Row(
modifier = Modifier.weight(1f),
horizontalArrangement = Arrangement.End
) {
IconButton(onClick = { navigator.reload() }) {
Icon(
imageVector = Icons.Default.Refresh,
contentDescription = "Refresh"
)
}
IconButton(onClick = { url = textFieldValue }) {
Icon(
imageVector = Icons.Default.Check,
contentDescription = "Go"
)
}
}
}
Row(modifier = Modifier.padding(all = 12.dp)) {
BasicTextField(
modifier = Modifier.weight(9f),
value = textFieldValue,
onValueChange = { textFieldValue = it },
maxLines = 1
)
if (state.errorsForCurrentRequest.isNotEmpty()) {
Icon(
modifier = Modifier
.weight(1f),
imageVector = Icons.Default.Warning,
contentDescription = "Error",
tint = Color.Red
)
}
}
val loadingState = state.loadingState
if (loadingState is LoadingState.Loading) {
LinearProgressIndicator(
progress = loadingState.progress,
modifier = Modifier.fillMaxWidth()
)
}
// A custom WebViewClient and WebChromeClient can be provided via subclassing
val webClient = remember {
object : AccompanistWebViewClient() {
override fun onPageStarted(
view: WebView?,
url: String?,
favicon: Bitmap?
) {
super.onPageStarted(view, url, favicon)
Log.d("Accompanist WebView", "Page started loading for $url")
}
}
}
WebView(
state = state,
modifier = Modifier.weight(1f),
navigator = navigator,
onCreated = { webView ->
webView.settings.javaScriptEnabled = true
},
client = webClient
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment