Android Question [SOLVED] Clicking a link in a WebView and show the content in another WebView

GMan

Well-Known Member
Licensed User
Longtime User
I have 2 WebViews.
In the first one a NewsTicker Marquee is loaded wihich contains links.

Because the first one is only a small banner i want to open the clicked link in another (fullscreen) WebView.

Is this generally possible , i.e. with a kind of the Target=Blank parameter ?
 

JohnC

Expert
Licensed User
Longtime User
ChatGPT suggested this code...

B4X:
Sub Process_Globals
    ' Global Variables
End Sub

Sub Globals
    Private wvMini As WebView
    Private wvFullScreen As WebView
    Private pnlFullScreen As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main") ' Ensure you have a layout with two WebViews and a Panel
    
    ' Initialize the mini WebView
    wvMini.Initialize("wvMini")
    Activity.AddView(wvMini, 10dip, 10dip, 300dip, 50dip) ' Small banner
    
    ' Initialize the full-screen WebView inside a Panel
    pnlFullScreen.Initialize("")
    pnlFullScreen.Color = Colors.ARGB(200, 0, 0, 0) ' Transparent background
    Activity.AddView(pnlFullScreen, 0, 0, 100%x, 100%y)
    
    wvFullScreen.Initialize("wvFullScreen")
    pnlFullScreen.AddView(wvFullScreen, 0, 0, 100%x, 100%y)
    
    pnlFullScreen.Visible = False ' Hide full-screen panel initially
    
    ' Load the NewsTicker in wvMini
    Dim html As String = "<html><body><marquee behavior='scroll' direction='left'>" & _
                         "<a href='https://www.example.com/news1'>News 1</a> | " & _
                         "<a href='https://www.example.com/news2'>News 2</a> | " & _
                         "<a href='https://www.example.com/news3'>News 3</a>" & _
                         "</marquee></body></html>"
    
    wvMini.LoadHtml(html)
    
End Sub

' Handle URL clicks
Sub wvMini_OverrideUrl (Url As String) As Boolean
    Log("Intercepted URL: " & Url)
    ShowFullScreenWebView(Url)
    Return True ' Prevent default behavior
End Sub

' Function to open full-screen WebView
Sub ShowFullScreenWebView(Url As String)
    wvFullScreen.LoadUrl(Url)
    pnlFullScreen.Visible = True
End Sub

' Handle back button press
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        If pnlFullScreen.Visible Then
            pnlFullScreen.Visible = False ' Close full-screen WebView
            Return True
        End If
    End If
    Return False
End Sub

Kept the functionality simple – Directly intercepts URL clicks.
Efficient Back Handling – Closes the full-screen WebView when the back button is pressed.

This should work exactly as expected—clicking a marquee link in wvMini opens it in wvFullScreen instead of wvMini
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Thx for that...i will give it a try.
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
-I tried and give it a GO.
Works perfect
 
Upvote 0
Top