Issue
I have a WebView element into which I am trying to load simple HTML data:
var data = @"
<html>
<head>
</head>
<body>
<div id='graph' class='panel'>test</div>
</body>
</html>";
var encodedData = Base64.EncodeToString(Encoding.UTF8.GetBytes(data), Base64Flags.Default);
webView.LoadData(encodedData, "text/html", "base64");
When the app reaches this part, however, the WebView disappears entirely without any trace in logcat. I am using Xamarin C# with Android 8 (Oreo). Is my setup correct, e.g. encoding? Why might this be happening?
Edit
Due to a misunderstanding I had with my previous self, I was of the mistaken opinion that the WebView disappeared when it should have been loading data. I have recently determined this is not in fact the case, and have updated the question title accordingly.
The WebView is, however, still at least appearing to fail in loading data as above. Per requests in the comment I am including a pared down MVP of the layout and activity code.
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="@string/submit"
android:id="@+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<WebView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/grpWebView"
android:foreground="#7D756B"
android:layout_weight="1" />
</LinearLayout>
Activity:
namespace AndroidCrmApp
{
using System;
using System.Text;
using Android.App;
using Android.OS;
using Android.Util;
using Android.Webkit;
using Android.Widget;
/// <summary>
/// Application launcher
/// </summary>
[Activity(Label = "Cross Reference Mapper", MainLauncher = true, Icon = "@drawable/Icon", ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait)]
public class MainActivity : Activity
{
private Button btnSubmit;
private WebView grpWebView;
/// <inheritdoc/>
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
this.SetContentView(Resource.Layout.Main);
this.btnSubmit = this.FindViewById<Button>(Resource.Id.btnSubmit);
this.btnSubmit.Click += this.BtnSubmit_Click;
this.grpWebView = this.FindViewById<WebView>(Resource.Id.grpWebView);
}
private void BtnSubmit_Click(object sender, EventArgs e)
{
var data = @"
<html>
<head>
</head>
<body>
<div id='graph' class='panel'>test</div>
</body>
</html>";
var encodedData = Base64.EncodeToString(Encoding.UTF8.GetBytes(data), Base64Flags.Default);
this.grpWebView.LoadData(encodedData, "text/html", "UTF-8");
}
}
}
Again, the main concern is it seems the WebView functionality has changed in a way that I am not aware of, despite looking into the Android documentation. I will check other aspects of my build environment, but would be interested to see if anything is suspect with the provided code.
Edit 2
Upon further investigation I have confirmed the only change that is necessary to bring about this unexpected behavior is targeting Android 8. (Previously I had been targeting 7.1.)
Before I press the button I clear the console; after pressing the button the following warnings are logged:
07-26 12:56:41.737 W/VideoCapabilities(26287): Unrecognized profile 2130706433 for video/avc
07-26 12:56:41.737 W/VideoCapabilities(26287): Unrecognized profile 2130706434 for video/avc
07-26 12:56:41.743 W/VideoCapabilities(26287): Unrecognized profile 2130706433 for video/avc
07-26 12:56:41.743 W/VideoCapabilities(26287): Unrecognized profile 2130706434 for video/avc
07-26 12:56:41.774 W/VideoCapabilities(26287): Unrecognized profile/level 0/3 for video/mpeg2
07-26 12:56:41.807 W/VideoCapabilities(26287): Unrecognized profile 2130706433 for video/avc
07-26 12:56:41.807 W/VideoCapabilities(26287): Unrecognized profile 2130706434 for video/avc
I have also tried setting a view client with no apparent change in logs or behavior:
webView.SetWebViewClient(new WebViewClient());
I have been doing more reading and it seems the WebView is generally falling away in favor to Chrome as the main means of loading HTML content in an app. I am considering migrating away from WebView as a result.
Solution
To recap, the layout involved contained a WebView element defined thusly:
<WebView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/grpWebView"
android:foreground="#7D756B"
android:layout_weight="1" />
Apparently, this has been loading data after all. However, the data has not been visible -- this due to the foreground color being set. If I remove the android:foreground attribute I can see the HTML.
Answered By - Bondolin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.