summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.ControlGallery.Android/BrokenNativeControl.cs
blob: c75e7ca9bdfc08fc03778f85018304004b354d09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using Android.Content;
using Android.Graphics;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;

namespace Xamarin.Forms.ControlGallery.Android
{
	/// <summary>
	///     This is a custom Android control which deliberately does some incorrect measuring
	/// </summary>
	internal class BrokenNativeControl : TextView
	{
		bool _on;

		public BrokenNativeControl (IntPtr javaReference, JniHandleOwnership transfer) : base (javaReference, transfer)
		{
		}

		public BrokenNativeControl (Context context) : base (context)
		{
		}

		public BrokenNativeControl (Context context, IAttributeSet attrs) : base (context, attrs)
		{
		}

		public BrokenNativeControl (Context context, IAttributeSet attrs, int defStyleAttr)
			: base (context, attrs, defStyleAttr)
		{
		}

		public BrokenNativeControl (Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes)
			: base (context, attrs, defStyleAttr, defStyleRes)
		{
		}

		public override bool OnTouchEvent (MotionEvent e)
		{
			_on = !_on;

			SetTypeface (null, _on ? TypefaceStyle.Bold : TypefaceStyle.Normal);

			return base.OnTouchEvent (e);
		}

		protected override void OnMeasure (int widthMeasureSpec, int heightMeasureSpec)
		{
			int width = MeasureSpec.GetSize (widthMeasureSpec);

			// Force the width to 1/2 of what's being requested. This is deliberately wrong so we can demo 
			// giving the LayoutExtensions an override to fix it with
			int widthSpec = MeasureSpec.MakeMeasureSpec (width / 2, MeasureSpec.GetMode (widthMeasureSpec));

			base.OnMeasure (widthSpec, heightMeasureSpec);
		}
	}
}