Sign in Join
Wiki copytemporary

Android4Pascal: what changed

Revision 2 to 3. 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-` svn co `[`https://p-tools.svn.sourceforge.net/svnroot/p-tools/turbochessclock4android`](https://p-tools.svn.sourceforge.net/svnroot/p-tools/turbochessclock4android)` turbochessclock4android`
41+```
42+ svn co https://p-tools.svn.sourceforge.net/svnroot/p-tools/turbochessclock4android turbochessclock4android
43+```
4244
4345![](/wiki-images/7/7c/Simple_Android_app.png)
4446
4547Here is the Pascal code from this example:
4648
4749``` pascal
4850{
4951 A simple Chess Clock application
5052
5153 Author: Felipe Monteiro de Carvalho - 2011
5254 License: Public Domain
5355}
5456program turbochessclock4android;
5557
5658{$mode objfpc}{$H+}
5759
5860uses
5961 Classes, SysUtils, androidpipescomm, androidview, javalang,
6062 androidapp, androidtimer, androidutil;
6163
6264type
6365 TEventHandler = class
6466 public
6567 procedure HandleOnTimer(ASender: TObject);
6668 procedure buttonStartClickCallback(v: TView);
6769 procedure buttonMoveClickCallback(v: TView);
6870 end;
6971
7072var
7173 layout: TLinearLayout;
7274 params: TLayoutParams;
7375 tv, black_label, white_label: TTextView;
7476 scroller: TScrollView;
7577 btn_move, btn_start: TButton;
7678 tp: TTimePicker;
7779 WhiteTimeCount: Integer = 0;
7880 BlackTimeCount: Integer = 0;
7981 MyTimer: TAndroidTimer;
8082 MyEventHandler: TEventHandler;
8183 IsWhitePlayerMove: Boolean = True;
8284
8385procedure TEventHandler.buttonStartClickCallback(v: TView);
8486begin
8587 black_label.setVisibility(VISIBLE);
8688 white_label.setVisibility(VISIBLE);
8789 btn_move.setVisibility(VISIBLE);
8890 //
8991 WhiteTimeCount := tp.getCurrentHour() * 60 * 60 + tp.getCurrentMinute() * 60;
9092 BlackTimeCount := WhiteTimeCount;
9193 //
9294 MyTimer.removeCallbacks();
9395 MyTimer.postDelayed(100);
9496end;
9597
9698procedure TEventHandler.buttonMoveClickCallback(v: TView);
9799begin
98100 IsWhitePlayerMove := not IsWhitePlayerMove;
99101end;
100102
101103procedure TEventHandler.HandleOnTimer(ASender: TObject);
102104var
103105 lSeconds, lMinutes, lHours: Integer;
104106begin
105107 if IsWhitePlayerMove then
106108 begin
107109 lSeconds := WhiteTimeCount mod 60;
108110 lMinutes := (WhiteTimeCount mod (60 * 60)) div 60;
109111 lHours := WhiteTimeCount div (60 * 60);
110112 white_label.setText(Format('White %d:%d:%d', [lHours, lMinutes, lSeconds]));
111113 //
112114 if WhiteTimeCount = 0 then Exit;
113115 Dec(WhiteTimeCount);
114116 end
115117 else
116118 begin
117119 lSeconds := BlackTimeCount mod 60;
118120 lMinutes := (BlackTimeCount mod (60 * 60)) div 60;
119121 lHours := BlackTimeCount div (60 * 60);
120122 black_label.setText(Format('Black %d:%d:%d', [lHours, lMinutes, lSeconds]));
121123 //
122124 if BlackTimeCount = 0 then Exit;
123125 Dec(BlackTimeCount);
124126 end;
125127 //
126128 MyTimer.removeCallbacks();
127129 // Note that this has a low precision, but it enough for this simple app
128130 // A more precise app would keep another time count instead of just using Dec
129131 // at each timer call
130132 MyTimer.postDelayed(1000);
131133end;
132134
133135begin
134136 // Here add any initialization.
135137 // Any initialization code will be run inside Activity.onCreate,
136138 // so keep it as short as possible!
137139 // It should mostly contain GUI initialization
138140 // User interface
139141 MyEventHandler := TEventHandler.Create;
140142
141143 // Prepares the UI of the program
142144 layout := TLinearLayout.Create;
143145 params := TLayoutParams.Create(androidview.FILL_PARENT, androidview.FILL_PARENT);
144146 layout.setLayoutParams(params);
145147 params.Free;
146148 layout.setOrientation(androidview.VERTICAL);
147149
148150 // Game UI
149151
150152 black_label := TTextView.Create;
151153 black_label.setText('Black time:');
152154 black_label.setVisibility(GONE);
153155 black_label.setTextSize(COMPLEX_UNIT_PX, 40);
154156 layout.addView(black_label);
155157
156158 white_label := TTextView.Create;
157159 white_label.setText('White time:');
158160 white_label.setVisibility(GONE);
159161 white_label.setTextSize(COMPLEX_UNIT_PX, 40);
160162 layout.addView(white_label);
161163
162164 btn_move := TButton.Create;
163165 btn_move.setText('Move finished!');
164166 btn_move.setOnClickListener(@MyEventHandler.buttonMoveClickCallback);
165167 btn_move.setVisibility(GONE);
166168 layout.addView(btn_move);
167169
168170 // Config UI
169171
170172 tv := TTextView.Create;
171173 tv.setText('Please select how much to give to the players and press "Start Game":');
172174 layout.addView(tv);
173175
174176 tp := TTimePicker.Create;
175177 tp.setIs24HourView(True);
176178 tp.setCurrentHour(0);
177179 tp.setCurrentMinute(30);
178180 layout.addView(tp);
179181
180182 btn_start := TButton.Create;
181183 btn_start.setText('Start game!');
182184 btn_start.setOnClickListener(@MyEventHandler.buttonStartClickCallback);
183185 layout.addView(btn_start);
184186
185187 // And also allow the user to scroll the UI if it is larger then the screen width
186188 // Scrolling takes place only horizontally
187189
188190 scroller := TScrollView.Create;
189191 scroller.addView(layout);
190192
191193 Activity.setContentView(scroller);
192194
193195 MyTimer := TAndroidTimer.Create;
194196 MyTimer.OnTimer := @MyEventHandler.HandleOnTimer;
195197
196198 // Now tell Java that the initialization has finished
197199 vAndroidPipesComm.onCreateFinished();
198200 // Here you can add any other initialization,
199201 // specially non-GUI code
200202
201203 // Now we block our execution waiting for callbacks from Java
202204 vAndroidPipesComm.MessageLoop();
203205end.
204206```
205207
206208##### Compiling the example project in Linux
207209
2082101\> Get a working arm-linux cross-compiler which generates ARMv5 eabi with softfloat (as many phones like HTC Wildfire have no FPU)
209211
210212To do this one can download an unofficial build of FPC 2.5.1 from here:
211213
212214<http://sourceforge.net/projects/p-tools/files/Free%20Pascal%20for%20ARM/> Compiled for ARMv5 eabi with softfloat.
213215
214216Or an older FPC 2.4.2
215217
216218<http://members.yline.com/~tom_at_work/fpc-2.4.2.UNOFFICIAL.arm-linux.tar> Compiled for ARMv5 eabi with softfloat.
217219
218220Or build your own. There are instructions here: [[Setup Cross Compile For ARM]]
219221
2202222\> 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]]
221223
2222243\> Install ant, for example in Mandriva Linux:
223225
224-` urpmi ant`
226+```
227+ urpmi ant
228+```
225229
2262304\> Open the project turbochessclock/turbochessclock4android.lpi in Lazarus and build it
227231
2282325\> Build the APK file in debug mode
229233
230-` ant debug`
234+```
235+ ant debug
236+```
231237
2322386\> 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]]
233239
2342407\> Install the APK file in your phone via ADB or whatever other method you prefer:
235241
236-` ../android-sdk-linux_x86/tools/adb install android/bin/TurboChessClock4Android-debug.apk`
242+```
243+ ../android-sdk-linux_x86/tools/adb install android/bin/TurboChessClock4Android-debug.apk
244+```
237245
238246If the package is already installed you need need to do this instead:
239247
240-` ../android-sdk-linux_x86/tools/adb uninstall com.pascal.turbochessclock`\
241-` ../android-sdk-linux_x86/tools/adb install android/bin/TurboChessClock4Android-debug.apk`
248+```
249+ ../android-sdk-linux_x86/tools/adb uninstall com.pascal.turbochessclock
250+ ../android-sdk-linux_x86/tools/adb install android/bin/TurboChessClock4Android-debug.apk
251+```
242252
243253### Version 0.1
244254
245255Released on January 2011
246256
247257This version was distributed as an example project which can be obtained with the following svn command:
248258
249259svn co <https://p-tools.svn.sourceforge.net/svnroot/PascalNotes4Android/> -r 230 PascalNotes4Android
250260
251261## See also
252262
253263- [[Android Interface/Native Android GUI]]
254264- [[Android Programming]]
255265
256266---
257267*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).*
258268Category: Android

Back to history