I'm starting my first Xamarin.Forms app in Visual Studio 2015 on Windows.
My StartUp project is firstApp.Droid.
In MainActivity.cs in firstApp.Droid I have this code:
namespace firstApp.Droid
{
[Activity (Label = "firstApp", Icon = "#drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
global::Xamarin.Forms.Forms.Init (this, bundle);
LoadApplication (new firstApp.App ());
}
}
}
In App.cs I have this code:
public App ()
{
// The root page of your application
MainPage = new NavigationPage(new SensorPage());
}
And in SensorPage.cs I have this auto-generated code:
public class SensorPage : ContentPage
{
public SensorPage ()
{
Content = new StackLayout {
Children = {
new Label { Text = "Hello SensorPage" } //line that causes the error
}
};
}
}
When I try to compile the app, I get the error:
CS0104 'Label' is an ambiguous reference between 'Xamarin.Forms.Label' and 'System.Reflection.Emit.Label'
What should I do to resolve this?
either remove using System.Reflection.Emit; from the top of your SensorPage.cs, or alternately use an explicit namespace for Label:
Content = new StackLayout {
Children = {
new Xamarin.Forms.Label { Text = "Hello SensorPage" }
}
};
Related
I'm trying to understand how actions in xamarin work and i tried to add some of them to my activity. It builds with error "java.exe exited with code 1". I found that the same code works in my main launcher activity, but it doesn't compile when I'm adding it to the other activities.
I tried to search for answer, but there is no topic with that question, so i decide to write here for help.
This is my first time questioning btw ._.
this code builds
namespace XamarinLearningField
{
[Activity(Label = "SomeApp", Theme = "#style/AppTheme", MainLauncher = true)]
[IntentFilter(new[] { Intent.ActionView },
Categories = new[] { Intent.CategorySampleCode, "my.custom.category" })]
public class MainActivity : AppCompatActivity, Android.Views.View.IOnClickListener
{
TextView tvName;
Button btnName;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
tvName = (TextView)FindViewById(Resource.Id.tvName);
btnName = (Button)FindViewById(Resource.Id.btnName);
btnName.SetOnClickListener(this);
Button btnTime = (Button)FindViewById(Resource.Id.btnTime);
Button btnDate = (Button)FindViewById(Resource.Id.btnDate);
btnTime.SetOnClickListener(this);
btnDate.SetOnClickListener(this);
}
}
this not
{
[Activity(Label = "Data/Time info", MainLauncher = false)]
[IntentFilter(new[] { "en.startandroid.intent.action.showtime","en.startandroid.intent.action.showdate" },
Categories = new[] { Intent.CategoryDefault })]
public class InfoActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Info_ActivityLayout);
Intent intent = this.Intent;
String action = intent.Action;
String format = "", textInfo = "";
if (action.Equals("en.startandroid.intent.action.showtime"))
{
format = "HH:mm:ss";
textInfo = "Time: ";
}
else if (action.Equals("en.startandroid.intent.action.showdate"))
{
format = "dd.MM.yyyy";
textInfo = "Date: ";
}
...
}
I added "Exported = false" to Attributes of my extra Activity and it worked.
the code will be like that:
[Activity(Label = "Data/Time info", Exported = false)]
[IntentFilter(new[] { "en.startandroid.intent.action.showtime","en.startandroid.intent.action.showdate" },
Categories = new[] { Intent.CategoryDefault })]
public class InfoActivity : AppCompatActivity
{
...
I want to change the status bar color on some views.
I found this solution https://github.com/yuv4ik/XFDynamicStatusBarAppearance
but it's working only with NavigationPage.
I don't need from my app Navigation Page ...
public App()
{
InitializeComponent();
MainPage = new MainPage();
}
Here is my try ...
var statusBarStyleManager = DependencyService.Get<IStatusBarStyleManager>();
MainCarousel.PositionChanged += (sender, e) =>
{
switch (e.CurrentPosition)
{
case 1:
Device.BeginInvokeOnMainThread(() =>
{
Xamarin.Forms.Application.Current.MainPage.SetValue(Xamarin.Forms.NavigationPage.BarBackgroundColorProperty, Color.DarkCyan);
//((Xamarin.Forms.NavigationPage)Xamarin.Forms.Application.Current.MainPage).BarBackgroundColor = Color.DarkCyan;
statusBarStyleManager.SetDarkTheme();
});
break;
case 0:
case 2:
Device.BeginInvokeOnMainThread(() =>
{
Xamarin.Forms.Application.Current.MainPage.SetValue(Xamarin.Forms.NavigationPage.BarBackgroundColorProperty, Color.LightGreen);
statusBarStyleManager.SetLightTheme();
});
break;
default:
break;
}
};
How can I change the status bar color ?
Here is my working solution on both platforms ...
public interface IStatusBarStyleManager
{
void SetColoredStatusBar(string hexColor);
void SetWhiteStatusBar();
}
Setup the Status bar color with this line
DependencyService.Get<IStatusBarStyleManager>().SetColoredStatusBar("#2196F3");
or you can keep it white with black font color
DependencyService.Get<IStatusBarStyleManager>().SetWhiteStatusBar();
Android
[assembly: Xamarin.Forms.Dependency(typeof(StatusBarStyleManager))]
namespace ShaXam.Droid.DependencyServices
{
public class StatusBarStyleManager : IStatusBarStyleManager
{
public void SetColoredStatusBar(string hexColor)
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
{
Device.BeginInvokeOnMainThread(() =>
{
var currentWindow = GetCurrentWindow();
currentWindow.DecorView.SystemUiVisibility = 0;
currentWindow.SetStatusBarColor(Android.Graphics.Color.ParseColor(hexColor);
});
}
}
public void SetWhiteStatusBar()
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
{
Device.BeginInvokeOnMainThread(() =>
{
var currentWindow = GetCurrentWindow();
currentWindow.DecorView.SystemUiVisibility = (StatusBarVisibility)SystemUiFlags.LightStatusBar;
currentWindow.SetStatusBarColor(Android.Graphics.Color.White);
});
}
}
Window GetCurrentWindow()
{
var window = CrossCurrentActivity.Current.Activity.Window;
// clear FLAG_TRANSLUCENT_STATUS flag:
window.ClearFlags(WindowManagerFlags.TranslucentStatus);
// add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
return window;
}
}
}
iOS
[assembly: Dependency(typeof(StatusBarStyleManager))]
namespace ShaXam.iOS.DependencyServices
{
public class StatusBarStyleManager : IStatusBarStyleManager
{
public void SetColoredStatusBar(string hexColor)
{
Device.BeginInvokeOnMainThread(() =>
{
UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
if (statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:")))
{
statusBar.BackgroundColor = Color.FromHex(hexColor).ToUIColor();
}
UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, false);
GetCurrentViewController().SetNeedsStatusBarAppearanceUpdate();
});
}
public void SetWhiteStatusBar()
{
Device.BeginInvokeOnMainThread(() =>
{
UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
if (statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:")))
{
statusBar.BackgroundColor = UIColor.White;
}
UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.Default, false);
GetCurrentViewController().SetNeedsStatusBarAppearanceUpdate();
});
}
UIViewController GetCurrentViewController()
{
var window = UIApplication.SharedApplication.KeyWindow;
var vc = window.RootViewController;
while (vc.PresentedViewController != null)
vc = vc.PresentedViewController;
return vc;
}
}
}
POST UPDATED TO SUPPORT THE IOS 13
[assembly: Dependency(typeof(StatusBarStyleManager))]
namespace ShaXam.iOS.DependencyServices
{
public class StatusBarStyleManager : IStatusBarStyleManager
{
public void SetColoredStatusBar(string hexColor)
{
Device.BeginInvokeOnMainThread(() =>
{
if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
{
UIView statusBar = new UIView(UIApplication.SharedApplication.KeyWindow.WindowScene.StatusBarManager.StatusBarFrame);
statusBar.BackgroundColor = Color.FromHex(hexColor).ToUIColor();
UIApplication.SharedApplication.KeyWindow.AddSubview(statusBar);
}
else
{
UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
if (statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:")))
{
statusBar.BackgroundColor = Color.FromHex(hexColor).ToUIColor();
}
}
UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, false);
GetCurrentViewController().SetNeedsStatusBarAppearanceUpdate();
});
}
public void SetWhiteStatusBar()
{
Device.BeginInvokeOnMainThread(() =>
{
if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
{
UIView statusBar = new UIView(UIApplication.SharedApplication.KeyWindow.WindowScene.StatusBarManager.StatusBarFrame);
statusBar.BackgroundColor = UIColor.White;
UIApplication.SharedApplication.KeyWindow.AddSubview(statusBar);
}
else
{
UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
if (statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:")))
{
statusBar.BackgroundColor = UIColor.White;
}
}
UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.DarkContent, false);
GetCurrentViewController().SetNeedsStatusBarAppearanceUpdate();
});
}
UIViewController GetCurrentViewController()
{
var window = UIApplication.SharedApplication.KeyWindow;
var vc = window.RootViewController;
while (vc.PresentedViewController != null)
vc = vc.PresentedViewController;
return vc;
}
}
}
The full working sample is here https://github.com/georgemichailou/ShaXam
Answer from #Cherry Bu - MSFT is great, but a bit outdated. SystemUiVisibility is obsolete in Android 11, so here is updated StatusBarStyleManager compatible with new API:
using Android.Graphics;
using Android.OS;
using Android.Views;
using Plugin.CurrentActivity;
using Xamarin.Essentials;
[assembly: Xamarin.Forms.Dependency(typeof(StatusBarStyleManager))]
namespace YourApp.Droid.Dependences
{
public class StatusBarStyleManager : IStatusBarStyleManager
{
public void SetColoredStatusBar(string hexColor)
{
if (Build.VERSION.SdkInt < BuildVersionCodes.M)
{
return;
}
MainThread.BeginInvokeOnMainThread(() =>
{
var currentWindow = GetCurrentWindow();
SetStatusBarIsLight(currentWindow, false);
currentWindow.SetStatusBarColor(Color.ParseColor(hexColor));
currentWindow.SetNavigationBarColor(Color.ParseColor(hexColor));
});
}
public void SetWhiteStatusBar()
{
if (Build.VERSION.SdkInt < BuildVersionCodes.M)
{
return;
}
MainThread.BeginInvokeOnMainThread(() =>
{
var currentWindow = GetCurrentWindow();
SetStatusBarIsLight(currentWindow, true);
currentWindow.SetStatusBarColor(Color.White);
currentWindow.SetNavigationBarColor(Color.White);
});
}
private static void SetStatusBarIsLight(Window currentWindow, bool isLight)
{
if ((int)Build.VERSION.SdkInt < 30)
{
#pragma warning disable CS0618 // Type or member is obsolete. Using new API for Sdk 30+
currentWindow.DecorView.SystemUiVisibility = isLight ? (StatusBarVisibility)(SystemUiFlags.LightStatusBar) : 0;
#pragma warning restore CS0618 // Type or member is obsolete
}
else
{
var lightStatusBars = isLight ? WindowInsetsControllerAppearance.LightStatusBars : 0;
currentWindow.InsetsController?.SetSystemBarsAppearance((int)lightStatusBars, (int)lightStatusBars);
}
}
private Window GetCurrentWindow()
{
Window window = Platform.CurrentActivity.Window;
window.ClearFlags(WindowManagerFlags.TranslucentStatus);
window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
return window;
}
}
}
Side note: if you also want to change color of status bar, here are lines that you need to change:
currentWindow.DecorView.SystemUiVisibility = isLight ? (StatusBarVisibility)(SystemUiFlags.LightStatusBar | SystemUiFlags.LightNavigationBar) : 0;
var lightStatusBars = isLight ? WindowInsetsControllerAppearance.LightStatusBars | WindowInsetsControllerAppearance.LightNavigationBars : 0;
Update - Summer 2021:
This code (more or less) is now part of Xamarin.CommunityToolkit: https://github.com/xamarin/XamarinCommunityToolkit/pull/812.
I'd recommend using it from there, and in case of any problems open an issue, so it's fixed for everybody.
Currently XCT targets Android 10, so there's no Android 11 related code, but when this logic will be migrated to CommunityToolkit.Maui, Android 11 logic will be added as well (both XCT and CT.MAUI work just fine on any Android version, just wanted to point out code differences)
You can try add this code in Android platform, OnCreate method:
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
Window.SetStatusBarColor(Android.Graphics.Color.Argb(255, 0, 0, 0)); //here
}
Please note SetStatusBarColor is only supported in API level 21 and up. Therefore, we should check for this before calling SetStatusBarColor.
if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
Window.SetStatusBarColor(...);
}
Update:
Create interface in PCL:
public interface IStatusBarColor
{
void changestatuscolor(string color);
}
Now, Mainactivity implement this interface.
[assembly: Dependency(typeof(demo2.Droid.MainActivity))]
namespace demo2.Droid
{
[Activity(Label = "demo2", Icon = "#mipmap/icon", Theme = "#style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity,IStatusBarColor
{
public static Context context;
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
public void changestatuscolor(string color)
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
var c = MainActivity.context as FormsAppCompatActivity;
c?.RunOnUiThread(() => c.Window.SetStatusBarColor(Android.Graphics.Color.ParseColor(color)));
}
}
protected override void OnResume()
{
context = this;
base.OnResume();
}
}
}
In Mainpage, to change status bar color in Button click event.
private void Changecolor_Clicked(object sender, EventArgs e)
{
DependencyService.Get<IStatusBarColor>().changestatuscolor(Color.Red.ToHex());
}
As said by #maxc137, this feature is now available in the Xamarin Community Toolkit. You just have to add the Xamarin.CommunityToolkit package to your projects and set the status bar color like this:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Example.Views.MyPage"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
xct:StatusBarEffect.Color="Red"
Title="My Page">
If you want to set the same color across all pages, then set a global implicit style:
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
x:Class="Example.App">
<Application.Resources>
<ResourceDictionary>
<Style TargetType="ContentPage" ApplyToDerivedTypes="True">
<Setter Property="xct:StatusBarEffect.Color" Value="Red"/>
</Style>
</ResourceDictionary>
</Application.Resources>
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
I want to use a variabele or method from my MainPage in my MainActivity. I have created an interface for my DependencyService but I always have the same error at
DependencyService.Get<IHPost>.HPost("R");
Method not valid in the given context
I followed the official documentation,so I created my interface in the PCL:
IHPost (public interface)
In Xamarin.Android i have written the implementation HPost_droid with
[assembly: Dependency(typeof(HPost_droid))] line
and in MainActivity i added this interface.
Code :
IHPost.cs (Interface)
namespace Xam
{
public interface IHPost
{
void HPost(string val);
}
}
MainPage.xaml.cs
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
// DependencyService.Get<IHPost>.HPost("R");
}
}
HPost_droid (Class)
[assembly: Dependency(typeof(HPost_droid))]
namespace Xam.Droid
{
class HPost_droid : Java.Lang.Object, IHPost
{
public HPost_droid() { }
public void HPost(string val)
{
System.Diagnostics.Debug.Write(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + val);
}
}
}
MainActivity
[assembly: Xamarin.Forms.Dependency(typeof(IHPost))]
namespace Xam.Droid
{
[Activity(Label = "Xam", Icon = "#drawable/icon", Theme = "#style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity, IHPost
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
DependencyService.Get<IHPost>.HPost("R");
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
}
}
I retry from scratch application following this perfect tutorial : https://wolfprogrammer.com/2016/08/05/dependency-services-with-xamarin-forms/
and it's work !
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);
}