pub struct WebView { /* private fields */ }Expand description
WebView component for displaying web content
§Important Usage Notes
-
JavaScript Support: If your HTML content contains JavaScript or dynamic effects, you must call
allow_javascript()first to enable JavaScript, otherwise you may see a blank page. -
HTML Content Display Order:
// Step 1: Enable JavaScript (if needed) webview.allow_javascript(&mut activity, true)?; // Step 2: Set HTML content webview.set_data(&mut activity, "<html>...</html>")?; -
Loading External URLs: Simply call
load_uri()- no special order required
§Examples
use termux_gui_rust_demo::prelude::*;
let mut activity = Activity::new()?;
let webview = WebView::new(&mut activity, None)?;
// Method 1: Display HTML content (requires JavaScript to be enabled first)
webview.allow_javascript(&mut activity, true)?;
webview.set_data(&mut activity, "<html><body><h1>Hello</h1></body></html>")?;
// Method 2: Load a webpage
webview.load_uri(&mut activity, "https://www.example.com")?;Implementations§
Source§impl WebView
impl WebView
Sourcepub fn set_data(&self, activity: &mut Activity, data: &str) -> Result<()>
pub fn set_data(&self, activity: &mut Activity, data: &str) -> Result<()>
Sets HTML content
⚠️ Important: If the HTML contains JavaScript or dynamic effects, you must call
allow_javascript(true) first, otherwise you may see a blank page.
§Arguments
activity: Reference to the Activitydata: The HTML document content
§Examples
// If HTML contains JavaScript, enable it first
webview.allow_javascript(&mut activity, true)?;
// Then set the HTML content
webview.set_data(&mut activity, "<html><body><h1>Hello</h1></body></html>")?;Sourcepub fn allow_javascript(
&self,
activity: &mut Activity,
allow: bool,
) -> Result<bool>
pub fn allow_javascript( &self, activity: &mut Activity, allow: bool, ) -> Result<bool>
Allows JavaScript execution
⚠️ Important: When displaying HTML with JavaScript or dynamic effects, you must call this method first to enable JavaScript.
If JavaScript is requested to be enabled, a user confirmation dialog will appear, and the user can deny the request. This method blocks until the user responds.
§Arguments
activity: Reference to the Activityallow: Whether to allow JavaScript
§Returns
Returns whether JavaScript is enabled after the call (if the user denies, it will return false even if you passed true)
§Examples
// Enable JavaScript (requires user confirmation)
let enabled = webview.allow_javascript(&mut activity, true)?;
if enabled {
println!("JavaScript enabled");
// Now you can set HTML with JavaScript
webview.set_data(&mut activity, "<html><body><script>alert('Hi!');</script></body></html>")?;
}Sourcepub fn allow_content_uri(
&self,
activity: &mut Activity,
allow: bool,
) -> Result<()>
pub fn allow_content_uri( &self, activity: &mut Activity, allow: bool, ) -> Result<()>
Allows loading content from content:// URIs
§Arguments
activity: Reference to the Activityallow: Whether to allow loading from content URIs
Allows navigation to different sites
§Arguments
activity: Reference to the Activityallow: Whether to allow users and JavaScript to navigate to different sites
Sourcepub fn evaluate_js(&self, activity: &mut Activity, code: &str) -> Result<()>
pub fn evaluate_js(&self, activity: &mut Activity, code: &str) -> Result<()>
Executes JavaScript code in the WebView
⚠️ Prerequisite: You must enable JavaScript via allow_javascript(true) first,
otherwise the code will not execute.
§Arguments
activity: Reference to the Activitycode: The JavaScript code to execute
§Examples
// Step 1: Enable JavaScript
webview.allow_javascript(&mut activity, true)?;
// Step 2: Execute JavaScript code
webview.evaluate_js(&mut activity, "document.body.style.background = 'red';")?;Sourcepub fn go_back(&self, activity: &mut Activity) -> Result<()>
pub fn go_back(&self, activity: &mut Activity) -> Result<()>
Goes back to the previous page in history
Sourcepub fn go_forward(&self, activity: &mut Activity) -> Result<()>
pub fn go_forward(&self, activity: &mut Activity) -> Result<()>
Goes forward to the next page in history