Sign in Join
Wiki copytemporary

Android4Pascal: what changed

Revision 1 to 2. 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
4141` svn co `[`https://p-tools.svn.sourceforge.net/svnroot/p-tools/turbochessclock4android`](https://p-tools.svn.sourceforge.net/svnroot/p-tools/turbochessclock4android)` turbochessclock4android`
4242
43-<figure>
44-<img src="Simple_Android_app.png" title="Simple_Android_app.png" />
45-<figcaption>Simple_Android_app.png</figcaption>
46-</figure>
43+![](/wiki-images/7/7c/Simple_Android_app.png)
4744
4845Here is the Pascal code from this example:
4946
5047``` pascal
5148{
5249 A simple Chess Clock application
5350
5451 Author: Felipe Monteiro de Carvalho - 2011
5552 License: Public Domain
5653}
5754program turbochessclock4android;
5855
5956{$mode objfpc}{$H+}
6057
6158uses
6259 Classes, SysUtils, androidpipescomm, androidview, javalang,
6360 androidapp, androidtimer, androidutil;
6461
6562type
6663 TEventHandler = class
6764 public
6865 procedure HandleOnTimer(ASender: TObject);
6966 procedure buttonStartClickCallback(v: TView);
7067 procedure buttonMoveClickCallback(v: TView);
7168 end;
7269
7370var
7471 layout: TLinearLayout;
7572 params: TLayoutParams;
7673 tv, black_label, white_label: TTextView;
7774 scroller: TScrollView;
7875 btn_move, btn_start: TButton;
7976 tp: TTimePicker;
8077 WhiteTimeCount: Integer = 0;
8178 BlackTimeCount: Integer = 0;
8279 MyTimer: TAndroidTimer;
8380 MyEventHandler: TEventHandler;
8481 IsWhitePlayerMove: Boolean = True;
8582
8683procedure TEventHandler.buttonStartClickCallback(v: TView);
8784begin
8885 black_label.setVisibility(VISIBLE);
8986 white_label.setVisibility(VISIBLE);
9087 btn_move.setVisibility(VISIBLE);
9188 //
9289 WhiteTimeCount := tp.getCurrentHour() * 60 * 60 + tp.getCurrentMinute() * 60;
9390 BlackTimeCount := WhiteTimeCount;
9491 //
9592 MyTimer.removeCallbacks();
9693 MyTimer.postDelayed(100);
9794end;
9895
9996procedure TEventHandler.buttonMoveClickCallback(v: TView);
10097begin
10198 IsWhitePlayerMove := not IsWhitePlayerMove;
10299end;
103100
104101procedure TEventHandler.HandleOnTimer(ASender: TObject);
105102var
106103 lSeconds, lMinutes, lHours: Integer;
107104begin
108105 if IsWhitePlayerMove then
109106 begin
110107 lSeconds := WhiteTimeCount mod 60;
111108 lMinutes := (WhiteTimeCount mod (60 * 60)) div 60;
112109 lHours := WhiteTimeCount div (60 * 60);
113110 white_label.setText(Format('White %d:%d:%d', [lHours, lMinutes, lSeconds]));
114111 //
115112 if WhiteTimeCount = 0 then Exit;
116113 Dec(WhiteTimeCount);
117114 end
118115 else
119116 begin
120117 lSeconds := BlackTimeCount mod 60;
121118 lMinutes := (BlackTimeCount mod (60 * 60)) div 60;
122119 lHours := BlackTimeCount div (60 * 60);
123120 black_label.setText(Format('Black %d:%d:%d', [lHours, lMinutes, lSeconds]));
124121 //
125122 if BlackTimeCount = 0 then Exit;
126123 Dec(BlackTimeCount);
127124 end;
128125 //
129126 MyTimer.removeCallbacks();
130127 // Note that this has a low precision, but it enough for this simple app
131128 // A more precise app would keep another time count instead of just using Dec
132129 // at each timer call
133130 MyTimer.postDelayed(1000);
134131end;
135132
136133begin
137134 // Here add any initialization.
138135 // Any initialization code will be run inside Activity.onCreate,
139136 // so keep it as short as possible!
140137 // It should mostly contain GUI initialization
141138 // User interface
142139 MyEventHandler := TEventHandler.Create;
143140
144141 // Prepares the UI of the program
145142 layout := TLinearLayout.Create;
146143 params := TLayoutParams.Create(androidview.FILL_PARENT, androidview.FILL_PARENT);
147144 layout.setLayoutParams(params);
148145 params.Free;
149146 layout.setOrientation(androidview.VERTICAL);
150147
151148 // Game UI
152149
153150 black_label := TTextView.Create;
154151 black_label.setText('Black time:');
155152 black_label.setVisibility(GONE);
156153 black_label.setTextSize(COMPLEX_UNIT_PX, 40);
157154 layout.addView(black_label);
158155
159156 white_label := TTextView.Create;
160157 white_label.setText('White time:');
161158 white_label.setVisibility(GONE);
162159 white_label.setTextSize(COMPLEX_UNIT_PX, 40);
163160 layout.addView(white_label);
164161
165162 btn_move := TButton.Create;
166163 btn_move.setText('Move finished!');
167164 btn_move.setOnClickListener(@MyEventHandler.buttonMoveClickCallback);
168165 btn_move.setVisibility(GONE);
169166 layout.addView(btn_move);
170167
171168 // Config UI
172169
173170 tv := TTextView.Create;
174171 tv.setText('Please select how much to give to the players and press "Start Game":');
175172 layout.addView(tv);
176173
177174 tp := TTimePicker.Create;
178175 tp.setIs24HourView(True);
179176 tp.setCurrentHour(0);
180177 tp.setCurrentMinute(30);
181178 layout.addView(tp);
182179
183180 btn_start := TButton.Create;
184181 btn_start.setText('Start game!');
185182 btn_start.setOnClickListener(@MyEventHandler.buttonStartClickCallback);
186183 layout.addView(btn_start);
187184
188185 // And also allow the user to scroll the UI if it is larger then the screen width
189186 // Scrolling takes place only horizontally
190187
191188 scroller := TScrollView.Create;
192189 scroller.addView(layout);
193190
194191 Activity.setContentView(scroller);
195192
196193 MyTimer := TAndroidTimer.Create;
197194 MyTimer.OnTimer := @MyEventHandler.HandleOnTimer;
198195
199196 // Now tell Java that the initialization has finished
200197 vAndroidPipesComm.onCreateFinished();
201198 // Here you can add any other initialization,
202199 // specially non-GUI code
203200
204201 // Now we block our execution waiting for callbacks from Java
205202 vAndroidPipesComm.MessageLoop();
206203end.
207204```
208205
209206##### Compiling the example project in Linux
210207
2112081\> Get a working arm-linux cross-compiler which generates ARMv5 eabi with softfloat (as many phones like HTC Wildfire have no FPU)
212209
213210To do this one can download an unofficial build of FPC 2.5.1 from here:
214211
215212<http://sourceforge.net/projects/p-tools/files/Free%20Pascal%20for%20ARM/> Compiled for ARMv5 eabi with softfloat.
216213
217214Or an older FPC 2.4.2
218215
219216<http://members.yline.com/~tom_at_work/fpc-2.4.2.UNOFFICIAL.arm-linux.tar> Compiled for ARMv5 eabi with softfloat.
220217
221218Or build your own. There are instructions here: [[Setup Cross Compile For ARM]]
222219
2232202\> 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]]
224221
2252223\> Install ant, for example in Mandriva Linux:
226223
227224` urpmi ant`
228225
2292264\> Open the project turbochessclock/turbochessclock4android.lpi in Lazarus and build it
230227
2312285\> Build the APK file in debug mode
232229
233230` ant debug`
234231
2352326\> 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]]
236233
2372347\> Install the APK file in your phone via ADB or whatever other method you prefer:
238235
239236` ../android-sdk-linux_x86/tools/adb install android/bin/TurboChessClock4Android-debug.apk`
240237
241238If the package is already installed you need need to do this instead:
242239
243240` ../android-sdk-linux_x86/tools/adb uninstall com.pascal.turbochessclock`\
244241` ../android-sdk-linux_x86/tools/adb install android/bin/TurboChessClock4Android-debug.apk`
245242
246243### Version 0.1
247244
248245Released on January 2011
249246
250247This version was distributed as an example project which can be obtained with the following svn command:
251248
252249svn co <https://p-tools.svn.sourceforge.net/svnroot/PascalNotes4Android/> -r 230 PascalNotes4Android
253250
254251## See also
255252
256253- [[Android Interface/Native Android GUI]]
257254- [[Android Programming]]
258255
259256---
260257*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).*
261258Category: Android

Back to history