Can't reference to newly added layout in Xamarin - c#

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));

Related

Xamarin.Forms - change device orientation based on device resolution

is there any chance to force change screen orientation based device screen resolution?
Have app for tablets, but I want to run app on mobile device too. I have a few pages, where I need to change orientation to landscape on phone, because then is my listview a litle bit messy.
Thank you for any advice and I apologize for the bad English :)
You can use MessagingCenter to achieve that .
For example , when navigating from MainPage to PageTwo in Forms , the content page of PageTwo as follow :
public partial class PageTwo : ContentPage
{
public PageTwo()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Send(this, "allowLandScape");
}
//during page close setting back to portrait
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Send(this, "quitLandScape");
}
}
In Android , need to modify MainActivity as follow :
[Activity(Label = "ForceOrientation", Icon = "#mipmap/icon", Theme = "#style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
//allowing the device to change the screen orientation based on the rotation
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
MessagingCenter.Subscribe<PageTwo>(this, "allowLandScape", sender =>
{
RequestedOrientation = ScreenOrientation.Landscape;
});
//during page close setting back to portrait
MessagingCenter.Subscribe<PageTwo>(this, "quitLandScape", sender =>
{
RequestedOrientation = ScreenOrientation.Portrait;
});
}
}
In iOS , we need to create a PageTwoRenderer as follow :
[assembly: ExportRenderer(typeof(PageTwo), typeof(PageTwoRenderer))]
namespace ForceOrientation.iOS
{
public class PageTwoRenderer : PageRenderer
{
public PageTwoRenderer()
{
MessagingCenter.Subscribe<PageTwo>(this, "allowLandScape", sender =>
{
UIDevice.CurrentDevice.SetValueForKey(NSNumber.FromNInt((int)(UIInterfaceOrientation.LandscapeLeft)), new NSString("orientation"));
});
//during page close setting back to portrait
MessagingCenter.Subscribe<PageTwo>(this, "quitLandScape", sender =>
{
UIDevice.CurrentDevice.SetValueForKey(NSNumber.FromNInt((int)(UIInterfaceOrientation.Portrait)), new NSString("orientation"));
});
}
}
}
==============================Update=============================
We can use Xamarin.Essentials: Device Information to check whether the device is Phone , Tablet or other devices .
DeviceInfo.Idiom correlates a constant string that maps to the type of device the application is running on. The values can be checked with the DeviceIdiom struct:
DeviceIdiom.Phone – Phone
DeviceIdiom.Tablet – Tablet
DeviceIdiom.Desktop – Desktop
DeviceIdiom.TV – TV
DeviceIdiom.Watch – Watch
DeviceIdiom.Unknown – Unknown
Modiied PageTwo as follow :
protected override void OnAppearing()
{
base.OnAppearing();
var idiom = DeviceInfo.Idiom;
if (idiom == DeviceIdiom.Phone)
{
MessagingCenter.Send(this, "allowLandScape");
}
}
//during page close setting back to portrait
protected override void OnDisappearing()
{
base.OnDisappearing();
var idiom = DeviceInfo.Idiom;
if (idiom == DeviceIdiom.Phone)
{
MessagingCenter.Send(this, "quitLandScape");
}
}
You can force set orientation on these few pages
// on IOS -> AppDelegate.cs
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, UIWindow forWindow)
{
if (Device.Idiom == TargetIdiom.Phone ) {
{
return UIInterfaceOrientationMask.Landscape;
}
else
{
return UIInterfaceOrientationMask.Portrait;
}
}
// and on Android -> MainActivity.cs do the same if else in here
protected override void OnCreate(Bundle savedInstanceState)
{
if (Device.Idiom == TargetIdiom.Phone)
{
RequestedOrientation = ScreenOrientation.Landscape;
}
else
{
RequestedOrientation = ScreenOrientation.Portrait;
}
}
You can check about Device class more in documentation https://learn.microsoft.com/en-us/xamarin/xamarin-forms/platform/device#deviceidiom

Xamarin c# SetLooping missing

I'm novice in programming and this is my first project ever in c# xamarin. I want to set loop on that MediaPlayer but SetLooping method is missing in Android.Media.
I'm using Visual Studio 2017 Community. Here is my code.
Thanks in advance for any feedback :D
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Media;
using Android.Content;
using Android.Preferences;
using Android.Views.Animations;
namespace App2
{
[Activity(Label = "FirstApp 1.34", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Activity
{
MediaPlayer music;
protected override void OnPause()
{
base.OnPause();
music.Pause();
}
protected override void OnDestroy()
{
music.Release();
base.OnDestroy();
}
protected override void OnResume()
{
music.Start();
base.OnResume();
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
music = MediaPlayer.Create(this, Resource.Raw.bgsound);
music.SetVolume(0.7f, 0.7f);
music.Start();
}
SetLooping method is mapped as a property in Xamarin.Android and you should use the Looping property instead. Take a look at: Android.Media.MediaPlayer.Looping Property
Try this on your code
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
music = MediaPlayer.Create(this, Resource.Raw.bgsound);
music.SetVolume(0.7f, 0.7f);
music.Start();
music.Looping = true;
}

Xamarin-Android, setting layout back not working

i have problem with navigating, i can get to the second layout but i can not get back to the first(main) from the second. Even hardware/software back button not working. As far as i know on layout Profil does not work everything, when i try to make button what can change textview on that layout, it does not work too. IDs should be right.
Here is the first activity:
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Home);
Button btnProfil = FindViewById<Button>(Resource.Id.btnProfil);
btnProfil.Click += delegate
{
StartActivity(typeof(Activity1));
};
Here is the second activity:
public class Activity1 : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Profil);
Button btnBack = FindViewById<Button>(Resource.Id.btnBack);
btnBack.Click += delegate
{
StartActivity(typeof(MainActivity));
};
}
}
These formulations works same when they are in same script.
Button btnProfil = FindViewById<Button>(Resource.Id.btnProfil);
btnProfil.Click += delegate { SetContentView(Resource.Layout.Profil); };
Button btnBack = FindViewById<Button>(Resource.Id.btnBack);
btnBack.Click += delegate { SetContentView(Resource.Layout.Home); } ;
Thank you :)

nativeOnDraw failed - Xamarin Android Webview

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);
}

Custom dialog collapsing when title is removed

My app is supposed to have a button in the main activity that when pressed should show a custom dialog box that will be used as a register form for users. The problem is when I remove the title bar of my custom dialog window collapses horizontally. Any advise on how to fix this? Is the idea of using a fragment as a registration form a good one? TIA
Here is the image to Illustrate
Here is what it looks like with the title bar
Here is the code for the Main Activity
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
mbtnfragment = FindViewById<Button>(Resource.Id.MyButton);
mbtnfragment.Click += mbtnfragment_Click;
}
void mbtnfragment_Click(object sender, EventArgs e)
{
FragmentTransaction transaction = FragmentManager.BeginTransaction();
create_dialog dialog = new create_dialog();
dialog.Show(transaction, "Fragment Dialog");
Console.WriteLine("Fragment created");
}
Code for the create_dialog class
class create_dialog: DialogFragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstancesState)
{
base.OnCreateView(inflater, container, savedInstancesState);
var view = inflater.Inflate(Resource.Layout.fragment_dialog, container, false);
return view;
}
public override void OnActivityCreated(Bundle savedInstanceState)
{
Dialog.Window.RequestFeature(WindowFeatures.NoTitle);
base.OnActivityCreated(savedInstanceState);
}
}
}
change the fragment_dialog layout's width from wrap_content to match_parent or a defined size
You can set a style without title to your custom dialog
#Override
public void onCreateDialog(Bundle savedInstanceState) {
setStyle(STYLE_NO_TITLE, 0);
}
Or you can set a theme in the second parameter like Theme.AppCompat.Dialog...

Categories