Reputation: 289
I am a beginner in making android applications. I followed THIS tutorial on how to make a webview and I ran the app on my Galaxy Note 2 without any errors, but the link written in the code wants to open in a external browser when the app is opened. I want the page to be visible inside the web view in the application. How do i proceed to do this?
MainActivity.java
WebView browser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// find the WebView by name in the main.xml of step 2
browser=(WebView)findViewById(R.id.wvwMain);
// Enable javascript
browser.getSettings().setJavaScriptEnabled(true);
// load a webpage
browser.loadUrl("http://www.google.com/");
}
Upvotes: 5
Views: 12085
Reputation: 6153
Add this inside your oncreate():
browser.setWebViewClient(new WebViewClient()); //open urls inside browser
Upvotes: 6
Reputation: 1020
To help you better understand, I'm leaving my simple browser app code here for you.
Here SimpleBrowser is the MainActivity.
public class SimpleBrowser extends Activity implements OnClickListener {
WebView ourBrowser;
EditText url;
Button go, go_Back, go_Forward, refresh, clr_History;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browser_webview);
ourBrowser = (WebView) findViewById(R.id.WVBrowser);
/* WebView Settings pretty important */
ourBrowser.getSettings().setJavaScriptEnabled(true);
ourBrowser.getSettings().setLoadWithOverviewMode(true);
ourBrowser.getSettings().setUseWideViewPort(true);
ourBrowser.setWebViewClient(new ourViewClient());
try {
ourBrowser.loadUrl("http://www.mybringback.com");
} catch (Exception e) {
e.printStackTrace();
}
go = (Button) findViewById(R.id.btn_Go);
go_Back = (Button) findViewById(R.id.btn_GoBack);
go_Forward = (Button) findViewById(R.id.btn_GoForward);
refresh = (Button) findViewById(R.id.btn_RefreshPage);
clr_History = (Button) findViewById(R.id.btn_ClrHistory);
url = (EditText) findViewById(R.id.eT_webbrowser);
go.setOnClickListener(this);
go_Back.setOnClickListener(this);
go_Forward.setOnClickListener(this);
refresh.setOnClickListener(this);
clr_History.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_Go:
String newWebaddress = url.getText().toString();
ourBrowser.loadUrl(newWebaddress);
/* Hiding the keyboard after the EditText data */
InputMethodManager ipmm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
ipmm.hideSoftInputFromWindow(url.getWindowToken(), 0);
break;
case R.id.btn_GoBack:
if (ourBrowser.canGoBack()) {
ourBrowser.goBack();
}
break;
case R.id.btn_GoForward:
if (ourBrowser.canGoForward()) {
ourBrowser.goForward();
}
break;
case R.id.btn_RefreshPage:
ourBrowser.reload();
break;
case R.id.btn_ClrHistory:
ourBrowser.clearHistory();
break;
}
}
}
Then have another Java file with name : ourViewClient.java and it should contain the below code:
public class ourViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true; // as mentioned in below notes, for your case., you do 'return false'
}
}
Android Manifest file:
Ensure to have <uses-permission android:name="android.permission.INTERNET" />
declared.
browser_webview.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/eT_webbrowser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btn_Go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Go" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_GoBack"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Go back a Page"
android:textSize="@dimen/mediumsize" />
<Button
android:id="@+id/btn_GoForward"
android:layout_width="78dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Go Forward"
android:textSize="@dimen/mediumsize" />
<Button
android:id="@+id/btn_RefreshPage"
android:layout_width="66dp"
android:layout_height="wrap_content"
android:layout_weight="0.90"
android:text="Refresh Page"
android:textSize="@dimen/mediumsize" />
<Button
android:id="@+id/btn_ClrHistory"
android:layout_width="82dp"
android:layout_height="wrap_content"
android:text="Clear History"
android:textSize="@dimen/mediumsize" />
</LinearLayout>
<WebView
android:id="@+id/WVBrowser"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</WebView>
</LinearLayout>
Upvotes: 7
Reputation: 80
You simply solve this problem with this:
myWebView.setWebViewClient(new HelloWebViewClient());
...
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("yoursite.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
Upvotes: 4