Sign in Join
Wiki copytemporary

Android4Pascal: what changed

Revision 3 to 4. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11*(Android only)*
22
33Android4Pascal are the bindings which connect Pascal applications running as executables in Android with the Android Java APIs.
44
55## Download
66
77### Latest development version
88
99- svn co <https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/bindings/android-sdk> android-sdk
1010- svn co <https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/examples/androidlcl> androidlcl
1111
1212Changelog:
1313
1414- Added a tool to automatically generate the bindings
1515- Added many new APIs (don't remember all of them)
1616
1717### Version 0.3
1818
1919Released June 2011
2020
2121svn co <https://p-tools.svn.sourceforge.net/svnroot/p-tools/systeminfo4android/> systeminfo4android
2222
2323Changelog:
2424
2525- Added support for reading android.util.DisplayMetrics, Activity.getWindowManager, WindowManager.getDefaultDisplay, Display.getMetrics
2626
2727### Version 0.2
2828
2929Released on March 2011
3030
3131This version was distributed as an example project which can be obtained with the following svn command:
3232
3333svn co <https://p-tools.svn.sourceforge.net/svnroot/p-tools/turbochessclock4android/> -r 230 turbochessclock4android
3434
3535#### Android API Hello World in Pascal
3636
3737Here is an example Pascal application written for Android. It shows how to create controls, receive callback events and how to use the timer.
3838
3939The full directory structure can be download with this svn command:
4040
41-```
41+```text
4242 svn co https://p-tools.svn.sourceforge.net/svnroot/p-tools/turbochessclock4android turbochessclock4android
4343```
4444
4545![](/wiki-images/7/7c/Simple_Android_app.png)
4646
4747Here is the Pascal code from this example:
4848
4949``` pascal
5050{
5151 A simple Chess Clock application
5252
5353 Author: Felipe Monteiro de Carvalho - 2011
5454 License: Public Domain
5555}
5656program turbochessclock4android;
5757
5858{$mode objfpc}{$H+}
5959
6060uses
6161 Classes, SysUtils, androidpipescomm, androidview, javalang,
6262 androidapp, androidtimer, androidutil;
6363
6464type
6565 TEventHandler = class
6666 public
6767 procedure HandleOnTimer(ASender: TObject);
6868 procedure buttonStartClickCallback(v: TView);
6969 procedure buttonMoveClickCallback(v: TView);
7070 end;
7171
7272var
7373 layout: TLinearLayout;
7474 params: TLayoutParams;
7575 tv, black_label, white_label: TTextView;
7676 scroller: TScrollView;
7777 btn_move, btn_start: TButton;
7878 tp: TTimePicker;
7979 WhiteTimeCount: Integer = 0;
8080 BlackTimeCount: Integer = 0;
8181 MyTimer: TAndroidTimer;
8282 MyEventHandler: TEventHandler;
8383 IsWhitePlayerMove: Boolean = True;
8484
8585procedure TEventHandler.buttonStartClickCallback(v: TView);
8686begin
8787 black_label.setVisibility(VISIBLE);
8888 white_label.setVisibility(VISIBLE);
8989 btn_move.setVisibility(VISIBLE);
9090 //
9191 WhiteTimeCount := tp.getCurrentHour() * 60 * 60 + tp.getCurrentMinute() * 60;
9292 BlackTimeCount := WhiteTimeCount;
9393 //
9494 MyTimer.removeCallbacks();
9595 MyTimer.postDelayed(100);
9696end;
9797
9898procedure TEventHandler.buttonMoveClickCallback(v: TView);
9999begin
100100 IsWhitePlayerMove := not IsWhitePlayerMove;
101101end;
102102
103103procedure TEventHandler.HandleOnTimer(ASender: TObject);
104104var
105105 lSeconds, lMinutes, lHours: Integer;
106106begin
107107 if IsWhitePlayerMove then
108108 begin
109109 lSeconds := WhiteTimeCount mod 60;
110110 lMinutes := (WhiteTimeCount mod (60 * 60)) div 60;
111111 lHours := WhiteTimeCount div (60 * 60);
112112 white_label.setText(Format('White %d:%d:%d', [lHours, lMinutes, lSeconds]));
113113 //
114114 if WhiteTimeCount = 0 then Exit;
115115 Dec(WhiteTimeCount);
116116 end
117117 else
118118 begin
119119 lSeconds := BlackTimeCount mod 60;
120120 lMinutes := (BlackTimeCount mod (60 * 60)) div 60;
121121 lHours := BlackTimeCount div (60 * 60);
122122 black_label.setText(Format('Black %d:%d:%d', [lHours, lMinutes, lSeconds]));
123123 //
124124 if BlackTimeCount = 0 then Exit;
125125 Dec(BlackTimeCount);
126126 end;
127127 //
128128 MyTimer.removeCallbacks();
129129 // Note that this has a low precision, but it enough for this simple app
130130 // A more precise app would keep another time count instead of just using Dec
131131 // at each timer call
132132 MyTimer.postDelayed(1000);
133133end;
134134
135135begin
136136 // Here add any initialization.
137137 // Any initialization code will be run inside Activity.onCreate,
138138 // so keep it as short as possible!
139139 // It should mostly contain GUI initialization
140140 // User interface
141141 MyEventHandler := TEventHandler.Create;
142142
143143 // Prepares the UI of the program
144144 layout := TLinearLayout.Create;
145145 params := TLayoutParams.Create(androidview.FILL_PARENT, androidview.FILL_PARENT);
146146 layout.setLayoutParams(params);
147147 params.Free;
148148 layout.setOrientation(androidview.VERTICAL);
149149
150150 // Game UI
151151
152152 black_label := TTextView.Create;
153153 black_label.setText('Black time:');
154154 black_label.setVisibility(GONE);
155155 black_label.setTextSize(COMPLEX_UNIT_PX, 40);
156156 layout.addView(black_label);
157157
158158 white_label := TTextView.Create;
159159 white_label.setText('White time:');
160160 white_label.setVisibility(GONE);
161161 white_label.setTextSize(COMPLEX_UNIT_PX, 40);
162162 layout.addView(white_label);
163163
164164 btn_move := TButton.Create;
165165 btn_move.setText('Move finished!');
166166 btn_move.setOnClickListener(@MyEventHandler.buttonMoveClickCallback);
167167 btn_move.setVisibility(GONE);
168168 layout.addView(btn_move);
169169
170170 // Config UI
171171
172172 tv := TTextView.Create;
173173 tv.setText('Please select how much to give to the players and press "Start Game":');
174174 layout.addView(tv);
175175
176176 tp := TTimePicker.Create;
177177 tp.setIs24HourView(True);
178178 tp.setCurrentHour(0);
179179 tp.setCurrentMinute(30);
180180 layout.addView(tp);
181181
182182 btn_start := TButton.Create;
183183 btn_start.setText('Start game!');
184184 btn_start.setOnClickListener(@MyEventHandler.buttonStartClickCallback);
185185 layout.addView(btn_start);
186186
187187 // And also allow the user to scroll the UI if it is larger then the screen width
188188 // Scrolling takes place only horizontally
189189
190190 scroller := TScrollView.Create;
191191 scroller.addView(layout);
192192
193193 Activity.setContentView(scroller);
194194
195195 MyTimer := TAndroidTimer.Create;
196196 MyTimer.OnTimer := @MyEventHandler.HandleOnTimer;
197197
198198 // Now tell Java that the initialization has finished
199199 vAndroidPipesComm.onCreateFinished();
200200 // Here you can add any other initialization,
201201 // specially non-GUI code
202202
203203 // Now we block our execution waiting for callbacks from Java
204204 vAndroidPipesComm.MessageLoop();
205205end.
206206```
207207
208208##### Compiling the example project in Linux
209209
2102101\> Get a working arm-linux cross-compiler which generates ARMv5 eabi with softfloat (as many phones like HTC Wildfire have no FPU)
211211
212212To do this one can download an unofficial build of FPC 2.5.1 from here:
213213
214214<http://sourceforge.net/projects/p-tools/files/Free%20Pascal%20for%20ARM/> Compiled for ARMv5 eabi with softfloat.
215215
216216Or an older FPC 2.4.2
217217
218218<http://members.yline.com/~tom_at_work/fpc-2.4.2.UNOFFICIAL.arm-linux.tar> Compiled for ARMv5 eabi with softfloat.
219219
220220Or build your own. There are instructions here: [[Setup Cross Compile For ARM]]
221221
2222222\> Install the Android SDK. Instructions here: [[Android Interface/Using the Android SDK, Emulator and Phones|Android_Interface/Using_the_Android_SDK%2C_Emulator_and_Phones#Using_the_Android_SDK]]
223223
2242243\> Install ant, for example in Mandriva Linux:
225225
226-```
226+```text
227227 urpmi ant
228228```
229229
2302304\> Open the project turbochessclock/turbochessclock4android.lpi in Lazarus and build it
231231
2322325\> Build the APK file in debug mode
233233
234-```
234+```text
235235 ant debug
236236```
237237
2382386\> Connect your phone and make sure you can connect to it via ADB. More info here: [[Android Interface/Using the Android SDK, Emulator and Phones|Android_Interface/Using_the_Android_SDK%2C_Emulator_and_Phones#Recognition_of_devices_under_Linux]]
239239
2402407\> Install the APK file in your phone via ADB or whatever other method you prefer:
241241
242-```
242+```text
243243 ../android-sdk-linux_x86/tools/adb install android/bin/TurboChessClock4Android-debug.apk
244244```
245245
246246If the package is already installed you need need to do this instead:
247247
248-```
248+```text
249249 ../android-sdk-linux_x86/tools/adb uninstall com.pascal.turbochessclock
250250 ../android-sdk-linux_x86/tools/adb install android/bin/TurboChessClock4Android-debug.apk
251251```
252252
253253### Version 0.1
254254
255255Released on January 2011
256256
257257This version was distributed as an example project which can be obtained with the following svn command:
258258
259259svn co <https://p-tools.svn.sourceforge.net/svnroot/PascalNotes4Android/> -r 230 PascalNotes4Android
260260
261261## See also
262262
263263- [[Android Interface/Native Android GUI]]
264264- [[Android Programming]]
265265
266266---
267267*From the Free Pascal wiki, page "Android4Pascal", as it stood on 2020-02-09 after a change by GAN, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
268268Category: Android

Back to history