Mobile Apps 2023W Lecture 6: Difference between revisions
No edit summary |
No edit summary |
||
Line 4: | Line 4: | ||
* [https://developer.android.com/develop/ui/views/layout/webapps/webview Build web apps in WebView] | * [https://developer.android.com/develop/ui/views/layout/webapps/webview Build web apps in WebView] | ||
* [https://www.geeksforgeeks.org/webview-in-android-using-jetpack-compose/ Android WebView & Jetpack Compose] | * [https://www.geeksforgeeks.org/webview-in-android-using-jetpack-compose/ Android WebView & Jetpack Compose] | ||
==Code== | |||
<syntaxhighlight lang="kotlin" line> | |||
package carleton.comp2601.webviewcompose | |||
import android.os.Bundle | |||
import android.view.ViewGroup | |||
import android.webkit.WebView | |||
import android.webkit.WebViewClient | |||
import androidx.activity.ComponentActivity | |||
import androidx.activity.compose.setContent | |||
import androidx.compose.material.* | |||
import androidx.compose.runtime.Composable | |||
import androidx.compose.ui.graphics.Color | |||
import androidx.compose.ui.tooling.preview.Preview | |||
import androidx.compose.ui.viewinterop.AndroidView | |||
class MainActivity : ComponentActivity() { | |||
override fun onCreate(savedInstanceState: Bundle?) { | |||
super.onCreate(savedInstanceState) | |||
setContent { | |||
// Calling the composable function | |||
// to display element and its contents | |||
MainContent() | |||
} | |||
} | |||
} | |||
// Creating a composable | |||
// function to display Top Bar | |||
@Composable | |||
fun MainContent() { | |||
Scaffold( | |||
topBar = { TopAppBar(title = {Text("COMP 2601")})}, | |||
content = { MyContent() } | |||
) | |||
} | |||
// Creating a composable | |||
// function to create WebView | |||
// Calling this function as | |||
// content in the above function | |||
@Composable | |||
fun MyContent(){ | |||
// Declare a string that contains a url | |||
val mUrl = "https://news.ycombinator.com" | |||
// Adding a WebView inside AndroidView | |||
// with layout as full screen | |||
AndroidView(factory = { | |||
WebView(it).apply { | |||
// layoutParams = ViewGroup.LayoutParams( | |||
// ViewGroup.LayoutParams.MATCH_PARENT, | |||
// ViewGroup.LayoutParams.MATCH_PARENT | |||
// ) | |||
webViewClient = WebViewClient() | |||
loadUrl(mUrl) | |||
} | |||
}, update = { | |||
//it.loadUrl(mUrl) | |||
}) | |||
} | |||
// For displaying preview in | |||
// the Android Studio IDE emulator | |||
@Preview(showBackground = true) | |||
@Composable | |||
fun DefaultPreview() { | |||
MainContent() | |||
} | |||
</syntaxhighlight> |
Latest revision as of 20:33, 8 February 2023
Resources
Code
package carleton.comp2601.webviewcompose
import android.os.Bundle
import android.view.ViewGroup
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.viewinterop.AndroidView
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
// Calling the composable function
// to display element and its contents
MainContent()
}
}
}
// Creating a composable
// function to display Top Bar
@Composable
fun MainContent() {
Scaffold(
topBar = { TopAppBar(title = {Text("COMP 2601")})},
content = { MyContent() }
)
}
// Creating a composable
// function to create WebView
// Calling this function as
// content in the above function
@Composable
fun MyContent(){
// Declare a string that contains a url
val mUrl = "https://news.ycombinator.com"
// Adding a WebView inside AndroidView
// with layout as full screen
AndroidView(factory = {
WebView(it).apply {
// layoutParams = ViewGroup.LayoutParams(
// ViewGroup.LayoutParams.MATCH_PARENT,
// ViewGroup.LayoutParams.MATCH_PARENT
// )
webViewClient = WebViewClient()
loadUrl(mUrl)
}
}, update = {
//it.loadUrl(mUrl)
})
}
// For displaying preview in
// the Android Studio IDE emulator
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
MainContent()
}