MonoDroid - Draw Ellipse in runtime - c#

I am newbie in MonoDroid. How can I draw ellipse in runtime using C# in Android Application?

To draw an ellipse, or other geometric shapes, you can use the canvas object. Here's a very basic bit of code that will draw an ellipse (oval). I basically just created a view and overrode the OnDraw method to draw the ellipse. You define a RectF object which defines the rectangular boundary of the ellipse. A good reference is the Android SDK:
http://developer.android.com/reference/android/graphics/Canvas.html
[Activity(Label = "MonoAndroidApplication1", MainLauncher = true, Icon = "#drawable/icon")]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
var targetView = new OvalView(this);
SetContentView(targetView);
}
}
public class OvalView : View
{
public OvalView(Context context) : base(context) { }
protected override void OnDraw(Canvas canvas)
{
RectF rect = new RectF(0,0, 300, 300);
canvas.DrawOval(rect, new Paint() { Color = Color.CornflowerBlue });
}
}

Related

OnSizeAllocated not being hit for .NET MAUI (iOS) apps on screen rotation

It appears when I create a new MAUI app, the event OnSizeAllocated(double, double) is not being fired on screen rotation when that page is a FlyoutPage.
Here is how my page is launched:
private void LaunchPage(ContentPage page)
{
FlyoutPage FlyoutPage = new FlyoutPage
{
Detail = new NavigationPage(page)
{
BarBackgroundColor = Color.FromArgb("#000000")
},
Flyout = new DrawerMenu(),
FlyoutLayoutBehavior = FlyoutLayoutBehavior.Popover
};
((App)Application.Current).MainPage = FlyoutPage;
}
public partial class MyPage : ContentPage
{
protected override void OnSizeAllocated(double width, double height)
{
// breakpoint here is not hit when the screen is rotated
base.OnSizeAllocated(width, height);
}
}
// MyPage is defined as a Transient
ContentPage page = serviceProvider.GetRequiredService<MyPage>() as ContentPage;
LaunchPage(page);
public partial class MyPage : ContentPage
{
protected override void OnSizeAllocated(double width, double height)
{
// breakpoint here is not hit when the screen is rotated
base.OnSizeAllocated(width, height);
}
}
Note: OnSizeAllocated is hit the first time the page is loaded. Just not on screen rotation.
Also note that this works perfectly on Android. So it appears to be limited to iOS.
Also note: if I just set ((App)Application.Current).MainPage = page without wrapping it in a FlyoutPage, the event is fired on iOS without a problem. So something about being a FlyoutPage doesn't work with the rotation on iOS.
I have created a repo here to demonstrate:
https://github.com/mikeluken/MauiSample
Is this a bug? Has anybody found a workaround?

Using Custom XAML Controls in the Xamarin.Android Project with a Keyboard Service App

I have a custom keyboard app I'm trying to write. I'd like to define the key layout as XAML control (KeyboardPage.xaml) in my shared code and then incorporate them into native code in the .Android and .iOS projects. Being primarily a desktop WPF developer, I'm comfortable with this approach.
When my MainActivity inherits from FormsAppCompatActivity or AppCompatActivity, this is pretty straight forward.
namespace MobileTestApp2.Droid
{
[Activity(Label = "MobileTestApp2", Icon = "#mipmap/icon", Theme = "#style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(savedInstanceState);
Forms.Init(this, bundle);
LoadApplication(new App());
}
}
}
However, in order for the app to register as an Android keyboard service, MainActivity has to inherit from InputMethodService:
namespace TestKeyboard.Droid
{
[Service(Permission = "android.permission.BIND_INPUT_METHOD", Label = "Test Keyboard")]
[MetaData("android.view.im", Resource = "#xml/method")]
[IntentFilter(new string[] { "android.view.InputMethod" })]
public class MainActivity : InputMethodService, KeyboardView.IOnKeyboardActionListener
{
private KeyboardView keyboardView;
private Keyboard keyboard;
private bool isCaps = false;
public override View OnCreateInputView()
{
keyboardView = (KeyboardView)LayoutInflater.Inflate(Resource.Layout.Keyboard, null);
keyboard = new Keyboard(this, Resource.Xml.Qwerty);
keyboardView.Keyboard = keyboard;
keyboardView.OnKeyboardActionListener = this;
return keyboardView;
}
}
I'm not sure how to convert my XAML controls into Android controls; and how to get them to replace the Keyboard.axml and.or qwerty.xml layouts I have defined in the .Android project.
From research, I found that this is possible using the CreateSupportFragment() method and the SupportFragmentManager member. However, the SupportFragmentManager member is only available when inheriting from FormsAppCompatActivity or AppCompatActivity. I tried instantiating a local object inheriting from FormsAppCompatActivity and replacing my Keyboard.axml and qwerty.xml layouts with my converted XAML control (KeyboardPage.xaml) before I instantiated a new Keyboard object; but it didn't work. I'm not even sure if the OnCreate() method is even being called.
namespace MurrayRegisterKeys.Droid
{
public class MainActivity : InputMethodService, KeyboardView.IOnKeyboardActionListener
{
private KeyboardView keyboardView = null;
private Keyboard keyboard = null;
private bool isQwerty = false;
public override View OnCreateInputView()
{
new InnerActivity();
KeyboardView keyboardView = (KeyboardView)LayoutInflater.Inflate(Resource.Layout.Keyboard, null);
keyboard = new Keyboard(this, Resource.Xml.Keypad);
keyboardView.Keyboard = keyboard;
keyboardView.OnKeyboardActionListener = this;
return keyboardView;
}
}
public class InnerActivity : FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Forms.Init(this, bundle);
SetContentView(Resource.Layout.Keyboard);
Android.Support.V4.App.Fragment keyboardPage = new KeyboardPage().CreateSupportFragment(this);
SupportFragmentManager
.BeginTransaction()
.Replace(Resource.Xml.Qwerty, keyboardPage)
.Commit();
}
}
}
Any ideas? Is what I'm trying to do even possible? Are there alternate approaches for creating a new default keyboard service?
Thanks!

How can I add a listener on android scroll view? C#

I'm currently developing for Android using C# and Xamarin.
I want to add a listener to the scroll views that I have within my app, so that it picks up when a user is touching the scroll bar.
How can I go about this?
Thanks!
Refer to this , in xamarin.android usage like this
public class MainActivity : Activity, IOnScrollChangedListener
{
ScrollView msv;
public void OnScrollChanged()
{
float scrollY = msv.ScrollX;
float scrollX = msv.ScrollY;
System.Diagnostics.Debug.Write("x:=" + scrollY + " y=" + scrollX );
//do something
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
msv = FindViewById<ScrollView>(Resource.Id.sv);
msv.ViewTreeObserver.AddOnScrollChangedListener(this);
}
}
you can also write up a class MyScrollView to extend ScrollView,and then override OnTouchEvent or onScrollChanged(int x, int y, int oldX, int oldY)method to implement your function.

Coloring a margin on Avalonedit

I have added a margin (For adding breakpoints) to the left side of my TextEditor in the following manner:
public partial class LogicSimViewCodeWPFCtrl : UserControl
{
private class BreakPointMargin : AbstractMargin
{
private const int margin = 20;
protected override HitTestResult HitTestCore(PointHitTestParameters hitTestParameters)
{
return new PointHitTestResult(this, hitTestParameters.HitPoint);
}
protected override Size MeasureOverride(Size availableSize)
{
return new Size(margin, 0);
}
}
}
private void LogicCodeInit()
{
try
{
TxtEditCodeViewer.TextArea.LeftMargins.Insert(0, new BreakPointMargin());
...
The margin is added successfully but now I'd like to color the background of the margin. How can I accomplish this?
https://web.archive.org/web/20190716171503/http://community.sharpdevelop.net/forums/t/16047.aspx
You would have to override OnRender:
protected override void OnRender(DrawingContext drawingContext)
{
Size renderSize = this.RenderSize;
drawingContext.DrawRectangle(SystemColors.ControlBrush, null,
new Rect(0, 0, renderSize.Width, renderSize.Height));
Also, you aren't required to derived from AbstractMargin - you can use any WPF control you want. AbstractMargin just provides the TextView and Document properties and keeps them up to date. If you don't need those or can implement them yourself, you can just use another base class.

graphics panel in c#

Instead of setting image using 'background' property, I would like to draw the image using Graphics class on a panel. How can I do it in C#.Net?
you can try following piece of code.
public class ImagePanel:Panel
{
private Image image;
public Image Image
{
get { return image; }
set
{
image = value;
Refresh();
}
}
protected override void OnPaint(PaintEventArgs e)
{
if(Image!=null)
{
e.Graphics.DrawImage(this.Image,Point.Empty);
}
base.OnPaint(e);
}
}
Make use of System.Drawing.Graphics class to draw the things.
Details : http://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx related to drawing
example : http://www.techotopia.com/index.php/Drawing_Graphics_in_C_Sharp

Categories

Resources