1

I want to remove the header and footer before loading the content to the web view. This code isn't working .

package com.wiseme; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.webkit.WebView; import android.widget.AdapterView; import android.widget.GridView; @SuppressLint("SetJavaScriptEnabled") public class MainActivity extends Activity { GridView grid; public WebView webView; public int pos; String[] desc = { "Who We Are", "What We Do", "Entrepreneur", "Scholarship", "Admission", "Internship", "Industrial Visit", "Project", "Buy or Sell Projects", "Free Training", "College Registration", "Information", "Feedback", "Contact" }; int[] imageId = { R.drawable.dummy, R.drawable.dummy, R.drawable.dummy, R.drawable.dummy, R.drawable.dummy, R.drawable.dummy, R.drawable.dummy, R.drawable.dummy, R.drawable.dummy, R.drawable.dummy, R.drawable.dummy, R.drawable.dummy, R.drawable.dummy, R.drawable.dummy }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activitymain); GridDesign adapter = new GridDesign(MainActivity.this, desc, imageId); grid = (GridView) findViewById(R.id.grid); grid.setAdapter(adapter); grid.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { pos = position; Intent intent = new Intent(MainActivity.this, webView.class); startActivity(intent); } }); } @Override public void onBackPressed() { new AlertDialog.Builder(this) .setMessage("Are you sure you want to exit?") .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { MainActivity.this.finish(); } }).setNegativeButton("No", null).show(); } } 

Web view class

package com.wiseme; import android.annotation.SuppressLint; import android.os.Bundle; import android.webkit.WebSettings; import android.webkit.WebView; public class webView extends MainActivity { @SuppressLint("SetJavaScriptEnabled") @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); WebView webView; webView = new WebView(this); WiseWeWebClient myWebClient = new WiseWeWebClient(); webView.setWebViewClient(myWebClient); WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); switch (pos) { case 0: webView.loadUrl("http://wisewe.com/who-we-are/"); break; case 1: webView.loadUrl("http://wisewe.com/what-we-do/ "); break; case 2: webView.loadUrl("http://wisewe.com/entrepreneur/ "); break; case 3: webView.loadUrl("http://wisewe.com/scholarship/"); break; case 4: webView.loadUrl("http://wisewe.com/college-admission/ "); break; case 5: webView.loadUrl("http://wisewe.com/internship/"); break; case 6: webView.loadUrl("http://wisewe.com/industrial-visit/"); break; case 7: webView.loadUrl("http://wisewe.com/project/ "); break; case 8: webView.loadUrl("http://wisewe.com/sell-projects/"); break; case 9: webView.loadUrl("http://wisewe.com/free-training/"); break; case 10: webView.loadUrl("http://wisewe.com/college-registration/"); break; case 11: webView.loadUrl("http://wisewe.com/branch/wise-we/"); break; case 12: webView.loadUrl("http://wisewe.com/branch/feedback/"); break; case 13: webView.loadUrl("http://wisewe.com/contact-us/"); break; } setContentView(webView); } @Override public void onBackPressed() { webView.this.finish(); } } 

WiseWeWebClient

package com.wiseme; import android.webkit.WebView; import android.webkit.WebViewClient; public class WiseWeWebClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } @Override public void onPageFinished(WebView view, String url) { view.loadUrl("javascript:var footer = document.getElementById('footer'); footer.parentNode.removeChild(footer); var header = document.getElementById('header-full'); header.parentNode.removeChild(header);"); } } 

Header .......

enter image description here

Footer......

enter image description here

Those are all updated to latest. Still I couldn't remove header and footer.

4
  • screenshot of the footer and header ? Commented Jul 13, 2015 at 7:14
  • I have posted in the question now. Have a look Commented Jul 13, 2015 at 7:23
  • to the header the the "wise we" with the google search bar, and the footer the social network buttons ? Commented Jul 13, 2015 at 7:25
  • Yes those are things I want removed. Commented Jul 13, 2015 at 7:27

2 Answers 2

1

According your screen-shot, you are using multiple Jsoup Library. Use only one of them. Use this link for download Jsoup Library.

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

4 Comments

I have just imported them. Even then i cant use jsoup functions.
@KiranManjunath - its working at my side. Just use jsoup-1.8.2.jar .
Okay wait let me check
@KiranManjunath - Great (y)
1

According to your screenshots and the website, it's a web/html issue : you want to remove some DOM elements of your page. As you want the header/footer on mobile device but not in your app, you will have to run some javascript when the page is loaded :

  1. Create a custom WebViewClient to listen to onPageFinished

    public class WiseWeWebClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } @Override public void onPageFinished(WebView view, String url) { view.loadUrl("javascript:var footer = document.getElementById(\"footer\"); footer.parentNode.removeChild(footer); var header = document.getElementById(\"header-full\"); header.parentNode.removeChild(header);"); } } 
  2. set the WebClient to your webview

    WiseWeWebClient myWebClient = new WiseWeWebClient(); webview.setWebViewClient(myWebClient); WebSettings webSettings = webview.getSettings(); webSettings.setJavaScriptEnabled(true); 

7 Comments

You need to escape the quotes here - getElementById("footer") and here getElementById("header-full"
Oh okay let me check.
Still getting the error (String literal is not properly closed by a double-quote)
Got it. It should be in a single line.
@KiranManjunath replace \" by ' (simple quote)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.