Nabdreas
Nabdreas

Reputation: 3467

Start activity from WebView

I have a WebView that is populated from the server in my app. HTML,that is being fed into it, has a button. Once that button is clicked it should open activity within the app. is this possible?

I could just add a Button widget but was wondering if its possible to have button in WebView

So its something like this.

WebView with HTML containing button -> click it -> open TestActivity

What would i put into

<a href="TestActivity">Test Activity</a>

Hope it makes sense.

Upvotes: 0

Views: 3949

Answers (1)

cliffroot
cliffroot

Reputation: 1691

Yes, you can do this. Try something like this.

public class JavaBridge {
    Activity parentActivity;
    public JavaBridge(Activity activity) {
        parentActivity = activity;
    }

    public void launchNewActvity(){    
        Intent intent = new Intent(parentActivity, NewActivity.class);
        parentActivity.startActivity(intent);
    }
}

For your WebView - add this mWebView.addJavascriptInterface(new JavaBridge(this), "JavaBridge");

In your html then you can write something like - <a href = "javascript:window.JavaBridge.launchNewActivity()" />

You can read more about it in here

Upvotes: 5

Related Questions