Im trying to add adsense code in one app that I have built. The adsense code
is stored in a html file on my website. And this is how I call it:
WebView ads = (WebView) findViewById(R.id.topads);
ads.getSettings().setJavaScriptEnabled(true);
ads.loadUrl("http://www.site.com/code/adsense.html");
And on activity main xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<WebView
android:id="#+id/topads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
I tried setting LinearLayots to:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
And it does not work either.
On AndroidManifest.html I have added:
<uses-permission android:name="android.permission.INTERNET" />
All I see is a black container after webview loads. Im trying this on the
android emulator, using android 4.4.
Can someone help me?
Thanks in advance.
Use a WebViewClient. Now it should open the url in webview itself. I am not sure of your own url that you say is working. But you try the below and it works. You can try opening try different url's just for testing
Tested on emulator api 19
public class MainActivity extends Activity {
/** Called when the activity is first created. */
WebView web;
ProgressBar progressBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = (WebView) findViewById(R.id.wv);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://slashdot.org/");
}
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
//super.onReceivedError(view, errorCode, description, failingUrl);
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}
// To handle "Back" key press event for WebView to go back to previous screen.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
else
{
finish();
return true;
}
}
}
Snap
Related
Android webview display white screen instead of loading the website on Android 4.1.2 .The website perfectly opens when I open it from web browser, I am using following code
public class MainActivity extends AppCompatActivity {
WebView web;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = (WebView) findViewById(R.id.webview);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("www.Example.com");
}
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
// To handle "Back" key press event for WebView to go back to previous screen.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Remember to set the protocol "http:"
web.loadUrl("http://www.Example.com");
and very important, be sure that url has a value at this point:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
This is my code which displays current webpage url. The code works fine but i want to get only specific value from url.
I follow this post but is not helping me. Code is blast when run. I want only Id when browsing playstore
public class WebViewClientDemoActivity extends Activity {
/** Called when the activity is first created. */
WebView web;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
web = (WebView) findViewById(R.id.webview01);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("https://play.google.com/store/apps");
}
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
String[] splited = url.split("&");
String[] id = splited[0].split("?id=");
Toast.makeText (getApplicationContext(), id.toString(),
Toast.LENGTH_SHORT).show ();
view.loadUrl(url);
return true;
// this is example url i just want to get only id from which url
//https://play.google.com/store/apps/details?id=com.eamobile.monopoly_row_wf&feature
//=more_from_developer#?t=W251bGwsMSwyLDEwMiwiY29tLmVhbW9iaWxlLm1vbm9wb2x5X3Jvd193ZiJd
}
}
// To handle "Back" key press event for WebView to go back to previous screen.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
As you mentioned in your comment you would like to see com.eamobile.monopoly_row_wf
Hence the code for that is
String standard = "https://play.google.com/store/apps/details?id=";
String id= url.substring(standard.length(), url.indexOf("&"));
Toast.makeText (getApplicationContext(), id.toString(), Toast.LENGTH_SHORT).show ();
i want to disable phone browser and i want to use a webview .
but this code didn't work for me . please guide me :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Wop=(WebView)findViewById(R.id.webi);
String mturl="http://www.google.com";
Wop.getSettings().setJavaScriptEnabled(true);
Wop.loadUrl(mturl);
}
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.loadUrl("http://www.stackoverflow.com");
To enable the previous web-page to be loaded,
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
} }
Definiton of the HelloWebViewClient class :
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
//what you want to do when the page finished loading, eg. give some message, show progress bar, etc
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
//what you want to do when the page starts loading, eg. give some message
}
}
WebView w = new WebView(this);
//add this to your code
w.setWebViewClient(new Callback());
w.loadUrl("http://www.facebook.com");
//and this class also
private class Callback extends WebViewClient{ //HERE IS THE MAIN CHANGE.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
}
}
I need to display a Webview in my android app. The problem is that the webpage I am trying to show gives an error:
"This page contains the following errors:
error on line xxx at column xxx: Entity 'abc' is not defined
Below is the rendering of the page upto the first error"
The site works fine on my mobile browser and also on my desktop. Also, I do not get this error with other websites
Is there anyway I can avoid this error. Kindly help.
This is the java code:
public class Zero extends Activity {
WebView wv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.zero);
wv=(WebView) findViewById(R.id.web);
wv.getSettings().setJavaScriptEnabled(true);
//wv.loadUrl("http://customercare.indianrailways.gov.in/criscm/common/complaint_registration.seam");
wv.loadUrl("http://ndtv.com");
wv.setWebViewClient(new HelloWebViewClient());
final Activity activity = this;
wv.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged(WebView view, int progress) {
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle("Easy Complaint Indian Railways");
}
});
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {
wv.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
The layout xml file:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/web">
</WebView>
Use the below activity to load your url :::
public class WebViewsScreenActivity extends Activity {
private WebView mwebview;
private WebViewsScreenActivity _activity;
ProgressDialog _dilog;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.webview);
_activity = this;
mwebview=(WebView)findViewById(R.id.webview);
mwebview.getSettings().setJavaScriptEnabled(true);
mwebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
if(checkInternetConnection(_activity)){
if(savedInstanceState==null)
mwebview.loadUrl(webUrl);
else
mwebview.restoreState(savedInstanceState);
}
else{
//showAlert "Unable to Connect Server"
}
mwebview.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int progress) {
if(mwebview.getVisibility()==View.VISIBLE)
{
WebViewsScreenActivity .this.setProgress(progress * 100);
}
}
});
mwebview.setWebViewClient(new HelloWebViewClient());
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK)
{
mwebview.goBack();
return true;
}
else
return super.onKeyUp(keyCode, event);
}
//To check whether network connection is available on device or not
private boolean checkInternetConnection(Activity _activity) {
ConnectivityManager conMgr = (ConnectivityManager) _activity.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected())
return true;
else
return false;
}//checkInternetConnection()
//HelloWebViewClient class for webview
private class HelloWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
} //HelloWebViewClient-class
}//AccountsScreenActivity-class
Note:: Dont forget to add the required permission in your manifeast file.
I'm trying to load the url for a google calendar public calendar in a webview. Initially it worked without issue. At somepoint all I get is the text "null" in the webview. I've tried changing the url as well to just a basic webpage with the same result. I've also tried commenting out the wbv.loadurl line and get the same result. I've tried stepping through the code and the line is called when not commented out. Any help would be appreciated. Below is the code to populate the webview:
wbv = (WebView) findViewById(R.id.audioPlayer_showSchedule);
WebSettings settings = wbv.getSettings();
wbv.setWebViewClient(new TwitWebViewClient());
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
settings.setJavaScriptEnabled(true);
settings.setDefaultTextEncodingName("utf-8");
wbv.loadUrl("http://www.google.com/calendar/embed?src=r8psn8mpajnfa2703k43l6o014#group.calendar.google.com&ctz=America/Los_Angeles&program&mode=Week&gsessionid=vzeVWSq2Wdk3eVmGOUp1bQ");
Use below working code ::
public class Android_Activity extends Activity {
private Android_Activity _activity;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
_activity = this;
setContentView(R.layout.main);
mwebview=(WebView)view.findViewById(R.id.webview);
mwebview.getSettings().setJavaScriptEnabled(true);
mwebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
if(checkInternetConnection(_activity)==true){
if(savedInstanceState==null)
mwebview.loadUrl("http://abc.com");
else
mwebview.restoreState(savedInstanceState);
}
else{
AlertDialog.Builder builder = new AlertDialog.Builder(_activity);
builder.setMessage("Please check your network connection.")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
mwebview.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int progress) {
if(mwebview.getVisibility()==View.VISIBLE)
{
_activity.setProgress(progress * 100);
}
}
});
mwebview.setWebViewClient(new HelloWebViewClient());
}
//HelloWebViewClient class for webview
private class HelloWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
view.loadUrl(url);
return true;
}
} //HelloWebViewClient-class
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && mwebview.canGoBack() ){
mwebview.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
//To check whether network connection is available on device or not
public static boolean checkInternetConnection(Activity _activity) {
ConnectivityManager conMgr = (ConnectivityManager) _activity.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected())
return true;
else
return false;
}//checkInternetConnection()
}
And also in your main.xml layout you must have a webview with id webview
It all looks good to me. Are you sure your TwitWebViewClient is doing what you expect it to?
Also, are you sure that's the URL you want to be loading? I can't help but notice the "gsessionid=vzeVWSq2Wdk3eVmGOUp1bQ" at the end of it. Surely that won't work as soon as your session expired?