forked from syncfusion/xamarin-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumericUpDown_Mobile.cs
524 lines (470 loc) · 22.6 KB
/
NumericUpDown_Mobile.cs
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#region Copyright Syncfusion Inc. 2001-2022.
// Copyright Syncfusion Inc. 2001-2022. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// [email protected]. Any infringement will be prosecuted under
// applicable laws.
using Com.Syncfusion.Numericupdown;
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Com.Syncfusion.Numerictextbox;
using Android.Graphics;
using Android.Views.InputMethods;
using Android.Text;
using Android.Text.Style;
namespace SampleBrowser
{
public class NumericUpDown_Mobile
{
/*********************************
**Local Variable Inizialisation**
*********************************/
LinearLayout propertylayout, autoReverseLayout, minimumValueLayout, maximumValueLayout, fontSizeLayOut, selectAllOnFocusLayout;
ArrayAdapter<String> spinButtonDataAdapter;
Spinner spinButtonSpinner;
bool autoReverse = false, selectAllOnFocus = false;
EditText minimumText, maximumText, fontSizeText;
SpinButtonAlignment spinButtonAlignment = SpinButtonAlignment.Right;
double minimumValue = 0, maximumValue = 100, fontSizeValue = 18;
Boolean maxNegative, minNegative, fontsizeNegative;
SfNumericUpDown adultNumericUpDown, infantsNumericUpDown;
TextView adultText, infantsText;
Context context;
int width;
GravityFlags gravity=GravityFlags.CenterVertical;
public View GetSampleContent(Context con)
{
int width = con.Resources.DisplayMetrics.WidthPixels - 40;
float density = con.Resources.DisplayMetrics.Density;
int numerHeight = (int)(density * 55);
if (density >= 3)
{
numerHeight = (int)(density * 65);
}
SamplePageContent(con);
//AdultNumericUpDown
adultNumericUpDown = new SfNumericUpDown(con);
adultNumericUpDown.FontSize = 18;
adultNumericUpDown.TextGravity = GravityFlags.CenterVertical;
adultNumericUpDown.LayoutParameters = new ViewGroup.LayoutParams(width, numerHeight);
adultNumericUpDown.Minimum = 0;
adultNumericUpDown.Maximum = 100;
adultNumericUpDown.Value = 5;
adultNumericUpDown.FormatString = "N";
adultNumericUpDown.AutoReverse = false;
adultNumericUpDown.MaximumDecimalDigits = 0;
adultNumericUpDown.StepValue = 1;
//InfantsNumericUpDown
infantsNumericUpDown = new SfNumericUpDown(con);
infantsNumericUpDown.FontSize = 18;
infantsNumericUpDown.TextGravity = GravityFlags.CenterVertical;
infantsNumericUpDown.LayoutParameters = new ViewGroup.LayoutParams(width, numerHeight);
infantsNumericUpDown.Minimum = 0;
infantsNumericUpDown.Maximum = 100;
infantsNumericUpDown.Value = 2;
infantsNumericUpDown.FormatString = "N";
infantsNumericUpDown.AutoReverse = false;
infantsNumericUpDown.MaximumDecimalDigits = 0;
infantsNumericUpDown.StepValue = 1;
//MainFrameLayout
FrameLayout mainFrameLayout = new FrameLayout(con);
mainFrameLayout.SetPadding(10, 10, 10, 10);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent, GravityFlags.FillVertical);
mainFrameLayout.LayoutParameters = layoutParams;
mainFrameLayout.AddView(GetView(con));
ScrollView mainScrollView = new ScrollView(con);
mainScrollView.AddView(mainFrameLayout);
return mainScrollView;
}
private LinearLayout GetView(Context con)
{
//mainLayout
LinearLayout mainLayout = new LinearLayout(con);
mainLayout.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
mainLayout.SetGravity(GravityFlags.FillVertical);
mainLayout.Orientation = Orientation.Vertical;
mainLayout.AddView(adultText);
mainLayout.AddView(adultNumericUpDown);
mainLayout.AddView(infantsText);
mainLayout.AddView(infantsNumericUpDown);
//MainLayout Touch Event
mainLayout.Touch += (object sender, View.TouchEventArgs e) => {
if (adultNumericUpDown.IsFocused || infantsNumericUpDown.IsFocused)
{
Rect outRect = new Rect();
adultNumericUpDown.GetGlobalVisibleRect(outRect);
infantsNumericUpDown.GetGlobalVisibleRect(outRect);
if (!outRect.Contains((int)e.Event.RawX, (int)e.Event.RawY))
{
adultNumericUpDown.ClearFocus();
infantsNumericUpDown.ClearFocus();
}
hideSoftKeyboard((Activity)con);
}
};
return mainLayout;
}
private void SamplePageContent(Context con)
{
//AdultText
adultText = new TextView(con);
adultText.Text = "Number of adults";
adultText.TextSize = 18;
//InfantsText
infantsText = new TextView(con);
infantsText.SetPadding(0, 15, 0, 0);
infantsText.Text = "Number of infants";
infantsText.TextSize = 18;
}
//Apply Changes Method
public void OnApplyChanges()
{
//adultNumericUpDown
adultNumericUpDown.Minimum = minimumValue;
adultNumericUpDown.Maximum = maximumValue;
adultNumericUpDown.FontSize = fontSizeValue;
adultNumericUpDown.AutoReverse = autoReverse;
adultNumericUpDown.SelectAllOnFocus = selectAllOnFocus;
adultNumericUpDown.SpinButtonAlignment = spinButtonAlignment;
adultNumericUpDown.TextGravity = gravity;
//infantsNumericUpDown
infantsNumericUpDown.Minimum = minimumValue;
infantsNumericUpDown.Maximum = maximumValue;
infantsNumericUpDown.FontSize = fontSizeValue;
infantsNumericUpDown.AutoReverse = autoReverse;
infantsNumericUpDown.SelectAllOnFocus = selectAllOnFocus;
infantsNumericUpDown.SpinButtonAlignment = spinButtonAlignment;
infantsNumericUpDown.TextGravity = gravity;
maxNegative = minNegative = fontsizeNegative = false;
}
public View GetPropertyWindowLayout(Context context1)
{
context = context1;
width = (context.Resources.DisplayMetrics.WidthPixels) / 2;
propertylayout = new LinearLayout(context);
propertylayout.Orientation = Orientation.Vertical;
MinimumLayout();
MaximumLayout();
FontSizeLayout();
SpinButtonLayout();
AutoReverseLayout();
SelectAllOnFocusLayout();
return propertylayout;
}
private void FontSizeLayout()
{
/***********
**FontSize**
***********/
double newNumber;
TextView fontSizeLabel = new TextView(context);
fontSizeLabel.Text = "FontSize";
fontSizeLabel.SetWidth(400);
fontSizeLabel.TextSize = 20;
//FontSize Text
fontSizeText = new EditText(context);
fontSizeText.Text = "18";
fontSizeText.TextSize = 16;
fontSizeText.InputType = Android.Text.InputTypes.ClassPhone;
// FontSizeText Changed Listener
fontSizeText.TextChanged += (object sender, Android.Text.TextChangedEventArgs e) =>
{
if (fontSizeText.Text.Length > 0)
{
String str = e.Text.ToString();
if (str.StartsWith("-") || str.EndsWith("-"))
{
str = str.Replace("-", "");
fontsizeNegative = true;
}
if (!str.Equals(""))
{
newNumber = Convert.ToDouble(str);
if (fontsizeNegative)
{
newNumber = newNumber * -1;
}
fontSizeValue = newNumber;
}
}
};
LinearLayout.LayoutParams fontsizeTextLayoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
fontsizeTextLayoutParams.SetMargins(0, 10, 0, 0);
LinearLayout.LayoutParams fontsizeLabelLayoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
fontsizeLabelLayoutParams.SetMargins(0, 10, 0, 0);
TextView textSpacing = new TextView(context);
propertylayout.AddView(textSpacing);
//FontSizeValueLayout
fontSizeLayOut = new LinearLayout(context);
fontSizeLayOut.AddView(fontSizeLabel, fontsizeLabelLayoutParams);
fontSizeLayOut.AddView(fontSizeText, fontsizeTextLayoutParams);
fontSizeLayOut.Orientation = Orientation.Horizontal;
propertylayout.AddView(fontSizeLayOut);
//FontSizeSeparate
SeparatorView fontsizeSeparate = new SeparatorView(context, width * 2);
fontsizeSeparate.LayoutParameters = new ViewGroup.LayoutParams(width * 2, 5);
LinearLayout.LayoutParams fontsizeSeparateLayoutParams = new LinearLayout.LayoutParams(width * 2, 5);
fontsizeSeparateLayoutParams.SetMargins(0, 20, 15, 0);
//propertylayout.AddView(fontsizeSeparate, fontsizeSeparateLayoutParams);
}
private void MinimumLayout()
{
/***********
**Minimum**
***********/
double newNumber;
TextView minimumLabel = new TextView(context);
minimumLabel.Text = "Minimum";
minimumLabel.SetWidth(400);
minimumLabel.TextSize = 20;
//Minimum Text
minimumText = new EditText(context);
minimumText.Text = "0";
minimumText.TextSize = 16;
minimumText.InputType = Android.Text.InputTypes.ClassPhone;
//Minimum Text Changed Listener
minimumText.TextChanged += (object sender, Android.Text.TextChangedEventArgs e) =>
{
if (minimumText.Text.Length > 0)
{
String str = e.Text.ToString();
if (str.StartsWith("-") || str.EndsWith("-"))
{
str = str.Replace("-", "");
minNegative = true;
}
if (!str.Equals(""))
{
newNumber = Convert.ToDouble(str);
if (minNegative)
{
newNumber = newNumber * -1;
}
minimumValue = newNumber;
}
}
};
LinearLayout.LayoutParams minimumTextLayoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
minimumTextLayoutParams.SetMargins(0, 10, 0, 0);
LinearLayout.LayoutParams minimumLabelLayoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
minimumLabelLayoutParams.SetMargins(0, 10, 0, 0);
//MinimumValueLayout
minimumValueLayout = new LinearLayout(context);
minimumValueLayout.AddView(minimumLabel, minimumLabelLayoutParams);
minimumValueLayout.AddView(minimumText, minimumTextLayoutParams);
minimumValueLayout.Orientation = Orientation.Horizontal;
propertylayout.AddView(minimumValueLayout);
//MinimumSeparate
SeparatorView minimumSeparate = new SeparatorView(context, width * 2);
minimumSeparate.LayoutParameters = new ViewGroup.LayoutParams(width * 2, 5);
LinearLayout.LayoutParams minimumLayoutParams = new LinearLayout.LayoutParams(width * 2, 5);
minimumLayoutParams.SetMargins(0, 20, 15, 0);
// propertylayout.AddView(minimumSeparate, minimumLayoutParams);
}
private void MaximumLayout()
{
/***********
**Maximum**
***********/
double newNumber;
TextView maximumLabel = new TextView(context);
maximumLabel.Text = "Maximum";
maximumLabel.SetWidth(400);
maximumLabel.TextSize = 20;
//MaximumText
maximumText = new EditText(context);
maximumText.Text = "100";
maximumText.TextSize = 16;
maximumText.InputType = Android.Text.InputTypes.ClassPhone;
//MaximumText TextChanged Listener
maximumText.TextChanged += (object sender, Android.Text.TextChangedEventArgs e) =>
{
if (maximumText.Text.Length > 0)
{
String str = e.Text.ToString();
if (str.StartsWith("-") || str.EndsWith("-"))
{
str = str.Replace("-", "");
maxNegative = true;
}
if (!str.Equals(""))
{
newNumber = Convert.ToDouble(str);
if (maxNegative)
{
newNumber = newNumber * -1;
}
maximumValue = newNumber;
}
}
};
LinearLayout.LayoutParams maximumTextLayoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
maximumTextLayoutParams.SetMargins(0, 10, 0, 0);
LinearLayout.LayoutParams maximumLabelLayoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
maximumLabelLayoutParams.SetMargins(0, 10, 0, 0);
TextView textSpacing = new TextView(context);
propertylayout.AddView(textSpacing);
//MaximumValueLayout
maximumValueLayout = new LinearLayout(context);
maximumValueLayout.AddView(maximumLabel, maximumLabelLayoutParams);
maximumValueLayout.AddView(maximumText, maximumTextLayoutParams);
maximumValueLayout.Orientation = Orientation.Horizontal;
propertylayout.AddView(maximumValueLayout);
//MaximumSeparate
SeparatorView maximumSeparate = new SeparatorView(context, width * 2);
maximumSeparate.LayoutParameters = new ViewGroup.LayoutParams(width * 2, 5);
LinearLayout.LayoutParams maximumLayoutParams = new LinearLayout.LayoutParams(width * 2, 5);
maximumLayoutParams.SetMargins(0, 20, 15, 0);
// propertylayout.AddView(maximumSeparate, maximumLayoutParams);
}
private void SpinButtonLayout()
{
/***********************
**SpinButtonAlignment**
***********************/
LinearLayout.LayoutParams spinButtonLayoutParams = new LinearLayout.LayoutParams(width * 2, 5);
spinButtonLayoutParams.SetMargins(0, 20, 0, 0);
//SpinButtonText
TextView spinButtonText = new TextView(context);
spinButtonText.TextSize = 20;
spinButtonText.Text = "SpinButtonAlignment";
//SpinButtonList
List<String> spinButtonList = new List<String>();
spinButtonList.Add("Right");
spinButtonList.Add("Left");
spinButtonList.Add("Both");
spinButtonDataAdapter = new ArrayAdapter<String>
(context, Android.Resource.Layout.SimpleSpinnerItem, spinButtonList);
spinButtonDataAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
//SpinButtonSpinner
spinButtonSpinner = new Spinner(context,SpinnerMode.Dialog);
spinButtonSpinner.Adapter = spinButtonDataAdapter;
TextView textSpacing = new TextView(context);
propertylayout.AddView(textSpacing);
//SpinButtonSpinner ItemSelected Listener
spinButtonSpinner.ItemSelected += (object sender, AdapterView.ItemSelectedEventArgs e) => {
String selectedItem = spinButtonDataAdapter.GetItem(e.Position);
if (selectedItem.Equals("Right"))
{
spinButtonAlignment = SpinButtonAlignment.Right;
gravity = GravityFlags.CenterVertical;
}
if (selectedItem.Equals("Left"))
{
spinButtonAlignment = SpinButtonAlignment.Left;
gravity=GravityFlags.End | GravityFlags.CenterVertical;
}
if (selectedItem.Equals("Both"))
{
spinButtonAlignment = SpinButtonAlignment.Both;
gravity = GravityFlags.Center;
}
};
propertylayout.AddView(spinButtonText);
propertylayout.AddView(spinButtonSpinner);
//SpinButtonSeparate
SeparatorView spinButtonSeparate = new SeparatorView(context, width * 2);
spinButtonSeparate.LayoutParameters = new ViewGroup.LayoutParams(width * 2, 5);
// propertylayout.AddView(spinButtonSeparate, spinButtonLayoutParams);
}
private void AutoReverseLayout()
{
/***************
**AutoReverse**
***************/
TextView autoReverseText = new TextView(context);
autoReverseText.Text = "AutoReverse";
autoReverseText.Gravity = GravityFlags.Center;
autoReverseText.TextSize = 20;
//AutoReverseCheckBox
Switch autoReverseCheckBox = new Switch(context);
autoReverseCheckBox.Checked = false;
autoReverseCheckBox.CheckedChange += (object send, CompoundButton.CheckedChangeEventArgs eve) => {
if (!eve.IsChecked)
autoReverse = false;
else
autoReverse = true;
};
LinearLayout.LayoutParams autoReverseCheckBoxLayoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
autoReverseCheckBoxLayoutParams.SetMargins(0, 10, 0, 0);
LinearLayout.LayoutParams autoReverseTextLayoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
autoReverseTextLayoutParams.SetMargins(0, 10, 0, 0);
TextView textSpacing = new TextView(context);
propertylayout.AddView(textSpacing);
//AutoReverseLayout
autoReverseLayout = new LinearLayout(context);
autoReverseLayout.AddView(autoReverseText, autoReverseTextLayoutParams);
autoReverseLayout.AddView(autoReverseCheckBox, autoReverseCheckBoxLayoutParams);
autoReverseLayout.Orientation = Orientation.Horizontal;
propertylayout.AddView(autoReverseLayout);
//AutoReverseSeparate
SeparatorView autoReverseSeparate = new SeparatorView(context, width * 2);
autoReverseSeparate.LayoutParameters = new ViewGroup.LayoutParams(width * 2, 5);
LinearLayout.LayoutParams autoReverseLayoutParams = new LinearLayout.LayoutParams(width * 2, 5);
autoReverseLayoutParams.SetMargins(0, 20, 15, 0);
// propertylayout.AddView(autoReverseSeparate, autoReverseLayoutParams);
propertylayout.SetPadding(20, 20, 20, 20);
}
private void SelectAllOnFocusLayout()
{
/***************
**SelectAllOnFocus**
***************/
TextView selectAllOnFocusText = new TextView(context);
selectAllOnFocusText.Text = "SelectAllOnFocus";
selectAllOnFocusText.Gravity = GravityFlags.Center;
selectAllOnFocusText.TextSize = 20;
//SelectAllOnFocusCheckBox
Switch selectAllOnFocusCheckBox = new Switch(context);
selectAllOnFocusCheckBox.Checked = false;
selectAllOnFocusCheckBox.CheckedChange += (object send, CompoundButton.CheckedChangeEventArgs eve) => {
if (!eve.IsChecked)
selectAllOnFocus = false;
else
selectAllOnFocus = true;
};
LinearLayout.LayoutParams selectAllOnFocusCheckBoxLayoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
selectAllOnFocusCheckBoxLayoutParams.SetMargins(0, 10, 0, 0);
LinearLayout.LayoutParams selectAllOnFocusTextLayoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
selectAllOnFocusTextLayoutParams.SetMargins(0, 10, 0, 0);
TextView textSpacing = new TextView(context);
propertylayout.AddView(textSpacing);
//SelectAllOnFocusLayout
selectAllOnFocusLayout = new LinearLayout(context);
selectAllOnFocusLayout.AddView(selectAllOnFocusText, selectAllOnFocusTextLayoutParams);
selectAllOnFocusLayout.AddView(selectAllOnFocusCheckBox, selectAllOnFocusCheckBoxLayoutParams);
selectAllOnFocusLayout.Orientation = Orientation.Horizontal;
propertylayout.AddView(selectAllOnFocusLayout);
propertylayout.SetPadding(20, 20, 20, 20);
}
public static void hideSoftKeyboard(Activity activity)
{
InputMethodManager inputMethodManager = (InputMethodManager)activity.GetSystemService(Activity.InputMethodService);
inputMethodManager.HideSoftInputFromWindow(activity.CurrentFocus.WindowToken, 0);
}
private int getDimensionPixelSize(Context con, int id)
{
return con.Resources.GetDimensionPixelSize(id);
}
}
}