0
votes

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.

1
sure the url is the right one. try opening the same in your mobile browser - Raghunandan
Sure it is. I have tried it. - inrob
you don't see anything in webview? try opening a different url like google.com - Raghunandan
Just a black container. - inrob
Try android:layout_height="match_parent" for LinearLayout. And yes, that url doesn't open in my browser. Getting message: "You found a page we don't have anymore or never had" - MysticMagicϡ

1 Answers

0
votes

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

enter image description here