How to perform url change for android webview?


In my app I have a navigation drawer with different options for different websites. These are coded in strings:

<string name="title_section1">Google</string>
<string name="title_section2">Stackoverflow</string>
<string name="title_section3">Twitter</string>
<string name="title_section4">Facebook</string>

I also - obviously - have a webview in place in a file called activity_app.xml:

<WebView  xmlns:android=""

When the app opens this webview defaults to a dutch news site called

protected void onCreate(Bundle savedInstanceState) {

    mNavigationDrawerFragment = (NavigationDrawerFragment)
    mTitle = getTitle();

    WebView myWebView = (WebView) findViewById(;
    String pageUrl = "";
    myWebView.setWebViewClient(new WebViewClient());

    // Set up the drawer.
        (DrawerLayout) findViewById(;

Now, ideally if I click on 'Google' for example, I want the webview to change to I started building the code for this but got stuck. My app keeps on crashing and I think it has something to do with findViewById.

public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    //FragmentManager fragmentManager = getSupportFragmentManager();
            //.replace(, PlaceholderFragment.newInstance(position + 1))
    int id = position.getId ();
    if (id == 'title_section1') {
        String pageUrl = "";
    } else if (id == 'title_section2') {
        String pageUrl = "";
    } else if (id == 'title_section3') {
        String pageUrl = "";
    } else if (id == 'title_section4') {
        String pageUrl = "";

What do I need to add or change to my code to make it redirect to the actual URL? I should note that this is my first android app after 5 years PHP experience.

EDIT: I added the log of the crash. It states that the mistake is in the xml/layout files, but the error appears when I add findViewById to the file, so I think that is what must be causing it.

06-21 23:13:20.580  19172-19172/test.webviewdrawer E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: test.webviewdrawer, PID: 19172
java.lang.RuntimeException: Unable to start activity ComponentInfo{test.webviewdrawer/test.webviewdrawer.login}: android.view.InflateException: Binary XML file line #31: Error inflating class fragment
        at android.os.Handler.dispatchMessage(
        at android.os.Looper.loop(
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #31: Error inflating class fragment
        at android.view.LayoutInflater.createViewFromTag(
        at android.view.LayoutInflater.rInflate(
        at android.view.LayoutInflater.inflate(
        at android.view.LayoutInflater.inflate(
        at android.view.LayoutInflater.inflate(
        at test.webviewdrawer.login.onCreate(

UPDATE 2: Might be of importance that the method onNavigationDrawerItemSelected() in the file gets called from a file...

private void selectItem(int position) {
    mCurrentSelectedPosition = position;
    if (mDrawerListView != null) {
        mDrawerListView.setItemChecked(position, true);
    if (mDrawerLayout != null) {
    if (mCallbacks != null) {

Myself and the OP debugged this in chat and through Github and I really should have come back before now to put the solution here as an answer and so here it is...

The issue you were having is one of variable scope. You defined your WebView and your pageUrls with method and block scope respectively. This meant that when you went to make use of them later they didn't exist any more.

The fix was to move the declarations to the class level and keep the assignments where they were previously declared.

public class MyActivity extends Activity
    private WebView myWebView;
    private String pageUrl;

    public void onCreate(...)
        myWebView = (WebView) findViewById(;
        pageUrl = "";  // default page

    public void onNavigationDrawerItemSelected(int position) {
        switch (position)
            case 0:
                pageUrl = "";
            case 1:
                pageUrl = "";
            case 2:
                pageUrl = "";
            case 3:
                pageUrl = "";
                Log.e(TAG, "Unhandled navigation selection: " + position);
        // preformed asynchronously but this here just for demonstrative purposes

The way we ended up solving it actually differs from what's above as there were better design decisions to be made but this solution fixes the stated Question. If anyone's interested it's on this Github repo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at


Login to comment


Change URL at runtime in webview in android

How to change activity when an url is detect in webview Android

How to perform a button click inside a Webview Android

check url change in webview android not work

android webview :How to change javaScript alert title text in android webview?

Android how to pass url in android iframe webview?

How to load the URL of youtube in webview android?

how to redirect a url after a login in webview android

Change Android webview content (hash) without reloading the url

Android - after loading URL with webview can i change background color

How to change a webview's url without using multiple webviews

How can I get my app to get the URL of a link clicked in WebView, perform operations on it, and then load it?

change textsize in android webview

How to change background in webview

How can I save a URL and load this URL in webview (android studio)

android: how to let js code to change the webview's size

How to clear Webview content to change the video in Android Oreo

How to change android activity by clicking a button in (ionic) webview?

Does the new Android WebView perform the same as CrossWalk WebView

How to perform synchronization in android?

Can't Change URL in WebView

Detect URL change in JavaFX WebView

Change webview URL in React native

Change url of webview based on click

Android open URL in WebView

Android Webview URL Authentication

How to load this URL into WebView?

Android How To Get Playing Video Url From WebView

How to get android webview url to auto detect https http and www?