8bdbdc8b0811b88efb8ff4f40ac0520f2dce5975
[nit.git] / examples / calculator / src / android_calculator.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Aesthetic adaptations for Android
16 module android_calculator
17
18 import calculator
19 import android
20
21 redef class Button
22 init do set_android_style(native, app.native_activity,
23 (text or else "?").is_int,
24 ["+","-","×","C","÷","=","."].has(text))
25
26 # Set color and text style
27 private fun set_android_style(java_button: NativeButton, activity: NativeActivity,
28 is_number: Bool, is_basic_op: Bool)
29 in "Java" `{
30 // Set color
31 int back_color_id = 0;
32 if (is_number)
33 back_color_id = R.color.pad_numeric_background_color;
34 else if (is_basic_op)
35 back_color_id = R.color.pad_operator_background_color;
36 else {
37 back_color_id = R.color.pad_advanced_background_color;
38
39 int text_color = activity.getResources().getColor(R.color.pad_button_advanced_text_color);
40 java_button.setTextColor(text_color);
41 }
42 java_button.setBackgroundResource(back_color_id);
43
44 // Center label, use lowercase and make text bigger
45 java_button.setGravity(android.view.Gravity.CENTER);
46 java_button.setAllCaps(false);
47 java_button.setTextSize(android.util.TypedValue.COMPLEX_UNIT_FRACTION, 100.0f);
48 `}
49 end
50
51 redef class TextInput
52 init do set_android_style(native, app.native_activity)
53
54 # Set text style and hide cursor
55 private fun set_android_style(java_edit_text: NativeEditText, activity: NativeActivity)
56 in "Java" `{
57 java_edit_text.setBackgroundResource(R.color.display_background_color);
58 java_edit_text.setTextColor(
59 activity.getResources().getColor(R.color.display_formula_text_color));
60 java_edit_text.setTextSize(android.util.TypedValue.COMPLEX_UNIT_FRACTION, 120.0f);
61 java_edit_text.setCursorVisible(false);
62 java_edit_text.setGravity(android.view.Gravity.CENTER_VERTICAL | android.view.Gravity.END);
63 `}
64 end