Im using xamarin for android and i have looked into this issue but nothing seems to work.
Im trying to get a webview to work with a button but it keeps saying whenever i click on the button.
nativeOnDraw failed; clearing to background color.
I will include image of my layout and my code here.
MainActivity
namespace Application
{
[Activity(Label = "Application", MainLauncher = true, Icon = "#drawable/icon", Theme = "#android:style/Theme.NoTitleBar")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
WebPage webPage = new WebPage();
Button btn_MySchedule = FindViewById<Button>(Resource.Id.btnSchedule);
Button btn_MyCareer = FindViewById<Button>(Resource.Id.btnCareer);
Button btn_MySocial = FindViewById<Button>(Resource.Id.btnSocial);
Button btn_Feedback = FindViewById<Button>(Resource.Id.btnFeedback);
Button btn_MyDetails = FindViewById<Button>(Resource.Id.btnDetails);
btn_MySchedule.Click += delegate
{
webPage.isSchedule = true;
//wPage.SetUrl("https://www.google.com");
StartActivity(typeof(WebPage));
};
}
}
}
WebPage
namespace Application
{
[Activity(Label = "WebPage", Theme = "#android:style/Theme.NoTitleBar")]
public class WebPage : Activity
{
public bool isSchedule;
private WebView _webView;
private string _url;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.WebPage);
_webView = FindViewById<WebView>(Resource.Id.webView);
new DisplayWebPage(_webView, GetUrl());
}
public string GetUrl()
{
return _url;
}
public void SetUrl(string sUrl)
{
_url = sUrl;
}
private void ButtonCLicked()
{
if (isSchedule == true)
{
SetUrl("https://www.google.com");
Toast.MakeText(this, "isSchedule is true", ToastLength.Short).Show();
}
}
}
}
I have found the issue. It was due to me calling an empty string via the GetUrl() methods.
The Solution I used is:
[Export("MyScheduleClicked")]
public void MyScheduleClicked(View v)
{
var activity2 = new Intent(this, typeof(WebPage));
activity2.PutExtra("MyURL", "https://www.google.com");
StartActivity(activity2);
}
Related
ConnectivityManager connMgr = (ConnectivityManager)this.Activity.GetSystemService(Android.Content.Context.ConnectivityService);
NetworkInfo networkInfo = connMgr.ActiveNetworkInfo;
bool isConnected = networkInfo.IsConnected;
this code is in the onCreateView method in my fragment and when i run the app i get the error:
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object. occurred
it would be great if someone would explain to me what "not set to an instance of an object means" as i dont quite understand. still a little new to xamarin and c# in general.
heres the fragament1.cs file:
using Android.OS;
using Android.Support.V4.App;
using Android.Views;
using Android.Webkit;
namespace TabsApp.Fragments
{
public class Fragment1 : Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public static Fragment1 NewInstance()
{
var frag1 = new Fragment1 { Arguments = new Bundle() };
return frag1;
}
WebView webView;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
View v = inflater.Inflate(Resource.Layout.fragment1, container, false);
webView = v.FindViewById<WebView>(Resource.Id.webView);
webView.LoadUrl("https://google.com");
webView.Settings.JavaScriptEnabled = true;
webView.SetWebViewClient(new WebViewClient());
return v;
}
public void OnBackPressed()
{
webView.GoBack();
}
}
}
and heres the MainActivity.cs file:
using Android.App;
using Android.OS;
using TabsApp.Fragments;
using Android.Content.PM;
using Android.Support.Design.Widget;
using Android.Support.V7.App;
namespace TabsApp
{
[Activity(Label = "#string/app_name", MainLauncher = true, LaunchMode = Android.Content.PM.LaunchMode.SingleTop, Icon = "#drawable/icon",
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : AppCompatActivity
{
BottomNavigationView bottomNavigation;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.main);
var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
if (toolbar != null)
{
SetSupportActionBar(toolbar);
SupportActionBar.SetDisplayHomeAsUpEnabled(false);
SupportActionBar.SetHomeButtonEnabled(false);
}
bottomNavigation = FindViewById<BottomNavigationView>(Resource.Id.bottom_navigation);
bottomNavigation.NavigationItemSelected += BottomNavigation_NavigationItemSelected;
LoadFragment(Resource.Id.menu_home);
}
private void BottomNavigation_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
{
LoadFragment(e.Item.ItemId);
}
void LoadFragment(int id)
{
Android.Support.V4.App.Fragment fragment = null;
switch (id)
{
case Resource.Id.menu_home:
fragment = Fragment1.NewInstance();
break;
case Resource.Id.menu_audio:
fragment = Fragment2.NewInstance();
break;
case Resource.Id.menu_video:
fragment = Fragment3.NewInstance();
break;
}
if (fragment == null)
return;
SupportFragmentManager.BeginTransaction()
.Replace(Resource.Id.content_frame, fragment)
.Commit();
}
}
}
Well its pretty simple this looks to me like you do not have your activity context here and for that reason you are unable to get the connMgr
What you need to do is when you call your fragment from your activity pass the current activity to your ~Fragment~ something like this on your fragment call:
Fragment1 newInstance(this);
Receive it in your fragment with something like this in your constructor:
public static AppCompatActivity _activity
public static Fragment1 newInstance(AppCompatActivity activity )
{
_activity =activity ;
//Lines of code }
And in your onCreateView use it like this :
ConnectivityManager connMgr = (ConnectivityManager)_activity.GetSystemService(Android.Content.Context.ConnectivityService);
NetworkInfo networkInfo = connMgr.ActiveNetworkInfo;
bool isConnected = networkInfo.IsConnected;
If it doesn't work lemme know! Goodluck
In my app I have implemented a webview to show a website link into that webview. Now that website has a button that contains a pdf file link. If I click on that button on website it shows a pdf file on the web. But if I try to open it into the webview in my app, nothing happens. I am very new in Xamarin android. I could not find any suitable way to that. Here is my code to show the website into the webview.
I want to relod the pdf when click on link from the website. But the result is same as before
Modified Code
namespace Xamarin.PDFView
{
[Activity (Label = "PDFView", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Activity
{
private string _documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
private string _pdfPath;
private string _pdfFileName = "thePDFDocument.pdf";
private string _pdfFilePath;
private WebView _webView;
private string _webUrl = "https://Link of thewebsite";
private string _pdfURL = "https://Link of thewebsite/25052016.pdf";
private WebClient _webClient = new WebClient();
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
_webView = FindViewById<WebView> (Resource.Id.webView1);
var settings = _webView.Settings;
settings.JavaScriptEnabled = true;
settings.AllowFileAccessFromFileURLs = true;
settings.AllowUniversalAccessFromFileURLs = true;
settings.BuiltInZoomControls = true;
_webView.SetWebViewClient(new WebViewClient());
_webView.SetWebChromeClient(new WebChromeClient());
_webView.LoadUrl(_webUrl);
}
public override bool OnTouchEvent(MotionEvent e)
{
if (_webUrl.Contains(".pdf"))
{
DownloadPDFDocument();
}
return base.OnTouchEvent(e);
}
protected override void OnResume ()
{
base.OnResume ();
_webView.LoadUrl( "javascript:window.location.reload( true )" );
}
protected override void OnPause ()
{
base.OnPause ();
_webView.ClearCache(true);
}
private void DownloadPDFDocument()
{
AndHUD.Shared.Show(this, "Downloading PDF\nPlease Wait ..", -1, MaskType.Clear);
_pdfPath = _documentsPath + "/PDFView";
_pdfFilePath = Path.Combine(_pdfPath, _pdfFileName);
// Check if the PDFDirectory Exists
if(!Directory.Exists(_pdfPath)){
Directory.CreateDirectory(_pdfPath);
}
else{
// Check if the pdf is there, If Yes Delete It. Because we will download the fresh one just in a moment
if (File.Exists(_pdfFilePath)){
File.Delete(_pdfFilePath);
}
}
// This will be executed when the pdf download is completed
_webClient.DownloadDataCompleted += OnPDFDownloadCompleted;
// Lets downlaod the PDF Document
var url = new Uri(_pdfURL);
_webClient.DownloadDataAsync(url);
}
private void OnPDFDownloadCompleted (object sender, DownloadDataCompletedEventArgs e)
{
// Okay the download's done, Lets now save the data and reload the webview.
var pdfBytes = e.Result;
File.WriteAllBytes (_pdfFilePath, pdfBytes);
if(File.Exists(_pdfFilePath))
{
var bytes = File.ReadAllBytes(_pdfFilePath);
}
_webView.LoadUrl("file:///android_asset/pdfviewer/index.html?file=" + _pdfFilePath);
AndHUD.Shared.Dismiss();
}
public class HelloWebViewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
{
view.LoadUrl(request.Url.ToString());
return false;
}
}
}
}
Mentioned in the link below:
Read this link
You will need to implement following code using WebViewRenderer
namespace DisplayPDF.Droid
{
public class CustomWebViewRenderer : WebViewRenderer
{
protected override void OnElementChanged (ElementChangedEventArgs<WebView> e)
{
base.OnElementChanged (e);
if (e.NewElement != null) {
var customWebView = Element as CustomWebView;
Control.Settings.AllowUniversalAccessFromFileURLs = true;
Control.LoadUrl (string.Format ("file:///android_asset/pdfjs/web/viewer.html?file={0}", string.Format ("file:///android_asset/Content/{0}", WebUtility.UrlEncode (customWebView.Uri))));
}
}
}
}
is there any way to show pdf file from a link
As discussed android webview doesn't support pdf files. As a workaround, you can use Google Docs in android webview:
Create a custom webview client and override ShouldOverrideUrlLoading like this:
public class MyWebViewClient: WebViewClient
{
public override bool ShouldOverrideUrlLoading(WebView view, string url)
{
//if PDF file then use google client to show it
if (url.ToLower().EndsWith(".pdf"))
{
var newUrl = "https://docs.google.com/viewer?url=" + url;
view.LoadUrl(newUrl);
}
return true;
}
}
Use your custom webview client:
mWebview.SetWebViewClient(new MyWebViewClient());
Notes: If you use android:layout_height="wrap_content" for your webview, you need to change it to a fixed dp like android:layout_height="400dp", otherwise the webview won't show pdf correctly.
There was a problem parsing the package while installing .apk file.
Given below is configuration and code.
VS2015
MainActivity.cs
namespace WebAp
{
[Activity(Label = "TC", MainLauncher = true, Theme ="#android:style/Theme.NoTitleBar")]
public class MainActivity : Activity
{
private WebView web_view;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
web_view = FindViewById<WebView>(Resource.Id.webview);
web_view.Settings.JavaScriptEnabled = true;
web_view.LoadUrl("http://www.tc.com");
web_view.SetWebViewClient(new HelloWebViewClient());
}
public class HelloWebViewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(WebView view, string url)
{
view.LoadUrl(url);
return true;
}
}
public override bool OnKeyDown(Android.Views.Keycode keyCode, Android.Views.KeyEvent e)
{
if (keyCode == Android.Views.Keycode.Back && web_view.CanGoBack())
{
web_view.GoBack();
return true;
}
return base.OnKeyDown(keyCode, e);
}
}
}
Configurataion :
enter image description here
Thats it my code. I was also try different API for Compile, Min and target version.
Please help me thanks.
( i use visual studio 2015 Xamarin )
this is the message that i see after few seconds that my android app starts:
" Android.Util.AndroidRuntimeException: Only the original thread that created a view hierarchy can touch its views. "
-My app should should write continuously current date and time in a TextView.
this is the code i have written.. where is the error? how can i fix it?
namespace App4
{
[Activity(Label = "app4", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Activity
{
private delegate void delegato();
private delegato del1;
public MainActivity()
{
del1 = getdatehour;
}
private void Do()
{
del1();
}
private void getdatehour()
{
TextView datehour = (TextView)FindViewById(Resource.Id.textView1);
DateTime date;
while (true)
{
date = DateTime.Now;
String s = "DATA E ORA: " + date;
datehour.Text = s;
}
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
Thread thdo = new Thread(Do);
thdo.Start();
}
}
}
I wanna point out that i'm a total newbie in Xamarin.
My goal is to create simple app that after you push the button, it will jump to another layout. My problem is, that after i created a new layout(called layout1) and new activity, I can't reference to this layout in my MainActivity.
freshly created Activity:
namespace Layouts
{
[Activity(Label = "Activity1")]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.layout1);
}
}
}
And my MainActivity:
namespace Layouts
{
[Activity(Label = "Layouts", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView (Resource.Layout.Main);
Button button1 = FindViewById<Button>(Resource.Id.button);
button1.Click += delegate
{
StartActivity(typeof(layout1)); //can't reference layout1 there
};
}
}
}
Any help will be great, thanks :)
StartActivity(typeof(Activity1));