Issue
I am writing an application to launch webView in my activity. But it was launched in web browser.
I refer the links and this tutorial too
But When i used shouldOverrideUrlLoading
, i can get only blank screen.
For that issue i refered this link
But no one help in my case..
I have given my code.
Please note what mistake I have done?
My code is:
webview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal|center_vertical" >
</WebView>
</LinearLayout>
WebViewActivity.java
public class webViewActivity extends Activity {
private WebView webView;
private String strUrl;
public final String TAG = "webViewActivity";
ProgressBar progBar;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.webview);
Bundle bundle = getIntent().getExtras();
strUrl = bundle.getString("URL");
Log.d(TAG, "link is " + strUrl);
// strUrl = "www.google.com";
webView = new WebView(this);
webView.loadUrl(strUrl);
// progBar = (ProgressBar)findViewById(R.id.progWeb);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(strUrl);
return true;
}
});
}
Please help to corect my mistake.
Thank you in Advance!!
Solution
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal|center_vertical" >
</WebView>
In your activity you need to find the id of the webview.
webView = new WebView(this);
You are creating a webview but you are not adding the same to your layout
You have already defined webview in your xml so there is no need to create the same again. Find the id of the webview and load the url accordingly.
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView);// you need to find the id of webview
Bundle bundle = getIntent().getExtras();
strUrl = bundle.getString("URL");
webView.loadUrl(strUrl);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(strUrl);
return true;
}
});
Make sure you have added internet permission in manifest file
<uses-permission android:name="android.permission.INTERNET"/>
Check the Edit 2 in the below link for the working sample
Answered By - Raghunandan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.