app: update UI example language and android style
[nit.git] / lib / app / examples / ui_example.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 # User interface example using `app::ui`
16 module ui_example is
17 app_name "app.nit UI"
18 app_namespace "org.nitlanguage.ui_example"
19 android_api_min 21
20 android_api_target 21
21 android_manifest_activity "android:theme=\"@android:style/Theme.Material\""
22 end
23
24 import app::ui
25 import app::data_store
26 import android::aware # for android_api_target
27
28 # Window showing off some the available controls
29 class UiExampleWindow
30 super Window
31
32 # Root layout
33 var layout = new ListLayout(parent=self)
34
35 # Some label
36 var some_label = new Label(parent=layout, text="Sample Window using a ListLayout.")
37
38 # A checkbox
39 var checkbox = new CheckBox(parent=layout, text="A CheckBox")
40
41 # Horizontal organization
42 var h_layout = new HorizontalLayout(parent=layout)
43
44 # Description for the `user_input`
45 var user_input_label = new Label(parent=h_layout, text="Input some text:", align=0.5)
46
47 # Field for the user to enter data
48 var user_input = new TextInput(parent=h_layout, text="Default text")
49
50 # Button to open a new window with a ListLayout
51 var button_window = new Button(parent=layout, text="Open a new window")
52
53 # URL to open
54 var example_url = "http://nitlanguage.org/"
55
56 # Button to open the browser
57 var button_browser = new Button(parent=layout, text="Open {example_url}")
58
59 redef fun on_event(event)
60 do
61 if event isa ButtonPressEvent then
62 if event.sender == button_browser then
63 example_url.open_in_browser
64 else if event.sender == button_window then
65 app.push_window new SecondWindow
66 end
67 else if event isa ToggleEvent then
68 if event.sender == checkbox then checkbox.text = if checkbox.is_checked then "Checked" else "Unchecked"
69 end
70 end
71 end
72
73 # Another window with a small `VerticalLayout`
74 class SecondWindow
75 super Window
76
77 # Root layout
78 var layout = new VerticalLayout(parent=self)
79
80 # Some label
81 var a_label = new Label(parent=layout, text="This window uses a VerticalLayout.")
82
83 # Another label
84 var another_label = new Label(parent=layout, text="Close it by tapping the back button.")
85 end
86
87 redef class App
88 redef fun on_create
89 do
90 # Create the main window
91 push_window new UiExampleWindow
92 super
93 end
94 end