forked from syncfusion/xamarin-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckBox_Mobile.cs
240 lines (205 loc) · 8.99 KB
/
CheckBox_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
#region Copyright Syncfusion Inc. 2001-2019.
// Copyright Syncfusion Inc. 2001-2019. 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.
#endregion
using Android.App;
using Android.Widget;
using Android.OS;
using Syncfusion.Android.Buttons;
using Android.Graphics;
using Android.Views;
using Android.Content;
using Android.Util;
using System;
namespace SampleBrowser
{
public class CheckBox_Mobile
{
private bool skip = false;
SfCheckBox selectAllBox, grilledBox, tikkaBox, beefBox, sausaga;
Button button;
AlertDialog.Builder resultsDialog;
public CheckBox_Mobile()
{
}
public View GetSampleContent(Context con)
{
LinearLayout linear = new LinearLayout(con);
linear.Orientation = Orientation.Vertical;
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent, (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 3, con.Resources.DisplayMetrics));
int margin;
if (IsTabletDevice(con))
margin = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 15, con.Resources.DisplayMetrics);
else
margin = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 9.5f, con.Resources.DisplayMetrics);
linearLayoutParams.SetMargins(margin, margin, margin, margin);
ImageView imageView = new ImageView(con);
imageView.SetScaleType(ImageView.ScaleType.FitStart);
imageView.SetAdjustViewBounds(true);
imageView.SetImageResource(Resource.Drawable.Pizzaimage);
linear.AddView(imageView);
int padding = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 7, con.Resources.DisplayMetrics);
LinearLayout frameParent = new LinearLayout(con);
frameParent.SetBackgroundColor(Color.White);
frameParent.Orientation = Orientation.Vertical;
frameParent.LayoutParameters = linearLayoutParams;
LinearLayout frame = new LinearLayout(con);
frame.Orientation = Orientation.Vertical;
int currentapiVersion = (int)Build.VERSION.SdkInt;
if (currentapiVersion > 21)
frame.Background = con.Resources.GetDrawable(Resource.Drawable.shadow, con.Theme);
TextView headLabel = new TextView(con);
headLabel.SetPadding(padding, headLabel.PaddingTop, headLabel.PaddingRight, headLabel.PaddingBottom);
headLabel.TextSize = 18;
headLabel.SetTextColor(Color.ParseColor("#FF007DE6"));
headLabel.Text = "Add Extra Toppings";
frame.AddView(headLabel);
#region Items Layout
selectAllBox = new SfCheckBox(con);
selectAllBox.Text = "Select All";
selectAllBox.TextSize = 15;
selectAllBox.SetTextColor(Color.ParseColor("#FF000000"));
selectAllBox.StateChanged += SelectAll1_StateChanged;
selectAllBox.LayoutParameters = linearLayoutParams;
frame.AddView(selectAllBox);
grilledBox = new SfCheckBox(con);
grilledBox.Text = "Grilled Chicken";
grilledBox.TextSize = 15;
grilledBox.SetTextColor(Color.ParseColor("#FF000000"));
grilledBox.StateChanged += NonvegToppingsChanged;
grilledBox.LayoutParameters = linearLayoutParams;
frame.AddView(grilledBox);
tikkaBox = new SfCheckBox(con);
tikkaBox.Text = "Chicken Tikka";
tikkaBox.TextSize = 15;
tikkaBox.SetTextColor(Color.ParseColor("#FF000000"));
tikkaBox.StateChanged += NonvegToppingsChanged;
tikkaBox.LayoutParameters = linearLayoutParams;
frame.AddView(tikkaBox);
sausaga = new SfCheckBox(con);
sausaga.Text = "Chicken Sausage";
sausaga.TextSize = 15;
sausaga.SetTextColor(Color.ParseColor("#FF000000"));
sausaga.StateChanged += NonvegToppingsChanged;
sausaga.LayoutParameters = linearLayoutParams;
frame.AddView(sausaga);
beefBox = new SfCheckBox(con);
beefBox.Text = "Beef";
beefBox.TextSize = 15;
beefBox.SetTextColor(Color.ParseColor("#FF000000"));
beefBox.StateChanged += NonvegToppingsChanged;
beefBox.LayoutParameters = linearLayoutParams;
frame.AddView(beefBox);
frameParent.AddView(frame);
linear.AddView(frameParent);
#endregion
button = new Button(con);
button.SetWidth(ActionBar.LayoutParams.MatchParent);
button.SetHeight(43);
button.TextSize = 21;
button.Text = "Order Now";
button.SetBackgroundColor(Color.ParseColor("#FF007DE6"));
button.SetTextColor(Color.ParseColor("#73FFFFFF"));
button.Click += SearchButton_Click;
button.Enabled = false;
linear.AddView(button);
resultsDialog = new AlertDialog.Builder(con);
resultsDialog.SetPositiveButton("OK", (object sender, DialogClickEventArgs e) =>
{
});
resultsDialog.SetCancelable(true);
return linear;
}
public static bool IsTabletDevice(Android.Content.Context context)
{
try
{
DisplayMetrics displayMetrics = context.Resources.DisplayMetrics;
float screenWidth = displayMetrics.WidthPixels / displayMetrics.Xdpi;
float screenHeight = displayMetrics.HeightPixels / displayMetrics.Ydpi;
double size = Java.Lang.Math.Sqrt(Math.Pow(screenWidth, 2) + Math.Pow(screenHeight, 2));
return size >= 6;
}
catch
{
return false;
}
}
private void NonVegBox_CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
{
if (e.IsChecked)
{
grilledBox.Visibility = tikkaBox.Visibility = sausaga.Visibility = beefBox.Visibility = ViewStates.Visible;
}
else
{
grilledBox.Visibility = tikkaBox.Visibility = sausaga.Visibility = beefBox.Visibility = ViewStates.Gone;
}
if (!e.IsChecked)
{
selectAllBox.Checked = false;
grilledBox.Checked = tikkaBox.Checked = sausaga.Checked = beefBox.Checked = false;
}
}
private void SearchButton_Click(object sender, EventArgs e)
{
bool? temp = ValidateNonVegToopings();
if (!temp.HasValue || (temp.HasValue && temp.Value))
{
resultsDialog.SetMessage("Your order has been placed successfully !");
selectAllBox.Checked = false;
}
resultsDialog.Create().Show();
}
private bool? ValidateNonVegToopings()
{
if (grilledBox.Checked.Value && tikkaBox.Checked.Value && sausaga.Checked.Value && beefBox.Checked.Value)
return true;
else if (!grilledBox.Checked.Value && !tikkaBox.Checked.Value && !sausaga.Checked.Value && !beefBox.Checked.Value)
return false;
else
return null;
}
private void NonvegToppingsChanged(object sender, Syncfusion.Android.Buttons.StateChangedEventArgs eventArgs)
{
if (!skip)
{
skip = true;
selectAllBox.Checked = ValidateNonVegToopings();
if (!selectAllBox.Checked.HasValue || (selectAllBox.Checked.HasValue && selectAllBox.Checked.Value))
{
button.Enabled = true;
button.SetTextColor(Color.ParseColor("#FFFFFF"));
}
else
{
button.Enabled = false;
button.SetTextColor(Color.ParseColor("#73FFFFFF"));
}
skip = false;
}
}
private void SelectAll1_StateChanged(object sender, Syncfusion.Android.Buttons.StateChangedEventArgs eventArgs)
{
if (!skip)
{
skip = true;
grilledBox.Checked = tikkaBox.Checked = sausaga.Checked = beefBox.Checked = eventArgs.IsChecked.Value;
button.Enabled = eventArgs.IsChecked.Value;
if (button.Enabled)
{
button.SetTextColor(Color.ParseColor("#FFFFFF"));
}
else
{
button.SetTextColor(Color.ParseColor("#73FFFFFF"));
}
skip = false;
}
}
}
}