Xamarin/Android webview hide website element/class
I'm trying to make a little android app showing a webview loading a website.
I got it to show it with the following code snippets. But what I need now is to hide some elements on the header and the footer(a menu for example). I thought I could do it not loading some classes from the webpage, but I'm not sure how to properly do it. Do anyone have some experience on this to share some light? :)
Thanks in advance.
protected override void OnCreate(Bundle savedInstanceState)
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
FloatingActionButton fab = FindViewById<FloatingActionButton>(Resource.Id.fab);
fab.Click += FabOnClick;
DrawerLayout drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, Resource.String.navigation_drawer_open, Resource.String.navigation_drawer_close);
drawer.AddDrawerListener(toggle);
toggle.SyncState();
NavigationView navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
navigationView.SetNavigationItemSelectedListener(this);
wbv = FindViewById<WebView>(Resource.Id.webView1);
wbv.SetWebViewClient(new ExtendWebViewClient());
WebSettings webSettings = wbv.Settings;
webSettings.JavaScriptEnabled = true;
wbv.LoadUrl(txtUrl);
internal class ExtendWebViewClient : WebViewClient
public override bool ShouldOverrideUrlLoading(WebView view, string url)
view.LoadUrl(url);
return true;
edit: Just to reflect the changes to the second class
public class ExtendWebViewClient : WebViewClient
public override void OnPageFinished(WebView view, string url)
base.OnPageFinished(view, url);
string js = "var myElements = document.getElementsByClassName('fusion-main-menu');" +
" myElements[0].style.display = 'none'; ";
if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
view.EvaluateJavascript(js, null);
else
view.LoadUrl(js);
webview xamarin.android
add a comment |
I'm trying to make a little android app showing a webview loading a website.
I got it to show it with the following code snippets. But what I need now is to hide some elements on the header and the footer(a menu for example). I thought I could do it not loading some classes from the webpage, but I'm not sure how to properly do it. Do anyone have some experience on this to share some light? :)
Thanks in advance.
protected override void OnCreate(Bundle savedInstanceState)
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
FloatingActionButton fab = FindViewById<FloatingActionButton>(Resource.Id.fab);
fab.Click += FabOnClick;
DrawerLayout drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, Resource.String.navigation_drawer_open, Resource.String.navigation_drawer_close);
drawer.AddDrawerListener(toggle);
toggle.SyncState();
NavigationView navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
navigationView.SetNavigationItemSelectedListener(this);
wbv = FindViewById<WebView>(Resource.Id.webView1);
wbv.SetWebViewClient(new ExtendWebViewClient());
WebSettings webSettings = wbv.Settings;
webSettings.JavaScriptEnabled = true;
wbv.LoadUrl(txtUrl);
internal class ExtendWebViewClient : WebViewClient
public override bool ShouldOverrideUrlLoading(WebView view, string url)
view.LoadUrl(url);
return true;
edit: Just to reflect the changes to the second class
public class ExtendWebViewClient : WebViewClient
public override void OnPageFinished(WebView view, string url)
base.OnPageFinished(view, url);
string js = "var myElements = document.getElementsByClassName('fusion-main-menu');" +
" myElements[0].style.display = 'none'; ";
if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
view.EvaluateJavascript(js, null);
else
view.LoadUrl(js);
webview xamarin.android
add a comment |
I'm trying to make a little android app showing a webview loading a website.
I got it to show it with the following code snippets. But what I need now is to hide some elements on the header and the footer(a menu for example). I thought I could do it not loading some classes from the webpage, but I'm not sure how to properly do it. Do anyone have some experience on this to share some light? :)
Thanks in advance.
protected override void OnCreate(Bundle savedInstanceState)
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
FloatingActionButton fab = FindViewById<FloatingActionButton>(Resource.Id.fab);
fab.Click += FabOnClick;
DrawerLayout drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, Resource.String.navigation_drawer_open, Resource.String.navigation_drawer_close);
drawer.AddDrawerListener(toggle);
toggle.SyncState();
NavigationView navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
navigationView.SetNavigationItemSelectedListener(this);
wbv = FindViewById<WebView>(Resource.Id.webView1);
wbv.SetWebViewClient(new ExtendWebViewClient());
WebSettings webSettings = wbv.Settings;
webSettings.JavaScriptEnabled = true;
wbv.LoadUrl(txtUrl);
internal class ExtendWebViewClient : WebViewClient
public override bool ShouldOverrideUrlLoading(WebView view, string url)
view.LoadUrl(url);
return true;
edit: Just to reflect the changes to the second class
public class ExtendWebViewClient : WebViewClient
public override void OnPageFinished(WebView view, string url)
base.OnPageFinished(view, url);
string js = "var myElements = document.getElementsByClassName('fusion-main-menu');" +
" myElements[0].style.display = 'none'; ";
if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
view.EvaluateJavascript(js, null);
else
view.LoadUrl(js);
webview xamarin.android
I'm trying to make a little android app showing a webview loading a website.
I got it to show it with the following code snippets. But what I need now is to hide some elements on the header and the footer(a menu for example). I thought I could do it not loading some classes from the webpage, but I'm not sure how to properly do it. Do anyone have some experience on this to share some light? :)
Thanks in advance.
protected override void OnCreate(Bundle savedInstanceState)
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
FloatingActionButton fab = FindViewById<FloatingActionButton>(Resource.Id.fab);
fab.Click += FabOnClick;
DrawerLayout drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, Resource.String.navigation_drawer_open, Resource.String.navigation_drawer_close);
drawer.AddDrawerListener(toggle);
toggle.SyncState();
NavigationView navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
navigationView.SetNavigationItemSelectedListener(this);
wbv = FindViewById<WebView>(Resource.Id.webView1);
wbv.SetWebViewClient(new ExtendWebViewClient());
WebSettings webSettings = wbv.Settings;
webSettings.JavaScriptEnabled = true;
wbv.LoadUrl(txtUrl);
internal class ExtendWebViewClient : WebViewClient
public override bool ShouldOverrideUrlLoading(WebView view, string url)
view.LoadUrl(url);
return true;
edit: Just to reflect the changes to the second class
public class ExtendWebViewClient : WebViewClient
public override void OnPageFinished(WebView view, string url)
base.OnPageFinished(view, url);
string js = "var myElements = document.getElementsByClassName('fusion-main-menu');" +
" myElements[0].style.display = 'none'; ";
if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
view.EvaluateJavascript(js, null);
else
view.LoadUrl(js);
webview xamarin.android
webview xamarin.android
edited Nov 14 '18 at 17:15
vvic
asked Nov 14 '18 at 12:02
vvicvvic
666
666
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Since you have enabled javascript, there will be a function to run the script in the webview.
This is the code of WebView using Xamarin.Forms.WebView and this is how you can hide any element of webview using it's id.
var hide = await webview.EvaluateJavaScriptAsync("document.getElementById('UserName').style.display = 'none';");
Edited the original question to reflect the changes on the second class based on your suggestion. It's loading the webview just fine but it's somehow ignoring the js command. If I execute it on the webpage console it hides the element. It's xamarin.android by the way, if it changes anything. Do you know what can I be doing wrong? Had to change your suggested code sightly to adequate to my code. Thanks for your time.
– vvic
Nov 14 '18 at 17:19
Nevermind, it was my bad. I was trying to find the wrong class. Thanks.
– vvic
Nov 14 '18 at 17:30
That is why I mentioned that the webview is of Xamarin Forms to give you the idea what you really need. You're welcome :)
– Wasif Mahmood Mustafa
Nov 14 '18 at 20:30
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299801%2fxamarin-android-webview-hide-website-element-class%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since you have enabled javascript, there will be a function to run the script in the webview.
This is the code of WebView using Xamarin.Forms.WebView and this is how you can hide any element of webview using it's id.
var hide = await webview.EvaluateJavaScriptAsync("document.getElementById('UserName').style.display = 'none';");
Edited the original question to reflect the changes on the second class based on your suggestion. It's loading the webview just fine but it's somehow ignoring the js command. If I execute it on the webpage console it hides the element. It's xamarin.android by the way, if it changes anything. Do you know what can I be doing wrong? Had to change your suggested code sightly to adequate to my code. Thanks for your time.
– vvic
Nov 14 '18 at 17:19
Nevermind, it was my bad. I was trying to find the wrong class. Thanks.
– vvic
Nov 14 '18 at 17:30
That is why I mentioned that the webview is of Xamarin Forms to give you the idea what you really need. You're welcome :)
– Wasif Mahmood Mustafa
Nov 14 '18 at 20:30
add a comment |
Since you have enabled javascript, there will be a function to run the script in the webview.
This is the code of WebView using Xamarin.Forms.WebView and this is how you can hide any element of webview using it's id.
var hide = await webview.EvaluateJavaScriptAsync("document.getElementById('UserName').style.display = 'none';");
Edited the original question to reflect the changes on the second class based on your suggestion. It's loading the webview just fine but it's somehow ignoring the js command. If I execute it on the webpage console it hides the element. It's xamarin.android by the way, if it changes anything. Do you know what can I be doing wrong? Had to change your suggested code sightly to adequate to my code. Thanks for your time.
– vvic
Nov 14 '18 at 17:19
Nevermind, it was my bad. I was trying to find the wrong class. Thanks.
– vvic
Nov 14 '18 at 17:30
That is why I mentioned that the webview is of Xamarin Forms to give you the idea what you really need. You're welcome :)
– Wasif Mahmood Mustafa
Nov 14 '18 at 20:30
add a comment |
Since you have enabled javascript, there will be a function to run the script in the webview.
This is the code of WebView using Xamarin.Forms.WebView and this is how you can hide any element of webview using it's id.
var hide = await webview.EvaluateJavaScriptAsync("document.getElementById('UserName').style.display = 'none';");
Since you have enabled javascript, there will be a function to run the script in the webview.
This is the code of WebView using Xamarin.Forms.WebView and this is how you can hide any element of webview using it's id.
var hide = await webview.EvaluateJavaScriptAsync("document.getElementById('UserName').style.display = 'none';");
answered Nov 14 '18 at 13:35
Wasif Mahmood MustafaWasif Mahmood Mustafa
33415
33415
Edited the original question to reflect the changes on the second class based on your suggestion. It's loading the webview just fine but it's somehow ignoring the js command. If I execute it on the webpage console it hides the element. It's xamarin.android by the way, if it changes anything. Do you know what can I be doing wrong? Had to change your suggested code sightly to adequate to my code. Thanks for your time.
– vvic
Nov 14 '18 at 17:19
Nevermind, it was my bad. I was trying to find the wrong class. Thanks.
– vvic
Nov 14 '18 at 17:30
That is why I mentioned that the webview is of Xamarin Forms to give you the idea what you really need. You're welcome :)
– Wasif Mahmood Mustafa
Nov 14 '18 at 20:30
add a comment |
Edited the original question to reflect the changes on the second class based on your suggestion. It's loading the webview just fine but it's somehow ignoring the js command. If I execute it on the webpage console it hides the element. It's xamarin.android by the way, if it changes anything. Do you know what can I be doing wrong? Had to change your suggested code sightly to adequate to my code. Thanks for your time.
– vvic
Nov 14 '18 at 17:19
Nevermind, it was my bad. I was trying to find the wrong class. Thanks.
– vvic
Nov 14 '18 at 17:30
That is why I mentioned that the webview is of Xamarin Forms to give you the idea what you really need. You're welcome :)
– Wasif Mahmood Mustafa
Nov 14 '18 at 20:30
Edited the original question to reflect the changes on the second class based on your suggestion. It's loading the webview just fine but it's somehow ignoring the js command. If I execute it on the webpage console it hides the element. It's xamarin.android by the way, if it changes anything. Do you know what can I be doing wrong? Had to change your suggested code sightly to adequate to my code. Thanks for your time.
– vvic
Nov 14 '18 at 17:19
Edited the original question to reflect the changes on the second class based on your suggestion. It's loading the webview just fine but it's somehow ignoring the js command. If I execute it on the webpage console it hides the element. It's xamarin.android by the way, if it changes anything. Do you know what can I be doing wrong? Had to change your suggested code sightly to adequate to my code. Thanks for your time.
– vvic
Nov 14 '18 at 17:19
Nevermind, it was my bad. I was trying to find the wrong class. Thanks.
– vvic
Nov 14 '18 at 17:30
Nevermind, it was my bad. I was trying to find the wrong class. Thanks.
– vvic
Nov 14 '18 at 17:30
That is why I mentioned that the webview is of Xamarin Forms to give you the idea what you really need. You're welcome :)
– Wasif Mahmood Mustafa
Nov 14 '18 at 20:30
That is why I mentioned that the webview is of Xamarin Forms to give you the idea what you really need. You're welcome :)
– Wasif Mahmood Mustafa
Nov 14 '18 at 20:30
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299801%2fxamarin-android-webview-hide-website-element-class%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown