0

exi want to load an url in mywebview when the end-user click on 'item1' in the option menu

Here is my code :

package com.exemple.test;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings;
import android.app.AlertDialog;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.loadUrl("http://m.google.com");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.item1:
                //AlertDialog alertDialog = new AlertDialog.Builder(this).create();
                //alertDialog.setTitle("Reset...");
                //alertDialog.setMessage("Are you sure?");
                //alertDialog.show();
                myWebView.loadUrl("http://m.google.com");
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

Thanks !

1
  • The question title and content don't match up. Can you clarify? Commented Sep 23, 2012 at 19:07

1 Answer 1

2

Just change

WebView myWebView = (WebView) findViewById(R.id.webview);

to

myWebView = (WebView) findViewById(R.id.webview);

And add the following as a class variable:

WebView myWebView;

So your final code becomes:

public class MainActivity extends Activity {

    WebView myWebView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.loadUrl("http://m.google.info"); //You may want to comment this out, as you said you wanted to load it only when the user clicked
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.item1:
                //AlertDialog alertDialog = new AlertDialog.Builder(this).create();
                //alertDialog.setTitle("Reset...");
                //alertDialog.setMessage("Are you sure?");
                //alertDialog.show();
                myWebView.loadUrl("http://m.google.com");
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you very much for your answer but can you be more precise, can you modify my code :) im a complety beginnier !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.