Remove Header and Footer in WebView Amdroid - android

I want to remove the header and footer before loading the content to the web view. This code isn't working. I have boutique website. My code remove header and footer when load url in webview. When i click any boutique,it load details page with header and footer, it does not remove from inner pages
public class WebviewFragment extends Fragment implements IOnBackPressed {
WebSettings ws;
WebView webView;
Document document;
// TODO: Rename and change types of parameters
private String url;
public WebviewFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_webview, container, false);
webView = (WebView) v.findViewById(R.id.webView);
url = getArguments().getString("url");
new MyAsynTask().execute();
return v;
}
#Override
public boolean onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
return true;
} else {
// activity will act normal
return false;
}
}
private class MyAsynTask extends AsyncTask<Void, Void, Document> {
#Override
protected Document doInBackground(Void... voids) {
Document document = null;
try {
document = Jsoup.connect(url).get();
document.getElementById("header").remove();
document.getElementById("footer").remove();
} catch (IOException e) {
e.printStackTrace();
}
return document;
}
#Override
protected void onPostExecute(Document document) {
super.onPostExecute(document);
webView.loadDataWithBaseURL(url, document.toString(), "text/html", "utf-8", "");
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
// webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String request) {
view.loadUrl(request);
return super.shouldOverrideUrlLoading(view, request);
}
#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);");
}
});
}
}
}
Please help me out, I shall be very thankful to you.

Related

Android App Linking not working inside the WebView

When I try with the phone browser it opens the app. but not inside of the app webview.
public class WebViewPayNowFragment extends Fragment implements AdvancedWebView.Listener {
private View rootView;
private AdvancedWebView mWebView;
private String link, tid;
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
System.out.println("asdajsda : "+url);
if (url != null && url.startsWith("ptcl://")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
}
public WebViewPayNowFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getArguments();
if (bundle != null) {
link = bundle.getString(Constants.LINK, null);
tid = bundle.getString(Constants.TRANSACTION_ID, null);
System.out.println("asdiahsdhakjshdkah : " + link);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_web_view, container, false);
mWebView = rootView.findViewById(R.id.webview);
mWebView.setListener(getActivity(), this);
mWebView.setWebViewClient(new MyBrowser());
if (link != null) {
String postData = null;
try {
postData = "TransactionID=" + URLEncoder.encode(tid, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
mWebView.postUrl(link, postData.getBytes());
} else {
}
return rootView;
}
Here is my code inside the fragment.
I created a inner class extending WebViewClient. and set the setWebViewClient but im still having the problem.
Solution:
I encountered this situation myself. This is what I did:
First create a inner class and extend it with WebViewClient:
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
....
}
}
then:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("ptcl://")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
Lastly,
webView.setWebViewClient(new MyBrowser());
Try it, Hope it helps.

Hide HTML tags with Jsoup in Android

I am working with an Android application. I want must only load items from site and hide some tags (header, footer, search panel ). I have used Webview and Jsoup for this. All working fine. But when I click some item, in the new page will open header/ footer again. How to I can hide header and footer using this code ?
public class MainActivity extends AppCompatActivity {
WebView webview;
String url="http://hut.az/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview= (WebView) findViewById(R.id.webview);
new MyAsynTask().execute();
}
private class MyAsynTask extends AsyncTask<Void, Void, Document> {
#Override
protected Document doInBackground(Void... voids) {
Document document = null;
try {
document= Jsoup.connect(url).get();
document.getElementsByClass("header1").remove();
document.getElementsByClass("topbar clearfix").remove();
document.getElementsByClass("mapandslider").remove();
document.getElementsByClass("footer1").remove();
document.getElementsByClass("copyright").remove();
} catch (IOException e) {
e.printStackTrace();
}
return document ;
}
#Override
protected void onPostExecute(Document document) {
super.onPostExecute(document);
webview.loadDataWithBaseURL(url,document.toString(),"text/html","utf-8","");
webview.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
webview.setWebViewClient(new WebViewClient(){
private WebView view;
private WebResourceRequest request;
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
this.view = view;
this.request = request;
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, request);
}
});
}
}
}
When opens app:
When I click any item, header and footer shows again
It is because you open a whole new page in Webview then you haven't remove tags in that new webpage, what you have to do is change your code into
public class MainActivity extends AppCompatActivity {
WebView webview;
String url="http://hut.az/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview= (WebView) findViewById(R.id.webview);
webview.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
webview.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
new MyAsynTask().execute(url);
return super.shouldOverrideUrlLoading(view, url);
}
});
new MyAsynTask().execute(this.url);
}
private class MyAsynTask extends AsyncTask<String, Void, Document> {
#Override
protected Document doInBackground(String... urls) {
Document document = null;
try {
document= Jsoup.connect(urls[0]).get();
document.getElementsByClass("header1").remove();
document.getElementsByClass("topbar clearfix").remove();
document.getElementsByClass("mapandslider").remove();
document.getElementsByClass("footer1").remove();
document.getElementsByClass("copyright").remove();
} catch (IOException e) {
e.printStackTrace();
}
return document;
}
#Override
protected void onPostExecute(Document document) {
super.onPostExecute(document);
webview.loadDataWithBaseURL(url,document.toString(),"text/html","utf-8","");
}
}
}

How to hide header , footer and other tags with Webview in Android?

I have used Jsoup for hide html tags (header, search panel, footer) . All work fine. But when i click any ithem, in new page will show footer and header again. How to i can hide header and footer for all operations ?
Thanks for before :)
public class MainActivity extends AppCompatActivity {
WebView webview;
String url="http://hut.az/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview= (WebView) findViewById(R.id.webview);
new MyAsynTask().execute();
}
private class MyAsynTask extends AsyncTask<Void, Void, Document> {
#Override
protected Document doInBackground(Void... voids) {
Document document = null;
try {
document= Jsoup.connect(url).get();
document.getElementsByClass("header1").remove();
document.getElementsByClass("topbar clearfix").remove();
document.getElementsByClass("mapandslider").remove();
document.getElementsByClass("footer1").remove();
document.getElementsByClass("copyright").remove();
} catch (IOException e) {
e.printStackTrace();
}
return document;
}
#Override
protected void onPostExecute(Document document) {
super.onPostExecute(document);
webview.loadDataWithBaseURL(url,document.toString(),"text/html","utf-8","");
webview.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
webview.setWebViewClient(new WebViewClient(){
private WebView view;
private WebResourceRequest request;
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
this.view = view;
this.request = request;
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, request);
}
});
}
}
}

How to clear Http Basic Auth username and password from cache - android webview

I am using android webview to display a web page that requires basic authentication. I have the code that takes in username and password and allows successful login to the web page. But I want to clear the username and password from cache when the user visits the webview again. Looks like once you enter username and password for basic auth, it keeps that in cache and logs you in automatically.
I have tried
WebViewDatabase webViewDB = WebViewDatabase.getInstance(getActivity());
webViewDB.clearHttpAuthUsernamePassword();
I have also tried webview.clearFormData, webview.clearCache
But it doesn't seem to work. It logged me in automatically without the prompt to enter username and password.
Here is my full code for webview fragment.
public class WebViewFragment extends Fragment {
private static final String LOG_TAG = WebViewFragment.class.getName();
private ProgressBar progressBar;
private View footerV;
private String urlToLoad;
private int footerVisibility;
private int failureCount = 0;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.web_fragment, container, false);
try {
init(rootView);
} catch (Exception e) {
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(getActivity()));
}
return rootView;
}
#Override
public void onDestroyView() {
super.onDestroyView();
if (footerV != null) footerV.setVisibility(footerVisibility);
}
private void init(View rootView) {
Bundle bundle = getArguments();
ConstraintLayout navBarCL = (ConstraintLayout) rootView.findViewById(R.id.navBar);
progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);
if (bundle.getBoolean(BundleConstants.WEB_HIDE_HEADER)) {
navBarCL.setVisibility(View.GONE);
} else {
navBarCL.setVisibility(View.VISIBLE);
ImageView backButtonIV = (ImageView) rootView.findViewById(R.id.backButtonImage);
backButtonIV.setOnClickListener(view -> navigateBack());
String title = bundle.getString(BundleConstants.WEB_HEADER_TEXT);
TextView headerTextTV = (TextView) rootView.findViewById(R.id.headerText);
headerTextTV.setText(title);
}
WebView webView = (WebView) rootView.findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.setWebViewClient(getClient());
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onCloseWindow(WebView window) {
super.onCloseWindow(window);
navigateBack();
}
});
footerV = getActivity().findViewById(R.id.footer);
footerVisibility = footerV.getVisibility();
if (bundle.getBoolean(BundleConstants.WEB_SHOW_FOOTER)) {
footerV.setVisibility(View.VISIBLE);
} else {
footerV.setVisibility(View.GONE);
}
urlToLoad = bundle.getString(BundleConstants.WEB_URL);
Log.d(LOG_TAG, "Opening url :" + urlToLoad);
webView.loadUrl(urlToLoad);
}
#NonNull
private WebViewClient getClient() {
WebViewDatabase webViewDB = WebViewDatabase.getInstance(getActivity());
webViewDB.clearHttpAuthUsernamePassword();
return new WebViewClient() {
#Override
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
++failureCount;
showProgressBar();
LoginDialog loginDialog = new LoginDialog(getContext(), getFragment(), handler, failureCount);
loginDialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
hideProgressBar();
}
};
}
public void hideProgressBar() {
if (progressBar != null) progressBar.setVisibility(View.GONE);
}
public void showProgressBar() {
if (progressBar != null) progressBar.setVisibility(View.VISIBLE);
}
private WebViewFragment getFragment() {
return this;
}
public String getUrlToLoad() {
return urlToLoad;
}
protected void navigateBack() {
footerV.setVisibility(footerVisibility);
getFragmentManager().popBackStack();
}
}
Please provide some suggestions.
Try this
WebView webView = (WebView) rootView.findViewById(R.id.webView);
webView.clearCache(true);

WebView error in fragment

This code is working fine in Activity but same code doesn't work in Fragments. It gets error of ProgressDialog which can't be applied and another one is can't resolve method onBackPressed()
public class FacebookFragment extends Fragment {
private WebView webView;
public FacebookFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView= inflater.inflate(R.layout.fragment_facebook, container, false);
webView = (WebView)rootView. findViewById(R.id.webView1);
startWebView("https://betanews.in/");
return rootView;
}
private void startWebView(String url) {
//Create new webview Client to show progress dialog
//When opening a url or click on link
webView.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog;
//If you will not use this method url links are opeen in new brower not in webview
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
//Show loader on url load
public void onLoadResource (WebView view, String url) {
if (progressDialog == null) {
// in standard case YourActivity.this
progressDialog = new ProgressDialog(FacebookFragment.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
try{
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}catch(Exception exception){
exception.printStackTrace();
}
}
});
// Javascript inabled on webview
webView.getSettings().setJavaScriptEnabled(true);
// Other webview options
/*
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setBuiltInZoomControls(true);
*/
/*
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
webview.loadData(summary, "text/html", null);
*/
//Load url in webview
webView.loadUrl(url);
}
// Open previous opened link from history on webview when back button pressed
#Override
// Detect when the back button is pressed
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
replace this line progressDialog = new ProgressDialog(FacebookFragment.this); with progressDialog = new ProgressDialog(getActivity());
we need to pass a context to the ProgressDialog constructor, FacebookFragment.this will return object of Fragment, but the expected object is of Activity, you can get the Context of holding Activity.
onBackPressed() is method from class Activity , but here you are extending Fragment
override this method where you actually inflates this fragment. and check for the instance id of the fragment you needed, then do the stuff.
hope it help you

Categories

Resources