1

I have created an edittext and a go button and a webview in single layout. When a person enters a url and clicks on go button, the webview should load the website content, Instead it is going to default browser to load website how should i slove this issue ?

This is my code

xml file

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="50dip" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/etenterurl" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0" android:inputType="textUri" /> <Button android:id="@+id/buttongo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="3.0" android:text="Go" /> </LinearLayout> </FrameLayout> 

java code :

import java.util.ArrayList; import android.os.Bundle; import android.view.View; import android.webkit.WebView; import android.widget.Button; import android.widget.EditText; import android.annotation.SuppressLint; import android.app.Activity; public class MainActivity extends Activity { EditText etenter; Button bgo; WebView webview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etenter= (EditText)findViewById(R.id.etenterurl); bgo =(Button)findViewById(R.id.buttongo); webview = (WebView)findViewById(R.id.webview); bgo.setOnClickListener(new View.OnClickListener() { @SuppressLint("SetJavaScriptEnabled") @Override public void onClick(View v) { String url=etenter.getText().toString(); if(url.trim().length()>0){ webview.getSettings().setJavaScriptEnabled(true); webview.loadUrl("http://"+url); } } }); } 

}

1
  • can you post your code.. Commented Oct 23, 2012 at 6:48

2 Answers 2

5

You have to use WebViewClient

wbView.setWebViewClient(new MyWebViewClient()); private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { Log.v("uuuurl",url); view.loadUrl(url); return true; } } 

See here for example

Sign up to request clarification or add additional context in comments.

3 Comments

It worked, I'm having one more issue im giving 50dp as width for linear layout and 50dp as paddingTop for webview, but webview dosen't align accordingly.
Trail and error :). Check by Making changes in Height and width
I would say post another question for this issue, You may get some more ideas
2

you should use WebViewClient

webview.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { Log.d("TAG", " ==> "+url); view.loadUrl(url); return true; } }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.