WebView

Struct WebView 

Source
pub struct WebView { /* private fields */ }
Expand description

WebView component for displaying web content

§Important Usage Notes

  1. 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.

  2. 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>")?;
  3. 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

Source

pub fn new(activity: &mut Activity, parent: Option<i64>) -> Result<Self>

Creates a new WebView

§Arguments
  • activity: Reference to the Activity
  • parent: Optional parent view ID
§Examples
let webview = WebView::new(&mut activity, Some(layout_id))?;
Source

pub fn id(&self) -> i64

Gets the view ID

Source

pub fn view(&self) -> &View

Gets a reference to the underlying View

Source

pub fn load_uri(&self, activity: &mut Activity, uri: &str) -> Result<()>

Loads a URI/URL

§Arguments
  • activity: Reference to the Activity
  • uri: The URL to load, e.g., “https://www.google.com”
§Examples
webview.load_uri(&mut activity, "https://www.google.com")?;
Source

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 Activity
  • data: 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>")?;
Source

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 Activity
  • allow: 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>")?;
}
Source

pub fn allow_content_uri( &self, activity: &mut Activity, allow: bool, ) -> Result<()>

Allows loading content from content:// URIs

§Arguments
  • activity: Reference to the Activity
  • allow: Whether to allow loading from content URIs
Source

pub fn allow_navigation( &self, activity: &mut Activity, allow: bool, ) -> Result<()>

Allows navigation to different sites

§Arguments
  • activity: Reference to the Activity
  • allow: Whether to allow users and JavaScript to navigate to different sites
Source

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 Activity
  • code: 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';")?;
Source

pub fn go_back(&self, activity: &mut Activity) -> Result<()>

Goes back to the previous page in history

Source

pub fn go_forward(&self, activity: &mut Activity) -> Result<()>

Goes forward to the next page in history

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V