I have a webview working but not all the way. I can't get it it load scripts in html

This commit is contained in:
0ceanSlim 2023-07-28 23:30:07 -04:00
parent 4b49a97b7b
commit 1243596392
5 changed files with 95 additions and 25 deletions

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings"> <component name="GradleSettings">
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<GradleProjectSettings> <GradleProjectSettings>

View File

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">

21
app/assets/script.html Normal file
View File

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<div class="bitcointodollars">
<h1>Bitcoin Price Tracker</h1>
<div id="bitcoin-price"></div>
</div>
<script>
// Fetch the Bitcoin price from the CoinGecko API
fetch('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd')
.then(response => response.json())
.then(data => {
const bitcoinPrice = data['bitcoin']['usd'];
const bitcoinPriceDivided = bitcoinPrice; // Divide the Bitcoin price by 100 million
// Update the Bitcoin price on the website
const bitcoinPriceElement = document.getElementById('bitcoin-price');
bitcoinPriceElement.textContent = `1 Bitcoin = $${bitcoinPriceDivided.toFixed(2)}`; // Display the Bitcoin price
})
.catch(error => console.error(error));
</script>

View File

@ -2,10 +2,76 @@ package com.example.satsmonitor
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle import android.os.Bundle
import android.webkit.WebView
import android.webkit.WebSettings
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
private lateinit var webView: WebView
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
// Get the WebView reference from the layout
webView = findViewById(R.id.webView)
// Enable JavaScript support
val webSettings: WebSettings = webView.settings
webSettings.javaScriptEnabled = true
// Clear the WebView cache (optional)
webView.clearCache(true)
// Load the HTML content with the embedded script and CSS
val htmlContent = """<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
/* CSS styles for the Bitcoin Price Tracker */
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.bitcointodollars {
background-color: #f5f5f5;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
h1 {
color: #333;
}
#bitcoin-price {
font-size: 24px;
font-weight: bold;
color: #0066cc;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="bitcointodollars">
<h1>Bitcoin Price Tracker.</h1>
<div id="bitcoin-price"></div>
<script>
// Fetch the Bitcoin price from the CoinGecko API
fetch('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd')
.then(response => response.json())
.then(data => {
const bitcoinPrice = data['bitcoin']['usd'];
// Update the Bitcoin price on the website
const bitcoinPriceElement = document.getElementById('bitcoin-price');
bitcoinPriceElement.textContent = '1 Bitcoin = $' + bitcoinPrice.toFixed(2); // Display the Bitcoin price
})
.catch(error => console.error(error));
</script>
</div>
</body>
</html>"""
webView.loadDataWithBaseURL(null, htmlContent, "text/html", "UTF-8", null)
} }
} }

View File

@ -6,28 +6,11 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity"> tools:context=".MainActivity">
<TextView <WebView
android:id="@+id/textView2" android:id="@+id/webView"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:text="Hello World!" tools:layout_editor_absoluteX="0dp"
app:layout_constraintBottom_toBottomOf="parent" tools:layout_editor_absoluteY="-356dp" />
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="192dp"
android:layout_marginTop="90dp"
android:layout_marginEnd="192dp"
android:layout_marginBottom="246dp"
android:text="@string/test"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>