Colin747
Colin747

Reputation: 5023

Android - apply text box contents to variable for webview

I want the user to be able to enter a web address into a text box and for it to be loaded using webview, but I am getting the following error:

error: no suitable method found for loadUrl(EditText)
webview.loadUrl(edittext);
method WebView.loadUrl(String) is not applicable
(actual argument EditText cannot be converted to String by method invocation conversion)
method WebView.loadUrl(String,Map<String,String>) is not applicable
(actual and formal argument lists differ in length)

Here is the code I am using:

  Button button;
  private EditText edittext;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final WebView webview = new WebView(this);
    edittext = (EditText) findViewById(R.id.editText);
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View arg0) {
        webview.setWebChromeClient(new WebChromeClient());
        setContentView(webview);
        webview.loadUrl(edittext);
            }
        }); 

Upvotes: 1

Views: 459

Answers (2)

Ivo
Ivo

Reputation: 23267

change

webview.loadUrl(edittext);

to

webview.loadUrl(edittext.getText().toString());

Upvotes: 1

did you try this ??

edittext = (EditText) findViewById(R.id.editText);
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View arg0) {

        webview.loadUrl(edittext.getText().toString());
            }
        }); 

Upvotes: 0

Related Questions