Difference between revisions of "Android Interface/Native Android GUI"

From Lazarus wiki
Jump to navigationJump to search
m (Fixed syntax highlighting)
 
(20 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 +
{{Platform only|Android|Android|Android}}
 
Go back to [[Android Interface]]
 
Go back to [[Android Interface]]
  
==Android API Hello World in Pascal==
+
__TOC__
 +
==Android4Pascal==
  
Here is an example Pascal application written for Android.
+
See the page about [[Android4Pascal]], which is the bindings between Pascal programs and the Android Java APIs.
  
<delphi>
 
program pascalnotes4android;
 
  
{$mode objfpc}{$H+}
 
  
uses
+
==Manifest configuration==
  Classes, androidpipescomm, androidui, javalang;
 
  
{$R *.res}
+
===Control the program restart===
  
var
+
By default the program will restart on orientation change, on keyboard change, and in a lot of other cases. This is usually unwanted. To disable program restart on keyboard showing and orientation changed add this to the manifest file:
  layout: TAbsoluteLayout;
 
  params: TAbsoluteLayout_LayoutParams;
 
  tv: TTextView;
 
  et: TTextView;
 
  btn: TButton;
 
begin
 
  // Here add any initialization.
 
  // Any initialization code will be run inside Activity.onCreate,
 
  // so keep it as short as possible!
 
  // It should mostly contain GUI initialization
 
  // User interface
 
  
   // Prepares the UI of the program
+
<syntaxhighlight lang=pascal>
  layout := TAbsoluteLayout.Create;
+
   <activity
 +
    ...
 +
    android:configChanges="orientation|keyboardHidden"
 +
</syntaxhighlight>
  
  tv := TTextView.Create;
+
See also:
  tv.setText('The first Pascal Android application =)');
 
  params := TAbsoluteLayout_LayoutParams.Create(320, 300, 0, 120);
 
  layout.addView(tv, params);
 
  params.Free;
 
  
  et := TEditText.Create;
+
* http://developer.android.com/guide/topics/manifest/activity-element.html#config
  et.setText('edit me please');
 
  params := TAbsoluteLayout_LayoutParams.Create(320, 50, 0, 0);
 
  layout.addView(et, params);
 
  params.Free;
 
  
  btn := TButton.Create;
+
==Resource files==
  btn.setText('Go!');
 
{  btn.setOnClickListener(buttonClickCallback);}
 
  params := TAbsoluteLayout_LayoutParams.Create(320, 50, 0, 60);
 
  layout.addView(btn, params);
 
  params.Free;
 
  
  Activity.setContentView(layout);
+
===Guidelines for the Icons===
  
  // Now tell Java that the initialization has finished
+
Read here: http://developer.android.com/guide/practices/ui_guidelines/icon_design.html
  myAndroidPipesComm := TAndroidPipesComm.Create;
 
  myAndroidPipesComm.InitializationFinished();
 
  // Here you can add any other initialization,
 
  // specially non-GUI code
 
  
  // Now we block our execution waiting for callbacks from Java
+
==Using other APIs==
  myAndroidPipesComm.MessageLoop();
 
end.
 
</delphi>
 
  
[[Image:Simple Android app.png]]
+
===Using the Timer===
  
===Compiling the example project in Linux===
+
The Android API bindings include a handy timer control called TAndroidTimer. It works just like a Runnable, and inside it a Handler class is utilized to run the Runnable in the main GUI thread so that event executed in this timer can call Android APIs.
  
1> Get a working arm-linux cross-compiler which generates ARMv5 eabi with softfloat (as many phones like HTC Wildfire have no FPU)
+
====Timer example====
  
To do this one can download an unofficial build from here:
+
<syntaxhighlight lang=pascal>
 +
program turbochessclock4android;
  
http://members.yline.com/~tom_at_work/fpc-2.4.2.UNOFFICIAL.arm-linux.tar . Compiled for ARMv5 eabi with softfloat.
+
{$mode objfpc}{$H+}
  
Or build your own. There are instructions here: [[Setup_Cross_Compile_For_ARM]]
+
uses
 +
  Classes, SysUtils, androidpipescomm, androidview, javalang,
 +
  androidapp, androidtimer;//, gles11;
  
2> Install the Android SDK. Instructions here: [[Android_Interface/Using_the_Android_SDK%2C_Emulator_and_Phones#Using_the_Android_SDK]]
+
type
 +
  TEventHandler = class
 +
  public
 +
    procedure HandleOnTimer(ASender: TObject);
 +
    procedure buttonClickCallback(v: TView);
 +
  end;
  
3> Install ant, for example in Mandriva Linux:
+
var
 +
  //...
 +
  TimerCount: Integer = 0;
 +
  MyTimer: TAndroidTimer;
 +
  MyEventHandler: TEventHandler;
  
   urpmi ant
+
procedure TEventHandler.buttonClickCallback(v: TView);
 +
begin
 +
   MyTimer.postDelayed(100);
 +
end;
  
4> Open the project PascalNotes4Android/pascalsrc/pascalnotes4android.lpi in Lazarus and build it's build mode Android
+
procedure TEventHandler.HandleOnTimer(ASender: TObject);
 +
begin
 +
  Inc(TimerCount);
 +
  tv.setText(Format('Timer event #%d', [TimerCount]));
 +
  MyTimer.postDelayed(1000);
 +
end;
  
5> Copy the generate executable
+
begin
 
+
   MyEventHandler := TEventHandler.Create;
   cp pascalsrc/pascalnotes4android libs/armeabi/libpascalnotes4android.so
+
  // ...
 
+
end.
if the directory libs/armeabi doesn't exist, create it
+
</syntaxhighlight>
 
 
6> Build the APK file in debug mode
 
 
 
  ant debug
 
 
 
7> Connect your phone and make sure you can connect to it via ADB. More info here: [[Android_Interface/Using_the_Android_SDK%2C_Emulator_and_Phones#Recognition_of_devices_under_Linux]]
 
 
 
8> Install the APK file in your phone via ADB or whatever other method you prefer:
 
 
 
  ../android-sdk-linux_x86/tools/adb install bin/PascalNotes4Android-debug.apk
 
 
 
If the package is already installed you need need to do this instead:
 
  
  ../android-sdk-linux_x86/tools/adb uninstall com.pascalnotes
+
[[Category:Android]]
  ../android-sdk-linux_x86/tools/adb install bin/PascalNotes4Android-debug.apk
 

Latest revision as of 08:17, 9 February 2020

Android robot.svg

This article applies to Android only.

See also: Multiplatform Programming Guide

Go back to Android Interface

Android4Pascal

See the page about Android4Pascal, which is the bindings between Pascal programs and the Android Java APIs.


Manifest configuration

Control the program restart

By default the program will restart on orientation change, on keyboard change, and in a lot of other cases. This is usually unwanted. To disable program restart on keyboard showing and orientation changed add this to the manifest file:

  <activity
     ...
     android:configChanges="orientation|keyboardHidden"

See also:

Resource files

Guidelines for the Icons

Read here: http://developer.android.com/guide/practices/ui_guidelines/icon_design.html

Using other APIs

Using the Timer

The Android API bindings include a handy timer control called TAndroidTimer. It works just like a Runnable, and inside it a Handler class is utilized to run the Runnable in the main GUI thread so that event executed in this timer can call Android APIs.

Timer example

program turbochessclock4android;

{$mode objfpc}{$H+}

uses
  Classes, SysUtils, androidpipescomm, androidview, javalang,
  androidapp, androidtimer;//, gles11;

type
  TEventHandler = class
  public
    procedure HandleOnTimer(ASender: TObject);
    procedure buttonClickCallback(v: TView);
  end;

var
  //...
  TimerCount: Integer = 0;
  MyTimer: TAndroidTimer;
  MyEventHandler: TEventHandler;

procedure TEventHandler.buttonClickCallback(v: TView);
begin
  MyTimer.postDelayed(100);
end;

procedure TEventHandler.HandleOnTimer(ASender: TObject);
begin
  Inc(TimerCount);
  tv.setText(Format('Timer event #%d', [TimerCount]));
  MyTimer.postDelayed(1000);
end;

begin
  MyEventHandler := TEventHandler.Create;
  // ...
end.