100% found this document useful (1 vote)
600 views383 pages

Android Appliction PDF

The document provides instructions for installing the software required to develop Android applications using Basic4Android (B4A), including: 1. Installing the Java Development Kit (JDK) on Windows operating systems. 2. Extracting the Android Software Development Kit (SDK) to the D drive. 3. Installing and configuring the Basic4Android (B4A) integrated development environment. It then discusses creating an Android emulator or connecting a real Android device, and provides examples for writing a first and second program. The document is a tutorial for getting started with application development using the B4A programming language.

Uploaded by

Ko Zaw Yu Ko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
600 views383 pages

Android Appliction PDF

The document provides instructions for installing the software required to develop Android applications using Basic4Android (B4A), including: 1. Installing the Java Development Kit (JDK) on Windows operating systems. 2. Extracting the Android Software Development Kit (SDK) to the D drive. 3. Installing and configuring the Basic4Android (B4A) integrated development environment. It then discusses creating an Android emulator or connecting a real Android device, and provides examples for writing a first and second program. The document is a tutorial for getting started with application development using the B4A programming language.

Uploaded by

Ko Zaw Yu Ko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 383

Basic4Android ျဖင့္ Android Application ေရးနည္း (၁)

Chapter 1

Getting Started
1.1. JDK Installation 1

1.2. SDK Installation 4

1.3. B4A Installation and Configuration 4

1.4. Creating Android Virtual Device (AVD) or Emulator 14

1.5. Connecting A Real Device 15

1.6. First Program 24

1.7. Second Program 32

Chapter 2

Integrated Development Environment


2.1. Menu and Tool Bar 39

2.2. Code Area 54

2.3. Detail Area 66

2.4. Tabs 66

Chapter 3

BASIC Language
3.1. Variable and Object 70

3.2. Program Flow 87

i
3.3. Expression 88

3.4. Conditional Statement 92

3.5. Loop Structure 96

3.6. Sub 102

3.7. Event 106

3.8. Library 111

3.9. String Manipulation 114

3.10. Timer 121

3.11. File 123

3.12. TextReader/TextWriter 128

3.13. Text Encoding (Character Encoding) 131

3.14. List 135

3.15. Map 140

3.16. Regular Expression 150

3.17. Intent 154

Chapter 4

Process and Activity Life Cycle


4.1. Process 158

4.2. Activity 158

4.3. Variable 159

4.4. Activity_Create 161

4.5. FirstTime 162

4.6. Activity_Resume 163

ii
4.7. Activity_Pause 163

4.8. UserClosed 164

4.9. Activity Life Cycle 164

Chapter 5

Module
5.1. Activity Module 178

5.2. Class Module 178

5.3. Code Module 179

5.4. Service Module 181

Chapter 6

Screen Size and Resolution


6.1. Working with different Screen Sizes 189

6.2. Screen Orientation 191

Chapter 7

Designer
7.1. Menu 195

7.2. General Settings 251

7.3. Properties List 254

7.4. Image File 256

iii
7.5. Layout Variant 257

7.6. Abstract Designer 269

7.7. Adding Views by Code 274

7.8. Designer Scripts 278

7.9. Anchor 283

7.10. Auto Scale 285

7.11. UI Cloud 298

Chapter 8

Tutorials
8.1. Maths Trainer 302

8.2. Numeric Keyboard 331

8.3. Guess My Number 347

Chapter 9

Frequently Asked Questions


9.1. "You should first save the projects." Message 355

9.2. "Are you missing a library reference" Message 355

9.3. Library ကိုဘယ္လို load/update လုပ္မလဲ 356

9.4. View ကိုဘယ္အခ်ိန္မွာinitialize လုပ္ဘို႕လုိျပီး၊ ဘယ္အခ်ိန္မွာမလုိဘူးလဲ 357

9.5. စာေၾကာင္းရွည္ရင္ခြဲေရးျခင္း 357

9.6. Avoid closing an Application/capture Keycodes like Back/Menu 357

9.7. Activity ထဲသို႕ Menu Item ထည့္ျခင္း 358

9.8. Designer ကေန View ကုိဘယ္လိုဖ်က္မလဲ 360

iv
9.9. "Process has timeout" Message 360

9.10. Project ထဲက layout file သို႕မဟုတ္ အျခားဖုိင္တစ္ခုကိုဘယ္လိုဖ်က္မလဲ 361

9.11. Screen Orientation သတ္မွတ္ျခင္း 361

9.12. Activity ကုိပိတ္ျခင္း 362

9.13. Screenshot လုပ္ျခင္း 362

9.14. Compile လုပ္ျပီးရင္ဖုိင္ေတြဘယ္မွာရွိမွာလဲ 363

9.15. Array ကုိဘယ္လို pass လုပ္မလဲ 364

9.16. Device ရဲ႕ Language နဲ႕ Country ရယူျခင္း 365

9.17. ဘာေၾကာင့္ apk file name က result.apk ျဖစ္ရတာလဲ 365

9.18. ဘာေၾကာင့္ apk file name ဟာ xxx_DEBUG.apk ျဖစ္ရတာလဲ 366

9.19. Select True/Case Trick 366

9.20. Array ထဲမွာ random numberေတြမထပ္ေအာင္ထည့္ျခင္း 366

9.21. Detect Screen Orientation 367

9.22. Calling the Internal Calculator 368

9.23. Alpha/Red/Green/Blue 370

9.24. Devcie အမ်ိဳးအစားရယူျခင္း 371

9.25. Generate a Click Event 371

9.26. "Out of memory" Error/Bitmap 373

9.27. ScrollView မွ Scrollbar ကုိျဖဳတ္ျခင္း 374

9.28. Directory ရွိမရွိစစ္ျခင္း 374

9.29. Full Screenျဖစ္ေစရန္ Code ျဖင့္ျပဳလုပ္ျခင္း 375

9.30. EditText ရဲ႕ Input Mode ကုိေျပာင္းျခင္း 375

9.31. ေနာက္ဆံုး modify လုပခ


္ ဲ့တဲ့ အခ်ိန္အရ ဖိုင္မ်ားကိုစီျခင္း 376

9.32. Device ရဲ႕ dpi value ကုိရယူျခင္း 377

9.33. Java Program Line ကုိရွာျခင္း 378

v
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

Chapter 1

Getting Started

Basic4Android ကို Windows 2000 နဲ႕အထက္ PC ေတြမွာ run ႏိုင္ပါတယ္၉ Windows

32-bit/64-bit Operating System (OS) ၀မ်ိဳးလံုးမွာ အသံုးျပဳႏုိင္ပါတယ္၉ Basic4Android (B4A)

ကုိ အသံုးျပဳျပီး Android application (APK) ေရးဘုိ႕ လုိအပ္တဲ့ software ေတြကေတာ့

 Java Development Kit (JDK)

 Software Development Kit (SDK)

 Basic4Android

တုိ႕ျဖစ္ပါတယ္၉

1.1. JDK Installation

အသံုးျပဳမယ့္ computer (PC) မွာ Windows 32-bit OS တင္ထားရင္ jdk-7u45-windows-

i586.exe file ကို run ပါ၉ Winodws 64-bit OS ဆိုရင္ jdk-7u45-windows-x64.exe ကို run ျပီး

ေအာက္ပါအဆင့္မ်ားအတုိင္း install လုပ္ပါ၉ ှိုင္းျပထားတဲ့ button ေတြကိုႏွိပ္ပါ၉

ပံု ဿ-ဿ

1
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ဿ-၀

ပံု ဿ-၁

2
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ဿ-၂

ပံု ဿ-၃

3
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ဿ-၄

1.2. SDK Installation

sdk.rar ကုိ D drive ေအာက္မွာ ျဖည္ခ်လိုက္ရင္ ရပါျပီ၉ (D drive မရွိလဲ အျခား drive တစ္ခု

ခုမွာ ျဖည္ခ်ႏုိင္ပါတယ္)

1.3. B4A Installation and Configuration

Basic4Android_3.50.exe file ကို run ျပီး ေအာက္ပါအဆင့္မ်ားအတုိင္း install လုပ္ပါ၉

(ပံု ဿ-၅ မွ ဿ-ဿ၀ ထိ)

4
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ဿ-၅

ပံု ဿ-၆

5
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ဿ-၇

ပံု ဿ-ဿှ

6
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ဿ-ဿဿ

Uncheck လုပ္ပါ

ပံု ဿ-ဿ၀

7
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
1.3.1. B4A Registration

b4a-gen.exe file ကို C:\Program Files\Anywhere Software\Basic4android folder ထဲ

copy ကူးထည့္ျပီးမွ run ပါ၉ hhl@gmail.com လုိ႕ ရိုက္ထည့္ျပီး „Generate license file‟ button ကုိ

ႏွိပ္ပါ၉ (ပံု ဿ-ဿ၁)

ပံု ဿ-ဿ၁

Register ျပဳလုပ္ျပီးေၾကာင္း dialog box ေပၚလာရင္ „Ok‟ button ကို ႏွိပ္ပါ၉ (ပံု ဿ-ဿ၂)

ပံု ဿ-ဿ၂

1.3.2. B4A Configuration

Additional Libraries folder ကို C:\Program Files \Anywhere Software \

Basic4android ေအာက္မွာ ထားပါ၉ Desktop ေပၚက icon ကို double click လုပ္ျပီး

8
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
run ပါ၉ (ပံု ဿ-ဿ၃) မွာ ျပထားတဲ့အတုိင္း Integrated Development Environment (IDE) window

ေပၚလာမယ္၉ IDE window ဟာ application ေတြကို create, compile, run ရမယ့္ေနရာ

ျဖစ္ပါတယ္၉

ပံု ဿ-ဿ၃

javac.exe, android.jar file ေတြနဲ႕ Additional Libraries folder ရွိရာ path ေတြကို B4A

မွာ configure လုပ္ရမယ္၉ IDE window ရဲ႕ menu bar မွာ [Tools > Configure Paths] ကို ေရြးပါ၉

(ပံု ဿ-ဿ၄)

9
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ဿ-ဿ၄

(ပံု ဿ-ဿ၅) မွာ ျပထားသလို dialog တစ္ခု ေပၚလာမယ္၉

ပံု ဿ-ဿ၅

Browse button ကုိ ႏွိပ္ျပီး C:\Program Files\Java\jdk1.7.0_45\bin\javac.exe file ကို

ေရြးေပးပါ၉ (ပံု ဿ-ဿ၆)

10
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ဿ-ဿ၆

ပံု ဿ-ဿ၇

ဒုတိယ Browse button ကုိ ႏွိပ္ျပီး D:\sdk\platforms\android-19\android.jar file ကို

ေရြးေပးလိုက္ပါ၉ (ပံု ဿ-၀ှ)

11
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ဿ-၀ှ

ပံု ဿ-၀ဿ

တတိယေျမာက္ Browse button ကုိ ႏွိပ္ျပီး C:\Program Files\Anywhere Software\

Basic4android\Additional Libraries ကုိ ေရြးေပးလိုက္ပါ၉ (ပံု ဿ-၀၀)

12
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ဿ-၀၀

ပံု ဿ-၀၁

(ပံု ဿ-၀၁) အတုိင္း ေပၚလာရင္ „Ok‟ button ကို ႏွိပ္လိုက္ပါ၉ အခုဆိုရင္ application ေရးရန္

အသင့္ျဖစ္ပါျပီ၉

13
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
1.4. Creating Android Virtual Device (AVD) or Emulator

Emulator ဆိုတာ PC ေပၚမွာ real device ကို simulate လုပ္ေပးတဲ့ software တစ္ခု

ျဖစ္ပါတယ္၉ Application ေရးျပီးရင္ real device (phone/tablet) နဲ႕ ခ်ိတ္ျပီး install လို႕ရသလို

Emulator မွာလည္း install လုပ္လို႕ရပါတယ္၉ Emulator ကေတာ့ real device ထက္ ေႏွးေကြး

ပါတယ္၉ ေအာက္ပါအဆင့္ေတြအတုိင္း emulator ကို create လုပ္ပါ၉

IDE window ရဲ႕ menu bar မွာ [Toos > Run AVD Manager] ကို ေရြးပါ၉ (ပံု ဿ-၀၂)

ကဲ့သို႕ window ေပၚလာရင္ „Close‟ button ကုိ ႏွိပ္ပါ၉

ပံု ဿ-၀၂

14
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Android SDK Manager window မွာ [Tools > Manage AVDs…] ကို ေရြးပါ၉ (ပံု ဿ-၀၃)

ပံု ဿ-၀၃

(ပံု ဿ-၀၄) မွာကဲ့သို႕ window ေပၚလာမယ္၉ New button ကို ႏွိပ္ပါ၉

ပံု ဿ-၀၄

15
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
(ပံု ဿ-၀၅) အတုိင္း ေရြးခ်ယျ္ပီး „Ok‟ button ကုိ ႏွိပ္ပါ၉

ပံု ဿ-၀၅

16
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
အခုဆိုရင္ emulator တစ္ခုကို create လုပ္လုိ႕ ျပီးပါျပီ၉ (ပံု ဿ-၀၆)

ပံု ဿ-၀၆

Emulator ကို ဖြင့္ခ်င္ရင္ Emulator1 ကို select လုပ္ျပီး „Start‟ button ကုိ ႏွိပ္ပါ၉ (ပံု ဿ-၀၇)

ပံု ဿ-၀၇

17
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ဿ-၁ှ

ပံု ဿ-၁ဿ

ပံု ဿ-၁၀ ပံု ဿ-၁၁

18
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ဿ-၁၂

(ပံု ဿ-၁၂) အတုိင္း ျမင္ရရင္ application ကို emulator မွာ install လုပ္ဘို႕ အသင့္ျဖစ္ပါျပီ၉

1.5. Connecting a Real Device

PC နဲ႕ device ကုိခ်ိတ္ဆက္ရာမွာ USB နဲ႕ေသာ္လည္းေကာင္း၈ B4Abridge ကိုသံုးျပီး

Wi-Fi/Bluetooth နဲ႕ေသာ္လည္းေကာင္း ခ်ိတ္ဆက္ႏုိင္ပါတယ္၉

1.5.1. USB ျဖင့္ခ်ိတ္ဆက္ျခင္း

o PC မွာ USB driver ကို install လုပ္ပါ၉

19
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

o Device မွာ Settings ကေန Unknown sources နဲ႕ USB debugging ကုိ

check လုပ္ပါ၉ (ပံု ဿ-၁၃၈ ဿ-၁၄)

ပံု ဿ-၁၃

ပံု ဿ-၁၄

အထက္ပါအခ်က္မ်ား ျပည့္စံုရင္ PC နဲ႕ device ကို USB cable နဲ႕ ခ်ိတ္ဆက္လုိက္ရန္သာ

ျဖစ္ပါတယ္၉

1.5.2. B4Abridge ျဖင့္ခ်ိတ္ဆက္ျခင္း

Wi-Fi ခ်ိတ္ဆက္ႏုိင္တဲ့ PC ျဖစ္ရပါမယ္၉ Device မွာ [Settings > More…] ကိုေရြးျပီး

(ပံု ဿ-၁၅၈ ၁၆၈ ၁၇) မွာျပထားတဲ့အတုိင္း Wi-Fi hotspot ေထာင္ရပါမယ္၉

20
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ဿ-၁၅ ပံု ဿ-၁၆

21
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ဿ-၂ှ

ပံု ဿ-၁၇

PC မွာလည္း ပံု (ဿ-၂ှ) အတိုင္း Connected ျဖစ္ေအာင္ လုပ္ရပါမယ္၉ ျပီးရင္ B4Abridge

Plus1.4 ကို device မွာ install လုပ္ျပီး open လုပ္ပါ၉ (ပံု ဿ-၂ဿ) မွာကဲ့သုိ႕ ျမင္ရမယ္၉ Start button

ကုိ ႏွိပ္ပါ၉

22
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ဿ-၂ဿ ပံု ဿ-၂၀

ျပီးရင္ B4A IDE menu မွာ (ပံု ဿ-၂၁) ပါ အဆင့္အတိုင္း လုပ္ပါ၉ [B4A Bridge > Connect

– Wireless > New IP]

ပံု ဿ-၂၁

23
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Devcie မွာျပထားတဲ့ IP address ကို ရိုက္ထည့္ျပီး Ok ကုိ ႏွိပ္ပါ၉ ပံု (ဿ-၂၂)

ပံု ဿ-၂၂

IDE ဘယ္ဘက္ေအာက္ေထာင့္မွာ (ပံု ဿ-၂၃) အတုိင္း Connected ျဖစ္သြားမယ္၉ Device

မွာလည္း (ပံု ဿ-၂၀) မွာ ျပထားတဲ့အတုိင္း Connected ျဖစ္သြားရင္ program run ရန္ အသင့္ျဖစ္ပါ

ျပီ၉
ပံု ဿ-၂၃

1.6. First Program

အလြယ္ကူဆံုး application တစ္ခုနဲ႕ စတင္ပါမယ္၉ Application ကုိ run ရင္ “Hello

World!” ဆိုတဲ့ message box ေပၚလာမယ့္ application ေရးျပပါမယ္၉

Desktop ေပၚက icon ကုိ double click လုပ္ျပီး run ပါ၉ (ပံု ဿ-၂၄) အတုိင္း IDE

window ေပၚလာမယ္၉

Code Area

Tab Area
ပံု ဿ-၂၄

24
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Save the Program

Project file ကုိ save လုပ္ရန္ [File > Save] ကို ေရြးပါ၉ (ပံု ဿ-၂၅)

ပံု ဿ-၂၅

Project တစ္ခုအတြက္ folder တစ္ခု လုပ္ရမယ္၉ ဒါေၾကာင့္ New folder ကုိ ေရြးပါ၉

(ပံု ဿ-၂၆)

ပံု ဿ-၂၆

25
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Folder name ကုိ „FirstProgram‟ လို႕ေပးျပီး „Open‟ button ကို ႏွိပ္ပါ၉ (ပံု ဿ-၂၇)

ပံု ဿ-၂၇

File name မွာ MyFirst လို႕ ေပးပါ၉ MyFirst ဟာ project file name ျဖစ္ပါတယ္၉ File type

ကေတာ့ „.b4a‟ ျဖစ္ပါတယ္၉ ျပီးရင္ „Save‟ button ကုိ ႏွိပ္ပါ၉ (ပံု ဿ-၃ှ)

ပံု ဿ-၃ှ

26
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
အခုဆိုရင္ D:\FirstProgram folder ထဲမွာ MyFirst.b4a ဆိုတဲ့ project file တစ္ခု create

လုပ္ျပီးသြားပါျပီ၉

Icon

Icon ဆိုတာ ကိုယ့္ application ကုိ device မွာ install လုပ္ျပီးတဲ့အခါ device screen မွာ

ျမင္ရမယ့္ပံု ျဖစ္ပါတယ္၉ တစ္နည္းအားျဖင့္ မိမိ application ရဲ႕ အမွတ္သေကၤတပံု ျဖစ္ပါတယ္၉ ဥပမာ

(ပံု ဿ-၃ဿ) ကိုၾကည္ပါ၉ CB Bank application ရဲ႕ icon ကုိ screen မွာျမင္ရပံုျဖစ္ပါတယ္၉

ပံု ဿ-၃ဿ

Application အတြက္ စိတ္ၾကိဳက္ icon တစ္ခု ျပဳလုပ္ထားပါ၉ ျပီးရင္ IDE menu bar မွာ

[Project > Choose Icon] ကို ေရြးျပီး icon ကုိ ေရြးေပးလိုက္ပါ၉ Icon မေရြးေပးရင္ေတာ့ Android

ရဲ႕ default icon ျဖစ္တဲ့ စက္ရုပ္ပံုေလး ကိုသတ္မွတ္ေပးပါလိမ့္မယ္၉ ဒါေၾကာင့္ icon

အသင့္မရွိေသးရင္ ဒီအဆင့္ကို ေက်ာ္သြားလို႕ရပါတယ္၉

Package name

Application အတြက္ package name ေပးရပါမယ္၉ IDE menu bar မွာ [Project >

Package name] ကို ေရြးပါ၉ (ပံု ဿ-၃၀) မွာ ျပထားသလို package name ေပးလုိက္ျပီး „Ok‟ button

ကုိ ႏွိပ္ပါ၉

ပံု ဿ-၃၀

27
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Coding

Program code ကုိ စေရးပါမယ္၉ Code area မွာ ေအာက္ပါအတုိင္း နည္းနည္း ျပင္ေရးလိုက္

ပါ၉ ဒီလိုေျပာင္းေရးလိုက္ပါ၉ ဒီမွာ Hello လုိ႕ေပးထားရင္


Region Project Attributes application ကုိ install လုပ္ျပီးသြားတဲ့အခါ device screen
#ApplicationLabel: Hello
မွာ icon နဲ႕အတူ „Hello‟ ကိုျမင္ရမွာျဖစ္ပါတယ္၉ Hello
#VersionCode: 1
မဟုတ္လည္း မိမိစိတ္ၾကိဳက္ေပးႏုိင္ပါတယ္၉
#VersionName:

'SupportedOrientations possible values: unspecified, landscape or portrait.

#SupportedOrientations: unspecified

#CanInstallToExternalStorage: False

End Region

Region Activity Attributes

#FullScreen: False

#IncludeTitle: True

End Region

Sub Process_Globals

'These global variables will be declared once when the application starts.

'These variables can be accessed from all modules.

End Sub

Sub Globals

'These global variables will be redeclared each time the activity is created.

'These variables can only be accessed from this module.

End Sub

28
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub Activity_Create(FirstTime As Boolean)

'Do not forget to load the layout file created with the visual designer. For

example:

Msgbox("ံHello World!", "Greeting")


ဒီ code ႏွစ္ေၾကာင္းေရးပါ၉
Log(“Hi”)

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed AsBoolean)

End Sub

Run the program

Application ကုိ run ရန္ device ကို PC နဲ႕ ခ်ိတ္ပါ၉ (Emulator မွာ install လုပ္ခ်င္ရင္ေတာ့

emulator ကုိ အရင္ဖြင့္ထားပါ) IDE tool bar ေပၚက Compile options dropdown list မွာ

Release ကုိ ေရြးပါ၉ (ပံု ဿ-၃၁)

ပံု ဿ-၃၁

ျပီးရင္ IDE tool bar ေပၚက icon ကို click ပါ၉ (ပံု ဿ-၃၂) မွာကဲ့သုိ႕ dialog box ကုိ

ျမင္ရပါမယ္၉

29
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ဿ-၃၂

IDE window ရဲ႕ Tabs area မွာ „Logs‟ tab ကို ေရြးလုိက္ပါ၉ (ပံု ဿ-၃၃)

Log(“Hi”) လို႕ ေရးခဲ့တဲ့အတြက္ ဒီေနရာမွာ „Hi‟ လို႕

ျမင္ရတာပါ၉

ပံု ဿ-၃၃

Device မွာလည္း (ပံု ဿ-၃၄) အတုိင္း message box တစ္ခု ျမင္ရမယ္၉

30
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ဿ-၃၄

အခုဆိုရင္ Application ကုိ device မွာ install လုပ္ျပီး ျဖစ္ပါတယ္၉ APK file ကုိလည္း

D:\FirstProgram\Objects folder ထဲမွာ generate လုပ္ေပးျပီး ျဖစ္ပါတယ္၉ (ပံု ဿ-၃၅)

ပံု ဿ-၃၅

31
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
1.7. Second Program

First Program မွာ User Interface (UI) မပါပါဘူး၉ Second Program မွာေတာ့ UI သံုးျပီး

ရိုးရွင္းတဲ့ application တစ္ခု ေရးျပပါမယ္၉ UI မွာ button တစ္ခု ထည့္ထားပါမယ္၉ Button ကုိ

ႏွိပ္ရင္ “Hello World!” လို႕ ေပၚေစမယ့္ application ေရးမွာပါ၉

IDE widow ရဲ႕ menu bar မွာ Designer ကုိ ေရြးပါ၉ Designer နဲ႕ Abstract Designer

window ၀ခု ေပၚလာမယ္၉ (ပံု ဿ-၃၆)

ပံု ဿ-၃၆

User interface layout ကုိ Abstract Designer မွာသာမက emulator နဲ႕ real device

တုိ႕မွာလည္း design လုပ္ႏုိင္ပါတယ္၉ Abstract Designer မွာ ျပဳလုပ္ျခင္းရဲ႕ အားနည္းခ်က္ကေတာ့

Abstract Designer မွာ ျမင္ရတဲ့ view ေတြဟာ application ကုိ run လုိက္ရင္ ျမင္ရမယ့္အတုိင္း

အတိအက် မျဖစ္ပါဘူး၉

Device/emulator မွာ design လုပ္ရင္ေတာ့ အတိအက် ျမင္ရမယ္၉ Device မွာ design

လုပ္ခ်င္ရင္ device ကုိ PC နဲ႕ ခ်ိတ္ထားရမယ္၉ Emulator မွာ design လုပ္ခ်င္ရင္ emulator ကုိ

ဖြင့္ထားရမယ္၉

32
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ျပီးရင္ device/emulator နဲ႕ IDE ကုိ ခ်ိတ္ဆက္ေပးရပါမယ္၉ ခ်ိတ္ဆက္ နည္းကေတာ့

Designer window မွာ [Tools > Connect to Device/Emulator >] ကို ေရြးပါ၉ သိ႕ု မဟုတ္

Designer window မွာ (ပံု ဿ-၃၇) မွာ ျပထားတဲ့အတုိင္း chain ပံုေလးကို double click လုပ္လုိက္ပါ၉

ပံု ဿ-၃၇

PC မွာ device ေရာ emulator ပါ ခ်ိတ္ထားရင္ေတာ့ (ပံု ဿ-၄ှ) အတိုင္း dialog box

ေပၚလာမယ္၉ ကိုယ္ခ်ိတ္ခ်င္တဲ့ device/emulator ကို ေရြးပါ၉

ပံု ဿ-၄ှ

Status က (ပံု ဿ-၄ဿ) အတုိင္း Connected ျဖစ္သြားရင္ IDE နဲ႕ device/emulator

ခ်ိတ္မိသြားျပီမို႕ စတင္ design လုပ္ႏုိင္ပါျပီ၉

ပံု ဿ-၄ဿ

33
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Designer window ရဲ႕ menu bar မွာ [Add View > Button] ကုိ ေရြးပါ၉ (ပံု ဿ-၄၀)

ပံု ဿ-၄၀ ပံု ဿ-၄၁

Abstract Designer မွာ (ပံု ဿ-၄၁) မွာကဲ့သို႕ „Button‟ view တစ္ခု ေရာက္လာပါမယ္၉

Device နဲ႕ ခ်ိတ္ဆက္ထားရင္ေတာ့ (ပံု ဿ-၄၂) မွာ ျပထားတဲ့အတုိင္း ျမင္ရပါမယ္၉ Emulator နဲ႕

ခ်ိတ္ဆက္ထားရင္ေတာ့ (ပံု ဿ-၄၃) မွာ ျပထားသလုိ ျဖစ္ပါမယ္၉

ပံု ဿ-၄၂ ပံု ဿ-၄၃

34
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Designer window မွာ

Event Name: btnCM

Text: Click Me

လုိ႕ေရးပါ၉ (ပံု ဿ-၄၄)

ပံု ဿ-၄၄

Designer window menu bar မွာ [Tools > Generate Members] ကို ေရြးပါ၉ (ပံု ဿ-၄၅)

မွာ ျပထားတဲ့အတုိင္း Click ကုိ check လုပ္ပါ၉ Generate members > Close button ေတြကို ႏွိပ္ပါ၉

ပံု ဿ-၄၅

35
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ျပီးရင္ Designer window ရဲ႕ menu မွာ [File > Save As] ကို ေရြးပါ၉ (ပံု ဿ-၄၆) ကဲ့သို႕

ေပၚလာရင္ „Layout1‟ လို႕ ေရးျပီး „Ok‟ button ကုိ ႏွိပ္ပါ၉

ပံု ဿ-၄၆

IDE code area မွာ Activity_Create နဲ႕ btnCM_Click sub ေတြကို ေအာက္ပါအတုိင္း

ေရးပါ၉ က်န္ sub မ်ားကို မူလအတုိင္း ဆက္ထားပါ၉

Sub Activity_Create(FirstTime As Boolean)

Activity.LoadLayout("Layout1")

End Sub

Sub btnCM_Click
Msgbox("Second Program”,"Welcome!")
End Sub

ျပီးရင္ program ကို run ပါ၉ (ပံု ဿ-၄၇) အတုိင္း ျမင္ရမယ္၉ „Click Me‟ button ကုိ ႏွိပ္ပါ၉

(ပံု ဿ-၅ှ) မွာကဲ့သုိ႕ message box တစ္ခု ျမင္ရပါမယ္၉

36
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ဿ-၄၇ ပံု ဿ-၅ှ

37
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

Chapter 2

Integrated Development Environment

Application development လုပ္တဲ့အခါ IDE မွာ လုပ္ရပါတယ္၉ ဒါေၾကာင့္ IDE

အသံုးျပဳပံုကို ေလ့လာဘို႕ လိုပါတယ္၉

IDE မွာ (ပံု ၀-ဿ) တြင္ျပထားတဲ့အတုိင္း အပိုင္း ၂ပိုင္း ရွိပါတယ္၉ ပံု ၀-ဿ

 Menu and Toolbar

 Code Area

 Detail Area

 Tabs Area

38
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
2.1. Menu and Toolbar

2.1.1. File Menu

New

Project အသစ္တစ္ခုကို create လုပ္ရန္၉

Open Source

ရွိျပီးသား Project ကို ဖြင့္ရန္၉

Save

Project ကုိ save ရန္၉

Export As Zip

Project ကုိ zip file အျဖစ္ သိမ္းရန္၉

Page Setup

Source code ေတြကို print မထုတ္မီ paper, orientation, margins သတ္မွတ္ရန္၉

Print Preview

Print မထုတ္မီ နမူနာၾကည့္ရန္၉

Print

Source code ေတြကို print ထုတ္ရန္၉

Exit

IDE ကို ပိတ္ရန္၉ Exit ေအာက္မွာေတာ့ ေနာက္ဆံုးဖြင့္ခဲ့တဲ့ project ၄ခုကို ျပေနမွာပါ၉

39
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
2.1.2. Edit Menu

Cut (Ctrl+X)

Select လုပ္ထားတဲ့ text ကုိ ဖ်က္ျပီး clipboard မွာ copy လုပထ


္ ားေပးပါတယ္၉

Cut Line (Ctrl+Y)

Cursor ရွိေနတဲ့ တစ္လုိင္းလံုးကို ဖ်က္ရန္၉

Copy (Ctrl+C)

Select လုပ္ထားတဲ့ text ကုိ clipboard မွာ copy လုပထ


္ ားေပးပါတယ္၉

Paste (Ctrl+V)

Clipboard ေပၚက text ကုိ cursor ခ်ထားတဲ့ေနရာမွာ paste လုပ္ေပးပါတယ္၉

Undo (Ctrl+Z)

ေနာက္ဆံုးလုပ္ခဲ့တဲ့ operation ကို ျပန္ဖ်က္ေပးပါတယ္၉ ဥပမာ Dim လို႕ရိုက္ျပီး Undo

လုပ္ၾကည့္ပါ၉ Dim ဆိုတဲ့စာလံုး ပ်က္သြားမယ္၉

Redo (Ctrl+Shift+Z)

ေနာက္ဆံုးလုပ္ခဲ့တဲ့ operation ကုိ ထပ္လုပ္ေပးပါတယ္၉ ဥပမာ Dim လုိ႕ရိုက္ျပီး Redo

လုပ္ၾကည့္ပါ၉ ေနာက္ထပ္ Dim ဆိုတာ ထပ္ေရးျပီး ျဖစ္ပါလိမ့္မယ္၉

Find (F3 or Ctrl+F)

စာလံုးမ်ားရွာရန္၉

40
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Find All References (F7)

Select လုပ္ထားတဲ့ text ရဲ႕ reference ေတြ အားလံုးကို ျပေပးပါတယ္၉ သက္ဆုိင္ရာ code

ကို သြားခ်င္ရင္ reference ကုိ click လုပ္ျခင္းျဖင့္ အလြယ္တကူ ေရာက္ႏုိင္ပါတယ္၉ (ပံု ၀-၀)

ပံု ၀-၀

Block Comment

Comment ဆိုတာ program code ေတြကို ဖတ္တဲ့သူ နားလည္ေအာင္ မွတ္ခ်က္ေရးေပး

ထားတာပါ၉ အဲဒီမွတ္ခ်က္ေတြကို compiler က compile မလုပ္ေအာင္ code ေရွ႕မွာ ( „ ) ထည့္ေပး

ရပါတယ္၉ Comment အျဖစ္ သတ္မွတ္ခ်င္တဲ့လုိင္းေတြ မ်ားေနရင္ တစ္လိုင္းခ်င္း ( „ ) လိုက္ထည့္္မ

ေနဘဲ code ေတြကို select လုပ္ျပီး Block Comment ကို click ပါ၉ (ပံု ၀-၁) နဲ႕ (ပံု ၀-၂)

ပံု ၀-၁ ပံု ၀-၂

41
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

Block Uncomment

Comment သေကၤတျဖစ္တဲ့ ( „ ) ေတြကိုဖ်က္ခ်င္ရင္ code ေတြကို select လုပ္ျပီး Block

Uncomment ကို click ပါ၉

Remove All Bookmarks

Bookmark ေတြ အားလံုးကို ဖယ္ရွားရန္၉ Bookmark ဆိုတာကေတာ့ ကိုယ္စိတှ


္ င္စားတဲ့

code ကုိ ေနာက္တစ္ခါ ျပန္ရွာရင္ လြယ္ေအာင္ မွတ္သားထားတာပါ၉

Remove All Breakpoints

Breakpoint ေတြ အားလံုးကို ဖယ္ရွားရန္၉

Outlining

Function ၁ခုရတ
ွိ ယ္၉

- Toggle All (Ctrl-Shift-o)

Collapse လုပ္ထားတဲ့ code ဆိုရင္ expand လုပ္ေပးျပီး expand လုပ္ထားတဲ့ code ဆိုရင္

collapse လုပ္ေပးပါတယ္၉

- Expand All

Code ကို expend လုပ္ေပးပါတယ္၉

- Collapse All

Code ကို collapse လုပ္ေပးပါတယ္၉ အတြင္းက code ေတြကို မျမင္ရေတာ့ပါ၉

42
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
2.1.3. Designer Menu

Designer ကုိဖြင့္ရန္သံုးပါတယ္၉ အေသးစိတ္ကို Chapter 7 Designer တြင္ ၾကည္ပ


့ ါ၉

2.1.4. Project Menu

Add New Module

Module အသစ္ထပ္ထည့္ရန္၉ Module type ၂မ်ိဳးရွိပါတယ္၉

 Activity module

 Class module

 Code module

 Service module

Add Existing Module

အျခား project ထဲက module ကိုလက္ရွိ project ထဲ copy လုပ္ျပီး ထည့္ရန္ျဖစ္ပါတယ္၉

Main module မွအပ အျခား module ေတြအတြက္ ေအာက္ပါ option ၁ခုကို သံုးႏုိင္ပါတယ္၉

Change Module Name

Module name ေျပာင္းရန္သံုးပါတယ္၉

Remove Module

Active module စာရင္းထဲက ထုတ္ပယ္လုိက္တာပါ၉ Module ကုိ ဖ်က္ပစ္တာ မဟုတပ


္ ါ၉

Hide Module

Module ကို hide လုပ္ရန္သံုးပါတယ္၉ Hide လုပ္ထားရင္ editor မွာ မျမင္ရေတာ့ပါဘူး၉

Modules tab မွာေတာ့ ျမင္ေနရဦးမွာပါ၉ Modules tab ကို သြားျပီး hide လုပ္ထားတဲ့ module ကုိ

click လုပ္လိုက္ရင္ေတာ့ editor မွာ ျပန္ျမင္ရပါလိမ့္မယ္၉

43
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Choose Icon

Application အတြက္ မိမိစိတ္ၾကိဳက္ icon ကို ေရြးေပးႏုိင္ပါတယ္၉ Icon ကို ၾကိဳတင္လုပ္ထား

ျပီး ျဖစ္ရမယ္၉

Package Name

Application တုိင္းမွာ package name တစ္ခု ရွိရပါမယ္၉ Package name ဟာ application

ရဲ႕ ID ျဖစ္ပါတယ္၉

Unique Name

Application တစ္ခုရဲ႕ package name ဟာ အျခား application ရဲ႕ package name နဲ႕

မတူရပါဘူး၉ အျခား developer ေတြရဲ႕ package name နဲ႕ မတူရေလေအာင္ ကိုယ့္ရဲ႕ Internet

domain name ကို ေျပာင္းျပန္လုပ္ျပီး သံုးသင့္ပါတယ္၉ ဥပမာ ကိုယ့္ domain name က

compumobile.com ဆိုရင္ package name ကုိ com.compumobile.appname လို႕ေပးပါ၉

„appname„ ေနရာမွာ သင့္ေတာ္တာတစ္ခု ေပးပါ၉

Allowed Characters

Package name ေပးတဲ့အခါ dot (.), စာလံုးေသး (a to z), numbers (0 to 9),

underscores (_) အစရွိတဲ့ character ေတြ သံုးႏုိင္ပါတယ္၉ အစ စာလံုးေတြကိုေတာ့ „a to z‟ letter

ေတြနဲ႕ဘဲ စေရးႏုိင္ပါတယ္၉ Package name မွာ အနည္းဆံုး dot (.) နဲ႕ ျခားထားတဲ့ component ၀ခု

ပါရမယ္၉ စာလံုးအၾကီး (A to Z) ကုိလည္း သံုးႏိုင္ပါတယ္၉ ဒါေပမယ့္ တစ္ခါတရံမွာ ျပႆနာ

ရွိႏုိင္တာေၾကာင့္ စာလံုအေသးသံုးတာက ပိုစိတ္ခ်ရပါတယ္၉

Google Play URL

Application ကုိ Google Play မွာ တင္လိုက္ျပီဆိုပါစို႕၉ Google Play မွာ application ရဲ႕

URL ကိုသတ္မွတ္ဘုိ႕ package name ကုိအသံုးျပဳပါတယ္၉ ဥပမာ package name က

44
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
com.compumobile.appname ဆိုရင္ Google Play မွာ application ရဲ႕ URL က

https://play.google.com/store/apps/details?id=com.compumobile.appname

လို႕ျဖစ္ေနမွာပါ၉

Application ကုိ Google Play မွာ တင္ျပီးရင္ package name မေျပာင္းသင့္ေတာ့ပါဘူး၉

Package name ဟာ application ရဲ႕ ID ျဖစ္တာေၾကာင့္ application ကို Google Play မွာ

တင္ျပီးေနာက္ version အသစ္ ထပ္ေရးျဖစ္တယ္ဆိုပါစို႕၉ Version အသစ္ကုိ package name

ေျပာင္းျပီး Google Play မွာ ထပ္တင္မယ္ဆိုရင္ ေနာက္ထပ္တင္လိုက္တဲ့ application ကုိ

အသစ္တစ္ခလ
ု ို႕ မွတ္ယူလိုက္မွာျဖစ္ပါတယ္၉ အရင္တင္ထားတဲ့ application ရဲ႕ new version လို႕

မသတ္မွတ္ေတာ့ပါဘူး၉ ဘာေၾကာင့္လည္းဆိုေတာ့ package name ေျပာင္းလိုက္လို႕ပါ၉ Package

name မတူေတာ့ အျခား application တစ္ခုလို႕ဘဲ သတ္မွတ္လိုက္တာေပါ့၉ အဲဒါဆိုရင္ အရင္

application ကို download ဆြဲထားတဲ့ user ေတြဟာ version အသစ္ကို auto update

မရႏုိင္ေတာ့ပါဘူး၉

Manifest Editor

Application ရဲ႕ settings file ျဖစ္ပါတယ္၉

Do Not Overwrite Manifest File

မသံုးေတာ့ပါ၉

Where Are the Other Object Properties?

Project attributes နဲ႕ activity attributes ေတြ အေၾကာင္းရွင္းျပထားတဲ့ web page ကုိ

ေရာက္သြားပါမယ္၉

Compile & Run (Alt+1)

B4A code ေတြကို Java code အျဖစ္ေျပာင္းေပးျပီး device မွာ run ရန္၉ Run icon ကုိ

45
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ႏွိပ္တာနဲ႕ တူတူပါဘဲ၉ Debug (legacy) mode မွာ run ရင္ Objects folder ထဲမွာ (ပံု ဿ-၀၃)

မွာျပထားတဲ့အတိုင္း folder နဲ႕ file ေတြကို ထုတ္ေပးပါတယ္၉

ပံု ၀-၃

Test Compilation (Alt+2)

Compile ဘဲ လုပ္ပါတယ္၉ Device မွာ မ run ပါဘူး၉ ဒါေၾကာင့္ Device ကို PC နဲ႕

ခ်ိတ္ထားစရာ မလိုပါဘူး၉ Program မွာ error ရွိ၈ မရွိ စမ္းတဲ့အခါ သံုးသင့္ပါတယ္၉ Error မရွိေတာ့

မွသာ Compile & Run (Alt+1) ကို သံုးသင့္ပါတယ္၉

Compile & Run (background) (Alt+3)

Compile & Run နဲ႕ တူတူပါဘဲ၉ Background မွာလုပ္ေပးေနမွာပါ၉

Run Last Deployment (Alt+4)

Device မွာ install လုပ္ျပီးသား application ကို ျပန္ run ရန္၉ Compile လုပ္ျခင္း၈ install

လုပ္ျခင္း မရွိပါဘူး၉

Compile to Library (Alt+5)

Project ကို library file အျဖစ္ compile လုပ္ရန္၉ ဒီ library file ကုိ အျခား project ေတြ

မွာယူ သံုးႏုိင္ပါတယ္၉

46
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Compile (without signing)

Code ကုိ compile လုပ္ေပးပါတယ္၉ ဒါေပမယ့္ Objects folder ထဲမွာ apk file ကို

ထုတ္ေပးမွာ မဟုတ္ပါဘူး၉ Application ကုိ non-Basic4Android keystore နဲ႕ sign လုပ္တဲ့အခါမွာ

သံုးပါတယ္၉

2.1.5. Tools Menu

IDE Options

- Tab Size

Tab key ကို ႏွိပ္လုိက္တဲ့အခါ ဘယ္ေလာက္အကြာအေှး ေရြ႕သြားေစခ်င္သလဲဆိုတာကုိ

သတ္မွတ္ေပးႏုိင္ပါတယ္၉ Default ကေတာ့ 4 ပါ၉ (1 ကေန 15 ထိဘဲ ေပးလို႕ရပါတယ္)

- Change Font

Code window ရဲ႕ font ကို ေျပာင္းရန္၉ Zawgyi-One font ေျပာင္းထားရင္ string ေတြကို

ျမန္မာလို ေရးလို႕ရပါတယ္၉ Code ေတြကိုေတာ့ ျမန္မာလုိ ေရးလို႕မရပါ၉

- Word Wrap

Word Wrap ကုိ check မလုပ္ထားရင္ စာေၾကာင္းရွည္ၾကီးေတြဟာ window ထက္ေက်ာ္

သြားတဲ့အတြက္ မျမင္ရေတာ့ဘဲ ျဖစ္တတ္ပါတယ္၉ Word Wrap ကုိ check လုပ္ထားရင္ေတာ့

စာေၾကာင္းရွည္ရင္ ေနာက္တစ္လုိင္းကို ဆင္းေပးပါတယ္၉

- Auto Save

Auto Save ကုိ check လုပ္ထားရင္ application ကုိ run တဲ့အခါ program code ကုိေရာ

layout file ကုိပါ save လုပ္ေပးပါတယ္၉

47
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
- Show Tooltips During Typing

Show Tooltips During Typing ကို check လုပ္ထားရင္ method name ေရးျပီး

ကြင္းစေလးတစ္ခရ
ု ိုကလ
္ ိုက္တာနဲ႕ သက္ဆိုင္ရာ method အတြက္ ေရးရမယ့္ပံုစံကို နမူနာ

ျပေပးပါတယ္၉ (ပံု ၀-၄)

ပံု ၀-၄

- Configure Process Timeout

IDE နဲ႕ device/emulator ခ်ိတ္ဆက္ဘုိ႕ အခ်ိန္ (အမ်ားဆံုး) ဘယ္ေလာက္ထိ ေစာင့္ရမယ္

ဆိုတာ သတ္မွတ္ေပးတာပါ၉ သတ္မွတ္ထားတဲ့ အခ်ိန္အတြင္း မခ်ိတ္ဆက္မိရင္ error message

ျပပါလိ့မ္မယ္၉ Default value ကေတာ့ 30 seconds ပါ၉ ကိုယ္လိုသလို ျပင္ႏုိင္ပါတယ္၉

- Test Compile When Saving

Project ကုိ save လုပ္တအ


ဲ့ ခါတုိင္းမွာ compile လုပ္ေပးပါတယ္၉ Error ရွိရင္ color line

ေတြနဲ႕ ျပေပးပါတယ္၉ (ပံု ၀-၅)

ပံု ၀-၅

Clean Files Folder (unused files)

Files folder ေအာက္က (project မွာမသံုးတဲ)့ file ေတြကို delete လုပ္ရန္၉

48
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

Clean Project

Compile လုပ္စဥ္က generate လုပ္ခဲ့တဲ့ file ေတြကို delete လုပ္ရန္၉

Run AVD Manager

SDK update နဲ႕ Emulator create, start, edit, delete လုပ္ရန္၉

Configure Paths

javac.exe, android.jar နဲ႕ additional library ေတြ ရွိတဲ့ေနရာကို ေရြးေပးရန္၉

Restart ADB Server

ADB server ကုိ restart လုပ္ရန္၉

Private Sign Key

Publish လုပ္မယ့္ application ကုိ sign လုပ္ရန္၉

Take Screenshot

Device/emulator ကို screenshot လုပ္ရန္၉ (ပံု ၀-၆)

Size ကိုခ်ံဳ႕/ခ်ဲ႕ရန္

ပံု ၀-၆

Take Picture

Device/emulator ကုိ screenshot လုပ္ရန္၉ PC မွာ device/emulator တစ္ခုထက္ ပိုခ်ိတ္

ထားရင္ ဘယ္ device/emulator ကို screenshot လုပ္မယ္ဆိုတာ ေရြးေပးရမယ္၉

49
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Save Picture

Screenshot ကုိ ဖုိင္တစ္ခုအျဖစ္ save လုပ္ရန္၉

Orientation

Screenshot ရဲ႕ orientation ကုိ ေျပာင္းရန္၉

တကယ္လုိ႕ IDE ဟာ debug mode မွာ ျဖစ္ေနရင္ ေတာ့ [Debug > Take Screenshot]

ကို ေရြးပါ၉ (ပံု ၀-၇)

ပံု ၀-၇

2.1.6. Help Menu

Online Help and Tutorials

Online မွ အကူအညီယူရန္နဲ႕ tutorial မ်ား ေလ့လာရန္၉

About

Version number နဲ႕အျခား information မ်ားကို ၾကည့္ရန္၉

50
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
2.1.7. Toolbar

Back

Forward

Bookmark ထည့္ရန္၉ ကိုယ္စိတှ


္ င္စားတဲ့ code line ကုိ ေနာက္တစ္ခါ ျပန္ၾကည့္ရ

လြယ္ေအာင္ bookmark နဲ႕ မွတ္သားထားလို႕ ရပါတယ္၉ Code line မွာ cursor ခ်ျပီး bookmark

icon ကို click ပါ၉ အဲဒီလုိင္းမွာ ေလးေပၚလာပါလိမ့္၉

Bookmark တစ္ခုခ်င္း ဖ်က္ရန္၉ မလိုေတာ့ရင္ bookmark ကုိ ဖ်က္ပစ္ႏိုင္ပါတယ္၉

Previous bookmark ကို ျပန္သြားရန္၉

Next bookmark သုိ႕သြားရန္၉

(Auto complete) လိ႕ု ေခၚပါတယ္၉ Ctrl + space bar ရိုက္တာနဲ႕တူတူပါဘဲ၉ ကို click

ရင္ B4A မွာသံုးသမွ် keyword ေတြကို ျပေပးတဲ့အတြက္ လိုအပ္တာ ေရြးယူလိုက္ယံုပါဘဲ၉ (ပံု ၀-ဿှ)

ပံု ၀-ဿှ

51
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

Indentation decrease Indentation increase

Code ေတြကို (ပံု ၀-ဿဿ) အတုိင္း ေရးတာဟာ ဖတ္ရခက္ခဲပါတယ္၉

ပံု ၀-ဿဿ

ဒါေၾကာင့္ indentation ကုိ သံုးျပီး ေရးသင့္ပါတယ္၉ (ပံု ၀-ဿ၀) ကို ၾကည့္ပါ၉ Tab size = 2

နဲ႕ ေရးထားတာပါ၉ Tab size ဆိုတာ tab key တစ္ခါႏွိပ္ရင္ ေရြ႕သြားမယ့္အကြာအေှးပါ၉ Tab size

သတ္မွတ္ရန္ [Tools > IDE Options > Tab Size] ကို ေရြးရမယ္၉

ပံု ၀-ဿ၀

52
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
တစ္လုိင္းထက္ပိုတဲ့ code ေတြကို indent လုပ္ခ်င္ရင္ code line ေတြကို (ပံု ၀-ဿ၁)

မွာကဲ့သို႕ select လုပ္ျပီး (Indentation Increase) သို႕မဟုတ္ (Indentation Decrease)

ကို click ပါ၉

ပံု ၀-ဿ၁

Compile & run

Compile & Run Mode

Compile & Run လုပ္တဲ့အခါ ဘယ္ mode နဲ႕ compile & run မယ္ဆိုတာ ေရြးေပးရမယ္၉

(ပံု ၀-ဿ၂)

ပံု ၀-ဿ၂

Program မွာ code ေတြ သိပ္မ်ားသြားရင္ ကိုယ္သြားခ်င္တဲ့ sub routine ကုိ ရွာရခက္မယ္၉

ဒီကေန ကိုယ္သြားခ်င္တဲ့ sub routine ကို ေရြးုလိုက္တာနဲ႕ အဲဒီ sub routine ဆီ ေရာက္သြားမယ္၉

ဥပမာ Process_Globals ကိုေရြးလိက


ု ရ
္ င္ cursor ဟာ Sub Process_Globals ကုိေရာက္သြားမယ္၉

53
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
2.2. Code Area

Project တုိင္းမွာ အနည္းဆံုး activity module တစ္ခု ရွိျပီး အမည္က „Main‟ ပါ၉ Project

attribute ေတြကို Main module မွာ သတ္မွတ္ေပးရပါတယ္၉ Toolbar ေအာက္မွာလည္း tab

ေတြပါတဲ့ row တစ္ခု ရွိတယ္၉ (ပံု ၀-ဿ၃)

Module တစ္ခုအတြက္ tab တစ္ခု ရွိတယ္၉ (Main module မွအပ က်န္ module မ်ားကို

[Project > Add Modules] ကေန ထည့္ထားမွသာ ျမင္ရမွာျဖစ္ပါတယ္) Tab တစ္ခုကိုေရြးလုိက္ရင္

သက္ဆုိင္ရာ module ရဲ႕ code ေတြကို editor (code area) မွာ ျမင္ရမယ္၉

ပံု ၀-ဿ၃

2.2.1. Code Header

Main module ထဲမွာ pre-defined Region ၀ခု ရွိပါတယ္၉ Project Attributes နဲ႕ Activity

Attributes ပါ၉

2.2.1.1. Project Attributes Region

Project Attributes မွာ သတ္မွတ္ေပးလိုက္တဲ့ attribute ေတြဟာ project မွာပါတဲ့ module

အားလံုးအတြကအ
္ က်ိဳးသက္ေရာက္မွာျဖစ္ပါတယ္၉

#Region Project Attributes

#ApplicationLabel: B4A Example

#VersionCode: 1

#VersionName:

'SupportedOrientations possible values: unspecified, landscape or portrait.

#SupportedOrientations: unspecified

#CanInstallToExternalStorage: False

#End Region

54
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ApplicationLabel: မွာသတ္မွတ္ေပးလိုက္တဲ့ text ဟာ application ကုိ install လုပ္ျပီးေနာက္

screen မွာျမင္ရမယ့္ text ျဖစ္ပါတယ္၉ ဥပမာ

#ApplicationLabel: GPSBasics

လို႕ေပးလိုက္ရင္ device မွာ (ပံု ၀-ဿ၄) ကဲ့သို႕ ျမင္ရမွာ ျဖစ္ပါတယ္၉

ပံု ၀-ဿ၄

VersionCode: ကိန္းျပည့္တစ္ခုေပးရမယ္၉ ဥပမာ

VersionCode: 1

VersionName: String ျဖစ္ရမယ္၉ ဥပမာ

1.1

1.0.1 အစရွသ
ိ ျဖင့္ေပးႏုင
ိ ပ
္ ါတယ္၉

SupportedOrientations: Application ကုိ portrait mode (ေထာင္လိုက)္ မွာဘဲ run လိ႕ု ရေအာင္

„portrait‟, landscape mode (အလ်ားလိုက)္ မွာဘဲ run လိ႕ု ရေအာင္ „landscape‟, mode

၀မ်ိဳးလုံးမွာ run ႏိုင္ေအာင္ „unspecified‟ လုိ႕ (ၾကိဳက္ရာတစ္ခ)ု သတ္မွတ္ေပးပါႏင


ို ပ
္ ါတယ္၉

CanInstallToExternalStorage: Application ကုိ external storage (SD card) မွာ install

လုပ္ႏုိင္ေစရန္အတြက္ True လို႕ ေပးပါ၉ မလုပ္ႏုိင္ေစခ်င္ရင္ False လို႕ ေပးပါ၉

55
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
2.2.1.2. Activity Attributes Region

Current activity ရဲ႕ attribute ေတြကို ဒီ region မွာ ေရးရပါတယ္၉

#Region Activity Attributes

#FullScreen: False

#IncludeTitle: True

#End Region

Project ထဲကို ေနာက္ထပ္ activity module ေတြ ထပ္ထည့္ႏုိင္ပါတယ္၉ Activity module

တစ္ခုစီ အတြက္ activity attributes ေတြ သီးျခားသတ္မွတ္ေပးႏုိင္ပါတယ္၉ Activity attributes ဟာ

သူ႕ကို define လုပ္ထားတဲ့ activity နဲ႕သာ သက္ဆိုင္ပါတယ္၉

#FullScreen: မွာ False ထားရင္ (ပံု ၀-ဿ၅) အတုိင္း Full screen မဟုတ္ေတာ့ဘဲ status bar ကုိပါ

ျမင္ေနရပါမယ္၉ True ထားရင္ေတာ့ status bar ကို မျမင္ရေတာ့ဘဲ full screen ျဖစ္ေနပါ မယ္၉

(ပံု ၀-ဿ၆)

#IncludeTitle: ကို True လို႕သတ္မွတ္ရင္ B4A Example ဆိုတဲ့ title ကုိ ျမင္ရပါမယ္၉ False

ဆိုရင္ေတာ့ (ပံု ၀-ဿ၇) အတုိင္း title မပါေတာ့ပါဘူး၉

ပံု ၀-ဿ၅ ပံု ၀-ဿ၆ ပံု ၀-ဿ၇

56
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ေနာက္ထပ္ attribute ေတြ ထပ္ထည့္ခ်င္ရင္ # ကုိရက


ို ္လိုက္တာနဲ႕ ထပ္ထည့္ႏုိင္တဲ့

attribute ေတြကို ျပပါလိမ့္မယ္၉ (ပံု ၀-၀ှ)

ပံု ၀-၀ှ

2.2.1.3. Service Attributes Region

Project ထဲမွာ service module တစ္ခု ထည့္လိုက္ရင္ service attributes header က

ေအာက္ပါအတုိင္း ရွိမယ္၉ Device ဖြငတ


့္ ာနဲ႕ service ကုိ run ေစခ်င္ရင္ StartAtBoot: True ထားပါ၉

#Region Service Attributes

#StartAtBoot: False

#End Region

2.2.2. Region

Predefined region ေတြအျပင္ ကိုယ္တုိင္လည္း region ေတြ လုပ္လို႕ရပါတယ္၉ Region

တစ္ခုမွာ sub routine တစ္ခုထက္မက ထည့္လို႕ရပါတယ္၉ ဥပမာ အေနနဲ႕ region တစ္ခုကို ျပထား

ပါတယ္၉ Region name က GPS ပါ၉ GPS region မွာ sub routine ၁ခု (Routine1, Routine2,

Routine3) ကိထ
ု ည့္ျပထားပါတယ္၉ Sub routine ထဲမွာ program code ေတြ ေရးရမွာပါ၉ (ပံု ၀-၀ဿ)

57
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၀-၀ဿ

2.2.3. Autocomplete

Autocomplete function ပါတာေၾကာင့္ key word ေတြကို အလြတ္မွတ္ထားစရာ

မလုပ
ိ ါဘူး၉ ကို္ယ္ေရးခ်င္တဲ့ keyword/variable name ရဲ႕ေရွ႕ဆံုးက စာလံုးအနည္းငယ္ရိုက္ျပီး Ctrl

+ space bar ပုတ္လုိက္ပါ၉ ဥပမာ ty လို႕ရိုက္ျပီး Ctrl + space bar ပုတ္လုိက္ပါ၉ Ty နဲ႕စတဲ့

variable, view နဲ႕ properties ေတြ အားလံုးကို ျပေပးတဲ့ popup menu ေပၚလာမယ္၉

လိုအပ္တာတစ္ခုကုိ ေရြးယူလိုက္ရံုပါဘဲ၉ (ပံု ၀-၀၀)

ပံု ၀-၀၀

58
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
2.2.3.1. Icons

အထက္ပါ popup menu မွာ icon ေတြကို ၾကည့္ျပီး ေအာက္ပါအတုိင္း ခြဲျခားႏိုင္ပါတယ္၉

Keyword/method

Key constant

Function

Global variable

Local variable

Property

2.2.3.2. Autocomplete Properties and Methods

Variable/object name တစ္ခုကို ေရးျပီးရင္ ( . ) ေလးတစ္ခု ေရးလိုက္ပါ၉ အဲဒီ Variable

/object နဲ႕ဆုိင္တဲ့ property ေတြ method ေတြကို popup menu မွာ ျပပါလိမ့္မယ္၉ ကိုယ္လိုအပ္

တာေရြးျပီး enter ေခါက္လုိက္ရံုပါဘဲ၉ (ပံု ၀-၀၁)

ပံု ၀-၀၁

59
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
2.2.3.3. Autocomplete Event Subroutines

Event subroutine ေတြေရးတဲ့အခါ လုိအပ္တဲ့ argument ေတြကုိ မွန္မွန္ကန္ကန္ ေရးႏုိင္

ဘို႕အတြက္ အေထာက္အကူ ရပါတယ္၉ Sub လို႕ရိုက္ျပီး space တစ္ခုျခားပါ၉ ျပီးရင္ tab key ကုိ

ႏွိပ္ပါ၉ (ပံု ၀-၀၂) အတုိင္းျမင္ရမယ္၉

ပံု ၀-၀၂

လိုအပ္တဲ့ type ကုိ ေရြးျပီး enter ေခါက္လုိက္ပါ (ဥပမာ Button)၉ ကိုယ္ေရြးလုိက္တဲ့ type

မွာ ရႏိင
ု ္တဲ့ event ေတြကို (ပံု ၀-၀၃) မွာကဲ့သို႕ ျမင္ရပါလိမ့္မယ္၉ လိုအပ္တာတစ္ခုကို ေရြးျပီး enter

ေခါက္လုိက္ပါ (ဥပမာ Click)

ပံု ၀-၀၃

ေအာက္ပါအတုိင္း event sub frame တစ္ခု ရလာမယ္၉

Sub EventName_Click

End Sub

60
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
EventName ေနရာမွာ အဲဒီ event ကုိျဖစ္ေပၚေစတဲ့ source object (ဥပမာ Button1) ရဲ႕

event name ကုိ ေရးေပးရမယ္၉ Event name နမူနာတစ္ခုကို (ပံု ၀-၀၄) မွာ ျပထားပါတယ္၉

ပံု ၀-၀၄

Button1 ရဲ႕ event name က btnCM ျဖစ္ပါတယ္၉ ဒါေၾကာင့္ Sub EventName_Click ရဲ႕

EventName ေနရာမွာ btnCM လို႕အစားထုိးေပးပါ၉ ေအာက္ပါအတုိင္း ျဖစ္သြားမယ္၉

Sub btnCM_Click

End Sub

2.2.4. Built in Documentation

Code area မွာ object name ေရးျပီး dot (.) ခ်လိုက္တာနဲ႕ (ပံု ၀-၀၅) မွာ ျပထား

တဲ့အတုိင္း ျမင္ရပါမယ္၉ ဥပမာ MailParser.p လို႕ေရးလုိက္တာနဲ႕ MailParser object ရဲ႕ p နဲ႕စတဲ့

method ေတြကုိ ျပေပးေနပါမယ္၉ လုိအပ္တာကို select လုပ္ျပီး enter ေခါက္လိုက္ပါ၉ သက္ဆုိင္ရာ

method ရဲ႕ ေရးသားပံုနမူနာကိုလည္း ျပေပးေနပါလိမ့္မယ္၉

61
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၀-၀၅
2.2.5. Context Menu (Popup Menu)

Code area ရဲ႕လြတ္တဲ့ေနရာမွာ right click လုပ္ရင္ (ပံု ၀-၀၆) မွာျမင္ရတဲ့အတုိင္း context

menu တစ္ခုေပၚလာပါလိမ့္မယ္၉

ပံု ၀-၀၆

2.2.5.1. Goto Line

Code လိုင္းတစ္ခုဆီ သြားခ်င္ရင္ သံုးပါတယ္၉ သြားခ်င္တဲ့ line number ကုိ ထည့္ေပးပါ၉

(ပံု ၀-၀၇)

62
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၀-၀၇

2.2.5.2. Goto Sub Declaration

Subroutine declaration လုပ္ထားတဲ့ ေနရာကို သြားခ်င္ရင္ subroutine ကို select လုပ္ျပီး

right click လုပ္ပါ၉ Popup menu ေပၚလာရင္ Goto Sub Declaration ကုိ ေရြးပါ၉ (ပံု ၀-၁ှ)

ပံု ၀-၁ှ

63
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
2.2.6. Highlighting Occurrences of Words

Code area မွာ စာလံုးတစ္လံုးကို select လုပ္လိုက္ရင္ သူနဲ႕တူတဲ့ စာလံုးအားလံုးကို high

light လုပ္ေပးပါတယ္၉ (ပံု ၀-၁ဿ) မွာ Lat ကုိ select လုပ္လိုက္တာနဲ႕ Lat ေတြ အားလံုးကို high

light လုပ္ျပေပးပါတယ္၉

ပံု ၀-၁ဿ

2.2.7. Breakpoints

Program ကုိ အစအဆံုး မ run ေစဘဲ breakpoint သံုးျပီး ရပ္ခ်င္တဲ့ code line မွာ ရပ္ခိုင္း

လို႕ ရပါတယ္၉ Debug (legacy) နဲ႕ Debug (rapid) mode တို႕မွာသာ သံုးႏုိင္ပါတယ္၉ ရပ္ခ်င္တဲ့

code line ေဘးက margin ေပၚမွာ click လိုက္ရင္ breakpoint တစ္ခု ထည့္ျပီးျဖစ္ပါတယ္

(ပံု ၀-၁၀)၉ Breakpoint တစ္ခုထက္မကလည္း ထည့္လို႕ရပါတယ္၉ Breakpoint ကုိ သံုးျပီး error ကို

step by step ရွာႏုိင္ပါတယ္၉ Breakpoint ကုိ Globals, Process_Globals နဲ႕ Activity_Pause

ေတြမွာေတာ့ ထားလို႕မရပါဘူး၉

ပံု ၀-၁၀

64
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
2.2.8. Color Picker

Color picker ကုိသံုးျပီး application အတြက္ စိတ္ၾကိဳက္အေရာင္ ေရြးခ်ယ္အသံုးျပဳႏုိင္

ပါတယ္၉ Code window မွာ လြတ္တဲ့ေနရာ right click လုပ္ပါ၉ Popup menu (ပံု ၀-၁၁) မွာ „Show

Color Picker‟ ကုိေရြးပါ၉

ပံု ၀-၁၁ ပံု ၀-၁၂

(ပံု ၀-၁၂) မွာ ျပထားသလို color picker တစ္ခု ေပၚလာမယ္၉ ၾကိဳက္ရာအေရာင္ကို

ေရြးလုိက္ပါ၉ ေရြးလုိက္တဲ့ color value ကုိ clipboard မွာ တင္ေပးထားပါလိမ့္မယ္၉ မိမိသံုးခ်င္တဲ့

ေနရာမွာ paste လုပ္ျပီး သံုးႏုိင္ပါျပီ၉

2.2.9. Debugging

Program ကုိ compile & run လုပ္ရာတြင္ mode ၂မ်ိဳး သံုးလို႕ရပါတယ္၉

 Debug

o Debug (legacy) mode

o Debug (rapid) mode

 Release

o Release mode

o Release (obfuscated)

65
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
2.2.9.1. Debug (legacy) Mode

အျခားကို ျဖန္႕ေှမယ့္ application ကုိ Debug (legacy) mode နဲ႕ မ run သင့္ပါဘူး၉

Debugging code ေတြ ပါေနတဲ့အတြက္ျဖစ္ပါတယ္၉

2.2.9.2. Debug (rapid) Mode

Project ကုိ Debug (rapid) mode မွာ run ျခင္းအားျဖင့္ run ေနစဥ္အခ်ိန္အတြင္းမွာဘဲ

program code ကုိ ျပင္ဆင္၈ save လုပ္ျပီး code အသစ္နဲ႕ဆက္ run ႏုိင္ပါတယ္၉

2.2.9.3. Release and Release (obfuscated) Mode

Application ကုိ ျဖန္႕ေှေတာ့မယ္ဆိုရင္ ေအာက္ပါ mode ၀မ်ိဳးအနက္က တစ္ခုခုနဲ႕ Run

ရမယ္၉

 Release

 Release (obfuscated)

Program ကုိ compile လုပ္စဥ္မွာ Java code ေတြကို generate လုပ္ေပးပါတယ္၉ Java

compiler က Java code ေတြကို compile လုပ္ျပီး Dalvik (Android byte code format) အျဖစ္

ေျပာင္းေပးပါတယ္၉ Delvik byte code ကေန Java code ကုိ ေျပာင္းႏုိင္တဲ့ (decompile လုပ္ႏိုင္တ)ဲ့

tool ေတြရွိပါတယ္၉ ဒီနည္းနဲ႕ ကိုယ့္ application ရဲ႕ code ေတြကို အျခားသူေတြ ရသြားႏုိင္ပါတယ္၉

Release (obfuscated) mode နဲ႕ run ျခင္းအားျဖင့္ decompiled code ေတြကို ဖတ္ရခက္ခဲေအာင္၈

ရႈပ္ေထြးသြားေအာင္ လုပ္ႏုိင္ပါတယ္၉

2.3. Detail Area

ေရြးထားတဲ့ tab ေပၚမူတည္ျပီး detail area မွာ ျပသေပးမွာ ျဖစ္ပါတယ္၉

2.4. Tabs

IDE ညာဘက္ေအာက္ေဒါင့္မွာ tab ၂ခုရွိပါတယ္၉

ပံု ၀-၁၃

66
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
2.4.1. Modules Tab

Project တစ္ခမ
ု ွာ module တစ္ခုမက ပါႏုိင္ပါတယ္၉ Modules tab ကိုေရြးလုိက္ရင္ project

အတြင္း ရွိသမွ် module ေတြကို detail area မွာ ျပေပးပါလိမ့္မယ္၉ Module name ေတြေအာက္မွာ

module မွာပါတဲ့ subroutine ေတြကိုလည္း ျပပါလိမ့္မယ္၉ (ပံု ၀-၁၄)

ပံု ၀-၁၄

Module တစ္ခုကို click လုိက္ရင္ အဲဒီ module ရဲ႕ code ေတြကို code window မွာ

ျပပါလိမ့္မယ္၉ Subroutine တစ္ခုကို click လုိက္ရင္ cursor ဟာ အဲဒီ subroutine code ေတြဆီကုိ

ေရာက္သြားပါလိမ့္မယ္၉

2.4.2. Files Tab (File Manager)

Project ထဲမွာထည့္ထားတဲ့ file ေတြကိုျပပါလိမ့္မယ္၉ (ပံု ၀-၁၅)

ပံု ၀-၁၅

67
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Project ထဲ ဖုိင္ေတြထည့္ခ်င္ရင္ Add Files ကို click ပါ၉ မည္သည့္ဖုိင္အမ်ိဳးအစား (layout,

image, text, …) မဆိုထည့္ႏိုင္ပါတယ္၉ ဒီကေနထည့္လိုက္တဲ့ဖုိင္ေတြကို Files.DirAssets folder

ထဲမွာ သိမ္းေပးသြားပါလိမ့္မယ္၉ ထည့္ထားတဲ့ဖုိင္ကို ျပန္ဖ်က္ခ်င္ရင္ေတာ့ ဖ်က္ခ်င္တဲ့ဖင


ုိ ္ကို ေရြးျပီး

Remove Selected button ကို click ပါ၉

2.4.3. Logs Tab

Program ကို run ေနစဥ္မွာ program က generate လုပ္လိုက္တဲ့ comment ေတြကို Logs

tab ရဲ႕ detail area မွာျပပါတယ္၉ Program မွာ Log (“Hello”) လို႕ေရးျပီး run ရင္ detail area မွာ

Hello လို႕ျပပါလိမ့္မယ္၉

ပံု ၀-၁၆

 Filter ကို check လုပ္ထားရင္ ကိုယ့္ program နဲ႕ဆုိင္တဲ့ message ေတြကိုသာ ျမင္ရမွာပါ၉

Uncheck လုပ္ထားရင္ေတာ့ အားလံုးနဲ႕ဆုိင္တဲ့ message ေတြကို ျမင္ရမယ္၉

 Program ကုိ run ျပီး Logs ရဲ႕ detail area မွာ ဘာမွမျမင္ရေသးရင္ „Connect‟ button ကုိ

ႏွိပ္ပါ၉

 Detail area ကုိ clear လုပ္ရန္ Clear button ကုိ ႏွိပ္ပါ၉

2.4.4. Libs Tab

B4A မွာ configure လုပ္ထားတဲ့ library file ေတြကို ျပပါလိမ့္မယ္၉ (ပံု ၀-၁၇)

ပံု ၀-၁၇

ကိုယ့္ project မွာသံုးမယ့္ library ေတြကို check လုပ္ရပါမယ္၉

68
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

Chapter 3

BASIC Language

Android ဆိုတာ mobile device ေတြမွာသံုးတဲ့ software stack တစ္ခုပါ။ Android

software မွာ operating system, middleware နဲ႕ key application ေတြပါှင္ပါတယ္။ Android

ကိုစတင္ develop လုပ္ခဲ့တာကေတာ့ Android Inc. ပါ။ 2005 ခုႏွစ္မွာ Google Inc. ဟာ Android

ကိှ
ု ယ္ယူခပ
ဲ့ ါတယ္။

BASIC (Beginner's All-purpose Symbolic Instruction Code) programming

language ဟာ beginner မ်ားအတြက္လြယ္ကူေစရန္ design လုပ္ထားတဲ့ high level language

တစ္ခုျဖစ္ပါတယ္။ Baic4android ဟာ BASIC language ကုိအသံုးျပဳျပီး Android application

မ်ားကိုအလြယ္တကူေရးႏုိင္ေအာင္ design လုပ္ထားတဲ့ application development tool

တစ္ခုျဖစ္ပါတယ္။

3.1. Variable and Object

Program ထဲမွာအသံုးျပဳတဲ့ data ေတြကိုသိမ္းဘို႕ေနရာလိုပါတယ္။ Data ေတြကို memory

ေပၚမွာသိမ္းရပါတယ္။ Memory ေပၚမွာ data သိမ္းမယ့္ေနရာကို အမည္တစ္ခုေပးရပါတယ္။

သိမ္းမယ့္ data type ကုိလည္းသတ္မွတ္ေပးရပါတယ္။ အဲဒီလို name နဲ႕ data type

သတ္မွတ္ေပးျခင္းကို variable declaration လုပ္တယ္လို႕ေခၚပါတယ္။ သိမ္းမယ့္ data တိုင္းအတြက္

variable declaration လုပ္ရပါတယ္။

Variable ကို declare လုပ္ပံုကေတာ့ ေအာက္ပါအတုိင္းျဖစ္ပါတယ္။

Dim A As Int

A ဟာ variable name ျဖစ္ျပီး Int ဆိုတာကေတာ့ integer data type

အတြက္သတ္မွတ္ထားတဲ့ key word ပါ။ A မွာ ထည့္မယ့္ data type ဟာ integer data type

ျဖစ္တယ္လို႕ declare လုပ္လိုက္တာပါ။ Variable ထဲမွာ data ကို assign မလုပ္မီ variable ကို အရင္

declare လုပ္ရပါတယ္။ (assign လုပ္တယ္ဆိုတာ variable ထဲကို data ထည့္တာပါ)

70
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Variable type ၀မ်ိဳးရွိပါတယ္။

 Primitive နဲ႕

 Non-primitive ျဖစ္ပါတယ္။

Primitives type မွာ numberic type ေတြျဖစ္တဲ့

 Byte, Short, Int, Long, Float နဲ႕ Double တုိ႕အျပင္

 Boolean နဲ႕ Char လည္းပါပါတယ္။

3.1.1. Variable Type

ဇယား (၁-ဿ)

Boolean type မွာ value ၀မ်ိဳးထဲရွိပါတယ္။ True နဲ႕ False ပါ။

Byte, Shrot, Int, Long တို႕ဟာ integer type ေတြျဖစ္ပါတယ္။ Integer ဆိုတာ

ကိန္းျပည့္ကိုေျပာတာပါ။ ဥပမာ 1, 3, 5 အစရွိသည္တုိ႕ဟာ inter type ေတြျဖစ္ပါတယ္။

71
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Byte type အျဖစ္ declare လုပ္ထားတဲ့ variable ဟာ 8 bit (-128 ကေန 127 ထိ)

သိမ္းေပးႏုိင္ပါတယ္။ Float နဲ႕ Double က ဒႆမကိန္းေတြျဖစ္ပါတယ္။ Char ဆိုတာ character ပါ။

ဥပမာ A to Z, a to z, 0 to 9 နဲ႕အျခား character အားလံုးဟာ Char type ျဖစ္ပါတယ္။ Char type

variable မွာ character တစ္ခုသာသိမ္းႏုိင္ပါတယ္။

Primitive type ေတြဟာ အျခား sub ေတြကို ေခၚတဲ့အခါ၊ ဒါမွမဟုတ္ အျခား variable

တစ္ခုထဲကို assign လုပ္တဲ့အခါ variable ထဲမွာရွိတဲ့ value ကို pass လုပ္ပါတယ္။ ဥပမာ

Sub S1 Sub S2 (B As Int)

Dim A As Int Log (B * 2)

A = 12 End Sub

S2(A)

Log(A)

End Sub

အထက္ပါ ဥပမာ မွာ subroutine ၀ခုျပထားပါတယ္။ S1 နဲ႕ S2 ဟာ subroutine name

ေတြျဖစ္ပါတယ္။

S1 ထဲမွာ A ကုိ Int အျဖစ္ declare လုပ္ပါတယ္။

Dim A As Int

12 ကုိ A ထဲ assign လုပ္ပါတယ္။ A ထဲမွာ 12 ေရာက္သြားပါျပီ။

A = 12

Sub routine S2 ကို ေခၚပါတယ္။

S2(A)

အဲဒီအခါ A ထဲက value 12 ကို S2 ဆီပို႕လိုက္ပါတယ္။ S2 ကုိ ေခၚလုိက္တဲ့အတြက္ S2

ထဲက code ေတြကို execute လုပ္ပါေတာ့မယ္။

S1 ကပို႕လိုက္တဲ့ 12 ကုိလက္ခံဘုိ႕ S2 မွာ variable တစ္ခု declare လုပ္ရမယ္။ ဒါေၾကာင့္

S2 ေနာက္က ကြင္းထဲမွာ 12 ကုိလက္ခံဘို႕ integer variable တစ္ခု declare လုပ္ေပးရပါတယ္။

72
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
(B As Int)

ခုဆိုရင္ B ထဲမွာ 12 ေရာက္သြားပါျပီ။ B ကို 2 နဲ႕ေျမွာက္ျပီးရလာဒ္ကိုထုတ္ၾကည့္ပါ။

Log (B * 2)

Log area မွာ 24 ကိုျပပါလိမ့္မယ္။

ျပီးရင္ S2 မွာဆက္လုပ္စရာ code မရွိေတာ့တဲ့အတြက္ S1 ရဲ႕ က်န္ေနတဲ့ code

ေတြကိုဆက္လုပ္ပါမယ္။

Log (A)

Log area မွာ 12 လို႕ျပပါလိမ့္မယ္။ A ထဲမွာမူလက 12 ရွိပါတယ္။ S2

ဆီပို႕ျပီးေနာက္မွာလည္း A ထဲမွာ 12 ဘဲျဖစ္ပါတယ္။ အထက္မွာေျပာခဲ့သလို primitive type ေတြဟာ

အျခား sub ေတြဆီ variable ကို pass လုပ္တဲ့အခါ variable ထဲက value ကို copy

လုပ္ျပီးပို႕တာျဖစ္ေၾကာင္းသိေစခ်င္တာပါ။ S2 မွာ 12 ကုိ 2 နဲ႕ေျမွာက္လုိက္ေပမယ့္ S1

မွာေျပာင္းလည္းျခင္းမရွိဘဲ မူလအတုိင္း 12 ဘဲျဖစ္ေနပါတယ္။ Variable A ထဲကမူလတန္ဘိုး

ေျပာင္းလည္းျခင္းမရွိပါဘူး။ (Example 3.1 မွာၾကည့္ပါ)

Example 3.1

#Region Project Attributes

#ApplicationLabel: B4A Example

#VersionCode: 1

#VersionName:

#SupportedOrientations: unspecified

#CanInstallToExternalStorage: False

#End Region

#Region Activity Attributes

#FullScreen: False

73
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
#IncludeTitle: True

#End Region

Sub Process_Globals

End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)

S1

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub S1

Dim A As Int

A = 12

S2(A)

Log (A)

End Sub

74
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub S2(B As Int)

Log (B * 2)

End Sub

Object, array နဲ႕ string တုိ႕ကေတာ့ non-primitives type ေတြျဖစ္ပါတယ္။ Non-

primitive type variable ေတြကို sub တစ္ခုဆီ pass လုပ္တဲ့အခါမွာျဖစ္ေစ၊ variable တစ္ခုထဲကို

assign လုပ္တဲ့အခါျဖစ္ေစ value ကုိ pass လုပ္တာမဟုတ္ေတာ့ဘဲ reference ကိုသာ pass

လုပ္ပါတယ္။ Reference ဆုိတာ variable တည္ရွိရာ memory address ျဖစ္ပါတယ္။ S1 ကေန S2

ဆီ array တစ္ခုကို pass လုပ္ပါ။ S2 ထဲမွာ array value ကိုေျပာင္းလိုက္တာနဲ႕ S1 ထဲက array

မွာပါလိုက္ေျပာင္းသြားပါလိမ့္မယ္။ (Example 3.2 မွာၾကည့္ပါ)

Example 3.2

#Region Project Attributes

#ApplicationLabel: B4A Example

#VersionCode: 1

#VersionName:

#SupportedOrientations: unspecified

#CanInstallToExternalStorage: False

#End Region

#Region Activity Attributes

#FullScreen: False

#IncludeTitle: True

#End Region

75
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub Process_Globals

End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)

S1

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub S1

Dim A(5) As Int

A = Array As Int(1, 2, 3, 4, 5)

S2(A)

Log (A(0))

End Sub

Sub S2(B() As Int)

B(0) = B(0) * 2

76
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Log (B(0))

End Sub

အထက္ပါ program ကုိ run ရင္ မူလက A(0) မွာ 1 ျဖစ္ပါတယ္။ S2 ဆီ pass လုပ္လိုက္ရင္

B(0) မွာ 1 ေရာက္သြားပါမယ္။ B(0) ကို 2 နဲ႕ေျမွာက္ျပီး B(0) မွာျပန္ထည့္ပါ။ အခုဆိုရင္ B(0) မွာ 2

ျဖစ္သြားပါျပီ။ S1 ရဲ႕ A(0) မွာလည္း 2 ျဖစ္သြားပါတယ္။ S2 မွာ B(0) ကုိ 2 နဲ႕ေျမွာက္လက


ုိ ္ျခင္းဟာ

S1 ရဲ႕ A(0) တန္ဘုိးကိုပါေျပာင္းလည္းသြားေစပါတယ္။

3.1.2. Name of Variable

Variable name ကုိၾကိဳက္ရာေပးႏုိင္ပါတယ္။ ဒါေပမယ့္ reserved word ေတြ နဲ႕ key word

ေတြေတာ့မေပးရပါဘူး။ ဥပမာ variable name ကုိ Int, Sub အစရွိတဲ့ B4A မွာသံုးေနတဲ့ key word

ေတြေပးလို႕မရပါဘူး။ Variable name ဟာ letter နဲ႕သာစရမယ္။ A-Z, a-z, 0-9 နဲ႕ _

(underscore) တို႕ကိုသံုးႏုိင္ပါတယ္။ Variable name ေတြမွာ space မပါရဘူး၊ ကြင္းေတြမပါရပါဘူး။

အဓိပၸါယ္ရွိတဲ့ name ေတြသာေပးသင့္ပါတယ္။ ဥပမာ ေမြးေန႕ကိုသိမ္းမယ့္ variable name ကုိ

birthdate လို႕ေပးတာမ်ိဳးဘဲျဖစ္သင့္ပါတယ္။

3.1.3. Declaring Variable

3.1.3.1. Simple Variable

Variable declare လုပ္တဲ့အခါ Dim ဆိုတဲ့ keyword ေနာက္မွာ variable name နဲ႕ As

ဆိုတဲ့ keyword ျပီးရင္ variable type ကုိေရးျပီး declare လုပ္ရပါတယ္။ ဥပမာ

Dim Capital As Double

Dim Interest As Double

Dim Rate As Double

Dim i As Int

Dim j As Int

77
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Dim k As Int

Dim ch As Char

Dim edtCapital As EditText

Dim edtInterest As EditText

Dim edtRate As EditText

Dim btnNext As Button

Dim btnPrev As Button

Type တူတဲ့ variable ေတြကိုေအာက္ပါအတုိင္းေပါင္းျပီး declare လုပ္ႏုိင္ပါတယ္။ ဥပမာ

Dim Capital, Interest, Rate As Double

Dim i, j, k As Int

Dim edtCapital, edtInterest, edtRate As EditText

Dim btnNext, btnPrev As Button

Variable declare လုပ္ရင္းနဲ႕ value ကုိပါ assign လုပ္လို႕ရပါတယ္။ ဥပမာ

Dim i = 0, j = 2, k = 5 As Int

Dim txt = "test" As String, value = 1.05 As Double, flag = False As Boolean

Code ထဲမွာသံုးမယ့္ view ေတြကို declare လုပ္ေပးရမယ္။ ဥပမာ EditText view

တစ္ခုျဖစ္တဲ့ edtCaptial ကုိ code ထဲမွာသံုးမယ္ဆိုရင္

Dim edtCaptial As EditText

လို႕ declare လုပ္ရမယ္။ Code ထဲမွာမသံုးတဲ့ view ေတြကိုေတာ့ declare လုပ္စရာမလိုပါဘူး။

Value တစ္ခုကို variable ထဲ assign လုပ္ခ်င္ရင္ variable name ကိုအရင္ေရးျပီး „=‟ ေရးပါ။

ျပီးရင္ assign လုပ္ခ်င္တဲ့ value ကုိေရးပါ။ ဥပမာ

78
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Capital = 1200.00

LastName = "SMITH"

ch = „A‟

Capital မွာေတာ့ 1200.00 လို႕ဘဲေရးပါတယ္။ ဘာေၾကာင့္လည္းဆိုေတာ့ 1200.00 ဟာ

numeric value ျဖစ္လို႕ပါ။ ဒါေပမယ့္ LastName ထဲကို assign လုပ္တဲ့ SMITH ကုိေတာ့ “”

ထဲမွာေရးပါတယ္။ ဘာေၾကာင့္လည္းဆိုေတာ့ SMITH က string ျဖစ္လို႕ပါ။ String ေတြကို “”

ထဲမွာဘဲအျမဲေရးရမယ္။ Character ဆိုရင္ „‟ ထဲမွာေရးရပါမယ္။

3.1.3.2. Array Variable

Arrays ဆိုတာ data/object ေတြကိုစုျပီး အမည္တစ္ခုထဲေပးထားတဲ့ variable ပါဘဲ။ Array

ထဲက data/object ေတြကို ညြန္းခ်င္တဲ့အခါ index ကိုသံုးရပါတယ္။ Array ရဲ႕ပထမဆံုး element

အတြက္ index က 0 ျဖစ္ပါတယ္။ ဒါေၾကာင့္ ပထမဆံုး element ဟာ one dimension မွာဆို

ArrayName(0), two dimension မွာဆိုရင္ ArrayName (0,0), three dimension မွာဆိုရင္

ArrayName (0,0,0) မွာရွိပါတယ္။ ေနာက္ဆံုး element ရဲ႕ index ဟာ (စုစုေပါင္း element

အေရအတြက္ - 1) ျဖစ္ပါတယ္။ ဥပမာ element 5 ခုရွိရင္ ေနာက္ဆံုး element ရဲ႕ index ဟာ (5 –

1) = 4 ျဖစ္ပါတယ္။ Array declare လုပ္ပံုကေတာ့

Dim ArrayName (NumberofElements) As Type

One Dimensional Array

Element 5 ခုပါတဲ့ string array ဆိုရင္

Dim Name(5) As String

လို႕ declare လုပ္ရမယ္။ Array index ဟာ 0 ကေနစပါတယ္။ (ပံု ၁-ဿ)

Name(0) Name(1) Name(2) Name(3) Name(4)

ပံု ၁-ဿ

79
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Two Dimensional Array

Row ၁ခု၊ column ၁ခု ပါတဲ့ two dimensional array နမူနာတစ္ခုကို (ပံု ၁-၀)

မွာျပထားပါတယ္။ Declare လုပ္ရမယ့္ပံုစံကေတာ့

Dim Matrix(3, 3) As Double

0 1 2 Matrix (2, 1)

ပံု ၁-၀

Three Dimensional Array

ပံု ၁-၁

80
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Three dimensional array နမူနာတစ္ခုကို (ပံု ၁-၁) မွာၾကည့္ပါ။ Declare လုပ္ရမယ့္

ပံုစက
ံ ေတာ့ ေအာက္ပါအတုိင္းျဖစ္ပါတယ္။

Dim Data(3, 3, 3) As Int

Array ကုိေအာက္ပါအတိုင္း declare လုပ္ႏုိင္ပါတယ္။

Dim LastName(10) As String

Dim FirstName(10) As String

Dim Address(10) As String

Dim City(10) As String

သို႕မဟုတ္

Dim LastName(10), FirstName(10), Address(10), City(10) As String

လို႕လည္းေပါင္းေရးႏိုင္ပါတယ္။

Array ကုိေအာက္ပါပံုစံမ်ိဳးနဲ႕လည္း declare လုပ္ႏုိင္ပါေသးတယ္။

Dim NbPers As Int : NbPers = 10

Dim LastName(NbPers) As String

Dim FirstName(NbPers) As String

Dim Address(NbPers) As String

Dim City(NbPers) As String

NbPers ကုိ Int အျဖစ္ declare လုပ္ျပီးေနာက္ NbPers ထဲမွာ 10 ကို assign

လုပ္လုိက္ပါတယ္။

Dim NbPers As Int : NbPers = 10

တစ္လုိင္းထဲမွာ statement ၀ခုေရးတာမုိ႕ full column (:) ျခားေပးရပါတယ္။

81
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Dim LastName(NbPers) As String Dim LastName(10) As String

Dim FirstName(NbPers) As String Dim FirstName(10) As String

Dim Address(NbPers) As String Dim Address(10) As String

Dim City(NbPers) As String Dim City(10) As String

သည္ နဲ႕တူတူဘဲျဖစ္ပါတယ္။

Dim NbX As Int : NbX = 2 Dim Data(2, 5, 10) As Int

Dim NbY As Int : NbY = 5 နဲ႕တူတူဘဲျဖစ္ပါတယ္။

Dim NbZ As Int : NbZ = 10

Dim Data(NbX, NbY, NbZ) As Int

သည္

Array ထဲကို data ေတြ ထည့္ခ်င္ရင္ေအာက္ပါအတုိင္း Array keyword ကိုသံုးျပီးလုပ္ရမယ္။

Dim Name() As String

Name = Array As String("Miller", "Smith", "Johnson", "Jordan")

Array „Name‟ ထဲမွာ data ေတြရွိေနမယ့္ အေနအထားက ေအာက္ပါအတုိင္းျဖစ္ပါတယ္။

Name(0) Name(1) Name(2) Name(3)

Miller Smith Johnson Jordan

3.1.3.3. Array of Views (Objects)

Array ထဲမွာ view ေတြေရာ object ေတြပါထည့္ႏုိင္ပါတယ္။ ေအာက္ပါ ဥပမာမွာ button

view ေတြကို Buttons() array ထဲထည့္ပါမယ္။ အရင္ဆံုး button ေတြကို declare လုပ္ရမယ္။

Dim b1, b2, b3, b4, b5, b6, b7 As Button

Button type array ကုိ declare လုပ္မယ္။

82
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Dim Buttons() As Button

ျပီးရင္ Button() array ထဲကို button view ေတြထည့္ပါမယ္။

Buttons = Array As Button(b1, b2, b3, b4, b5, b6, b7)

3.1.3.4. Type Variable

„Type‟ keyword ကိုသံုးျပီး ကိုယ္ပိုင္ data type (structure) ေတြ create လုပ္ႏိုင္ပါတယ္။

Structure ဆိုတာ variable ေတြကို အုပ္စုဖြဲ႕ျပီး ကိုယ္ပိုင္ data type တစ္ခုသတ္မွတ္ေပးထားတာပါ။

Type variable တစ္ခုကို ဘယ္ module ကမဆိုေခၚသံုးႏုိင္ပါတယ္။ ဒါေၾကာင့္ Main module ထဲက

Process_Globals routine ထဲမွာ declare လုပ္သင့္ပါတယ္။

Type variable အသံုးျပဳပံုကို ဥပမာတစ္ခုနဲ႕ျပပါမယ္။

LastName, FirstName, Address, City အစရိွတဲ့ variable ေတြကို ေပါင္းျပီး „Person‟ type

အျဖစ္သတ္မွတ္လုိက္ပါတယ္။

Type Person(LastName As String, FirstName As String, Address AsString, City As

String)

ျပီးရင္ variable၀ခု User1 နဲ႕ User2 ကို Person type variable အျဖစ္ declare လုပ္မယ္။

Dim User1, User2 As Person

User1 မွာေအာက္ပါအတိုင္း data ေတြ assign လုပ္ပါမယ္။

User1.LastName = “Tun”

User1.FirstName = “Lwin”

User1.Address = “No. 1, Pyay Road”

User1.City = “Yangon”

83
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
User2 မွာလည္းေအာက္ပါအတုိင္း data ေတြ assign လုပ္ပါမယ္။

User1.LastName = “Sein”

User1.FirstName = “Than”

User1.Address = “No. 1, Bogyoke Road”

User1.City = “Bago”

Array ကုိလည္း Type variable အျဖစ္ declare လုပ္လို႕ရပါတယ္။

Dim Student(10) As Person

Data assign လုပ္ပံုကေတာ့ (ဥပမာ)

Student(1).LastName = “Maung”

Student(1).FirstName = “Aye”

Student(2).LastName = “Phyu”

Student(2).FirstName = “Win”

Type variable တစ္ခုကို (type တူတ)ဲ့ အျခား variable တစ္ခုထဲ assign လုပ္ႏုိင္ပါတယ္။

ဥပမာ

Dim IT As Person

IT= Student(1)

(IT ေရာ Student ပါ „Person‟ type ျဖစ္တာမုိ႕ထည့္ႏုိင္တာပါ)

84
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
3.1.4. Casting

Basic4android မွာလိုအပ္ရင္လိုအပ္သလို variable type ေတြကို auto cast

လုပ္ေပးႏုိင္ပါတယ္။ Cast လုပ္တယ္ဆိုတာ variable type ေျပာင္းေပးတာပါ။ Number type ကေန

string type ကုိေျပာင္းေပးႏုိင္တယ္။ String type ကေနလည္း number type

ကိုေျပာင္းေပးႏိုင္တယ္။ တစ္ခါတေလမွာ object တစ္ခုကို အျခား type တစ္ခုအျဖစ္ explicitly cast

လုပ္ေပးဘို႕လိုအပ္တတ္ပါတယ္။ Baisc4android ဟာ object type ကေန အျခား type

ေျပာင္းဘုိ႕ကိုေတာ့ auto cast လုပ္ေပးမွာမဟုတ္လို႕ပါ။ Explicitly cast လုပ္တယ္ဆိုတာ

programmer ကုယ
ိ ္တိုင္ code ေရးျပီး cast လုပ္ေပးျခင္းကိုေခၚပါတယ္။ လုပ္နည္းကေတာ့

ေျပာင္းခ်င္တဲ့ type နဲ႕ declare လုပ္ထားတဲ့ variable ထဲ object ကို assign လုပ္ရန္သာျဖစ္ပါတယ္။

ဥပမာ Sender ဆိုတာ object တစ္ခုျဖစ္ပါတယ္။ ဒီ object ကုိ Button type variable ထဲကို assign

လုပ္ျခင္းျဖင့္ Sender object ကို Button type အျဖစ္ cast လုပ္လိုက္ပါတယ္။ ဒါကို explicitly cast

လုပ္တယ္လို႕ေခၚပါတယ္။

Dim btn As Button

btn = Sender

3.1.5. Scope

3.1.5.1. Process Variable

Application တစ္ခုကို run လုိက္ရင္ memory ေပၚမွာ process တစ္ခုအျဖစ္တည္ရွိေနမွာပါ။

Process variable ေတြဟာ process ရွိေနသမွ်ရွိေနမွာပါ။ Process variable ေတြကို sub

Process_Globals မွာ declare လုပ္ရပါမယ္။ Process variable ေတြကို အျခား module

ေတြကပါသံုးႏုိင္ပါတယ္။ ဒါေၾကာင့္ public variable လို႕ေခၚပါတယ္။ Object တိုင္းကိုေတာ့ process

variable အျဖစ္ declare မလုပ္ႏုိင္ပါဘူး။ ဥပမာ view ေတြကို process variable အျဖစ္ declare

မလုပ္ႏုိင္ပါဘူး။ ဘာေၾကာင့္လည္းဆိုေတာ့ activity တစ္ခု destroy ျဖစ္သြားတဲ့အခါ activity

ထဲမွာပါတဲ့ view ေတြဟာလည္း destroy ျဖစ္သြားမွာပါ။ (Process ကေတာ့ဆက္ျပီးရွိေနမွာပါ)

85
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Activity တစ္ခု destroy ျဖစ္သြားရင္ garbage collector က activity ထဲက view ေတြကိုပါ

destroy လုပ္ျပီး memory ေနရာလြတ္ေတြရရွိေအာင္ေဆာင္ရြက္ေပးပါတယ္။ View ေတြကိုသာ

Process_Globals မွာ declare လုပ္ထားရင္ garbage collector က view ေတြကို destroy

လုပ္လို႕မရေတာ့ပါဘူး။ အဲဒါဆုိရင္ သံုးမရေတာ့တဲ့ view ေတြကို memory မွာဆက္ျပီးေနရာ

ေပးထားရတဲ့အတြက္ memory leak ျဖစ္ေစပါတယ္။

Process global variables ေတြကို အျခား module ထဲမွာေခၚသံုးတဲ့အခါ module name

ပါထည့္ေပးရမယ္။ ဥပမာ Main module ထဲမွာ process variable MyVar ကို ေအာက္ပါအတုိင္း

declare လုပ္ထားတယ္ဆုိပါေတာ့။

Sub Process_Globals

Dim MyVar As String

End Sub

Main module ထဲမွာ MyVar ကိုသံုးခ်င္ရင္ ေအာက္ပါအတုိင္းသံုးႏုိင္ပါတယ္။

MyVar = "Text"

ဒါေပမယ့္ အျခား module ထဲမွာ MyVar ကိုသံုးခ်င္ရင္ေတာ့

Main.MyVar = "Text"

လို႕ေရးရမယ္။

3.1.5.2. Activity Variable

Activity မွာသာအသံုးျပဳမယ့္ variable ေတြကို activity variable လို႕ေခၚပါတယ္။ Activity

variable ေတြကို Sub Globals အတြင္းမွာ declare လုပ္ရပါတယ္။ သူ႕ကို declare လုပ္ထားတဲ့

activity အတြင္းမွာဘဲသံုးႏုင
ိ ္တာေၾကာင့္ private variable လို႕ေခၚပါတယ္။ Activity ရွိေနသမွ် ဒီ

variable ေတြတည္ရွိေနမွာပါ။

86
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
3.1.5.3. Local Variable

Subroutine တစ္ခုအတြင္းမွာ declare လုပ္တဲ့ variable ေတြကို အဲဒီ subroutine

အတြင္းမွာသာသံုးႏုိင္ပါတယ္။ Local variable လို႕ေခၚပါတယ္။

3.2. Program Flow

Project တစ္ခုကို create လုပလ


္ ိုက္တာနဲ႕ module တစ္ခုကို create လုပ္ျပီးျဖစ္ပါတယ္။

အဲဒီ module ကေတာ့ activity module ပါ။ Module ရဲ႕အမည္ကိုေတာ့ Main လို႕ေခၚပါတယ္။

Activity module မွာေအာက္ပါ region ေတြနဲ႕ subroutine ေတြပါဝင္ပါတယ္။ Program ရဲ႕ flow

ကေတာ့ ေအာက္မွာေဖာ္ျပထားတဲ့ subroutine အစဥ္အတိုင္း ျဖစ္ပါတယ္။

#Region Project Attributes

#ApplicationLabel: B4A Example

#VersionCode: 1

#VersionName:

'SupportedOrientations possible values: unspecified, landscape or portrait.

#SupportedOrientations: unspecified

#CanInstallToExternalStorage: False

#End Region

#Region Activity Attributes

#FullScreen: False

#IncludeTitle: True

#End Region

Sub Process_Globals

End Sub

87
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed AsBoolean)

End Sub

3.3. Expression

Expression ဆိုတာ value, constant, variable, operator နဲ႕ function ေတြပါှင္တဲ့

ေဖာ္ျပခ်က္တစ္ခုပါ။ ဥပမာ „2 + 3‟ ဆိုတာ expression တစ္ခုေပါ့။ ဒီ expression ကုိ evaluate

လုပ္လိုက္ရင္ 5 ဆိုတဲ့ value ကုိရပါတယ္။ „y + 6‟ ဆိုတာလည္း expression တစ္ခုပါဘဲ။ Variable

„y‟ ထဲမွာရွိတဲ့ value နဲ႕ 6 ကုိေပါင္းတဲ့ expression ေပ့ါ။ „4 = 4‟ ဆိုတာလည္း expression

တစ္ခုပါဘဲ။ „4 = 4‟ expression ကုိ evaluate လုပ္လိုက္ရင္ True ဆိုတဲ့အေျဖကိုရပါတယ္။

3.3.1. Mathematical Expression

ဇယား (၁-၀) မွာ mathematical operator ေတြကိုေဖာ္ျပထားပါတယ္။

88
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ဇယား (၁-၀)

Precedence level ဆိုတာကေတာ့ expression တစ္ခုမွာ ဘယ္ operator ျပီးရင္ ဘယ္

operator ကိုေဆာင္ရြကရ
္ မယ္ဆိုတဲ့ သတ္မွတ္ခ်က္ျဖစ္ပါတယ္။ Level 1 operator ကုိအရင္

လုပ္ရပါတယ္။ ျပီးရင္ level 2, ျပီးမွ level 3 operator ကိုအစဥ္အတုိင္း ေဆာင္ရြက္ေပးရပါတယ္။

ေအာက္ပါ example ကိုၾကည့္ပါ။

3.3.2. Relational Expression

Relational operator ေတြကေတာ့ =, <>, >, <, >=, <= တုိ႕ျဖစ္ပါတယ္။

89
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Equal to (=)

x=y

(x ရဲ႕ value နဲ႕ y ရဲ႕ value ညီသလားစစ္တာပါ။ ညီရင္ evaluate result က True ေပါ့)

Not Equal to (<>)

x<>y

(x ရဲ႕ value နဲ႕ y ရဲ႕ value မညီဘူးလားဆိုတာစစ္တာပါ။ မညီရင္ evaluate result က True ေပါ့)

Greater than (>)

x> y

(x ဟာ y ထက္ၾကီးသလားစစ္တာပါ။ ၾကီးရင္ evaluate result က True ေပါ့)

Less than (<)

x< y

(x ဟာ y ထက္ငယ္သလားစစ္တာပါ။ ငယ္ရင္ evaluate result က True ေပါ့)

Greater than or equal to (>=)

x >= y

(x ဟာ y ထက္ၾကီးသလား/ညီသလားစစ္တာပါ။ ၾကီး/ညီရင္ evaluate result က True ေပါ့)

Less than ro equal to (<=)

x <= y

(x ဟာ y ထက္ငယ္သလား/ညီသလားစစ္တာပါ။ ငယ္/ညီရင္ evaluate result က True ေပါ့)

90
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Relational expression မွာ operator ဟာ entity ႏွစ္ခုရဲ႕ ဆက္စပ္မႈကိုစစ္ေဆးပါတယ္။

ဥပမာ 5 = 5 ဆိုတာ 5 နဲ႕ 5 ညီသလားစစ္တာေပါ့။ 4 >= 3 ဆိုတဲ့ expression ကေတာ့ 4 ဟာ 3

ထက္ၾကီးသလား သို႕မဟုတ္ ညီသလားဆိုတာကိုစစ္တာေပါ့။ ဒီလို expression ေတြကို relational

expression လို႕ေခၚပါတယ္။ Relational expression ေတြကို evaluate လုပ္ျပီးရလာတဲ့ result

ကေတာ့ True/False ျဖစ္ပါမယ္။ အထက္က ဥပမာမွာ 5 = 5 ကို evaluate လုပ္ရင္ result က True

ေပါ့။ 4 >= 3 expression ကုိ evaluate လုပ္ရင္ result က True ျဖစ္မယ္ေနာ္။ 6 = 5 ကို evaluate

လုပ္ရင္ result က False ျဖစ္ပါမယ္။

3.3.3. Boolean Expression

Evaluate လုပ္တဲ့အခါ True သို႕မဟုတ္ False ရတဲ့ expression ကုိ boolean expression

လို႕ေခၚပါတယ္။ Boolean operator ေတြကို conditional statement ေတြျဖစ္တဲ့ IF-Then နဲ႕

Select-Case တုိ႕မွာသံုးေလ့ရွိပါတယ္။ Boolean operator ေတြကေတာ့ Or, And နဲ႕ Not

တို႕ျဖစ္ပါတယ္။

Z = X Or Y

ဆိုတဲ့ expression မွာ X သို႕မဟုတ္ Y တစ္ခုခုဟာ True ျဖစ္ရင္ Z ဟာ True ျဖစ္မယ္။

Z = X And Y

ဆိုတဲ့ expression မွာ X ေရာ Y ပါ True ျဖစ္မွ Z ဟာ True ျဖစ္ပါမယ္။ ဇယား (၁-၁) မွာၾကည့္ပါ။

ဇယား (၁-၁)

91
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Not ကိုေလ့လာၾကည့္ရေအာင္။ X ဟာ True ျဖစ္ရင္

Y = Not(X)

ဆိုတဲ့ expression မွာ Y ဟာ False ျဖစ္မယ္။ True ကို Not လုပ္ရင္ False, False ကို Not လုပ္ရင္

True ျဖစ္ပါတယ္။

3.4. Conditional Statement

Expression ရဲ႕ evaluate result ေပၚမူတည္ျပီးေဆာင္ရြက္ရတဲ့ statement ေတြကို

conditional statement လို႕ေခၚပါတယ္။

3.4.1. If-Then Structure

„If – Then – End If‟ structure ကုိ ေအာက္ပါ example နဲ႕ေလ့လာၾကည့္ရေအာင္။

If test1 Then

code1

End If

Expression „test 1‟ True ျဖစ္ရင္ code1 ကုိေဆာင္ရြက္ပါတယ္။ False

ျဖစ္ရင္ဘာမွမလုပ္ပါဘူး။ ဥပမာ

Dim a, b As Int

b=0

If b = 0 Then

Log(“b is 0”)

End If

„b = 0‟ expression True ျဖစ္ရင္ „Log(“b is 0”)‟ code ကုိ execute လုပ္ပါတယ္။ False

ျဖစ္ရင္ ဘာမွမလုပ္ပါဘူး။

92
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
3.4.2. If-Then-Else Structure

„If – Then – Else‟ structure ကုိေအာက္ပါ example နဲ႕ေလ့လာၾကည့္ရေအာင္။

If test1 Then

code1

Else

code2

End If

Expression „test 1‟ True ျဖစ္ရင္ code1 ကုိေဆာင္ရြက္ပါတယ္။ False ျဖစ္ရင္ code2

ကုိေဆာင္ရြက္ပါမယ္။ ဥပမာ

Dim a, b As Int

b=0

If b = 0 Then

Log(“b is 0”)

Else

Log(“b is not 0”)

End If

„b = 0‟ expression True ျဖစ္ရင္ „Log(“b is 0”)‟ code ကုိ execute လုပ္ပါတယ္။ False

ျဖစ္ရင္ Log(“b is not 0”) ကို execute လုပ္ပါမယ္။

93
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ေနာက္ structure တစ္ခုကေတာ့

If test1 Then „test1‟ true ျဖစ္ရင္ „code1‟ ကို execute လုပ္မယ္။

code1

Else If test2 Then


„test1‟ false ျဖစ္ျပီး „test2‟ true ျဖစ္ရင္ „code2‟ ကို
code2
execute လုပ္မယ္။
Else If test3 Then

code3

… …

… …


Else
„test1‟ „test2‟ „…‟ „…‟ တစ္ခုမွ true မျဖစ္ရင္ „code‟
code
ကို execute လုပ္မယ္။
End If

ဥပမာ

Dim a, b As Int

a = 1, b = 2

If a < b Then

Log(“a is less than b”)

Else If a > b Then

Log(“a is greater than b”)

Else

Log(“a is equal to b”)

End If

94
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
3.4.3. Select – Case

„If - Else - If‟ အစား „Select - Case‟ ကုိ အသံုးျပဳႏုိင္ပါတယ္။ Select - Case ရဲ႕ structure

ကေတာ့ ေအာက္ပါအတုိင္းျဖစ္ပါတယ္။

Select test

Case 0 : code1

Case 1 : code2

Case 2 : code3

End Select

Test ထဲက value ဟာ 0 ျဖစ္ရင္ code1, 1 ျဖစ္ရင္ code2, 2 ျဖစ္ရင္ code3 ကို execute

လုပ္ပါတယ္။ ဥပမာ

Dim temp As Double

temp = 1

Select temp

Case 0 : “temp is 0”

Case 1 : “temp is 1”

Case 2 : “temp is 2”

End select

„temp‟ ရဲ႕ value ကို Case ေနာက္က value နဲ႕တိုက္စစ္မယ္။ တူတဲ့ Case ရဲ႕ code ကုိ

execute လုပ္မယ္။ အထက္ပါ example မွာဆုိရင္ Case 1 ကုိ execute လုပ္မွာျဖစ္တဲ့အတြက္ log

area မွာ „temp is 1‟ လို႕ျမင္ရပါမယ္။

95
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
3.5. Loop Structures

Statement(s) တစ္ခုကို ထပ္တလဲလဲ execute လုပ္ဘုိ႕အတြက္ loop structure

ကုိသံုးရပါမယ္။

3.5.1. For – Next

ဘယ္ႏွစ္ၾကိမ္ execute လုပ္ရမယ္ဆိုတာသိရင္ For - Next loop ကုိသံုးႏုိင္ပါတယ္။ ဥပမာ

For i = n1 To n2 Step n3

„Specific code

Next

„i' ကို incremental variable လို႕ေခၚပါတယ္။ „n1‟ ကေတာ့ initial value ပါ။ „n2‟ က final

value ပါ။ n3 ကေတာ့ step ျဖစ္ပါတယ္။

For – Next loop ရဲ႕အလုပ္လုပ္ပံုကိုေအာက္မွာအဆင့္လိုက္ရွင္းျပေပးထားပါတယ္။

1. စစခ်င္း i ထဲမွာ n1 ကို assign လုပ္ပါတယ္။

i = n1

2. For နဲ႕ Next ၾကားက code ေတြကို execute လုပ္ပါတယ္။

3. Next ကိုေရာက္သြားရင္ i ရဲ႕ value ကို n3 နဲ႕ေပါင္းပါတယ္။

i = i + n3

4. ျပီးရင္ control ဟာ For ဆီျပန္ေရာက္သြားပါတယ္။ i နဲ႕ n2 ကို compare လုပ္ပါတယ္။

(i <= n2) သည္ True ျဖစ္သလားေပါ့။

If i <= n2

5. (i <= n2) သည္ True ျဖစ္ရင္ program ဟာ For နဲ႕ Next ၾကားက code ေတြကိုထပ္မံ

execute လုပ္ပါတယ္။

6. (i <= n2) သည္ False ျဖစ္ရင္ေတာ့ program ဟာ For - Next loop ကေနထြက္သြားပါ

မယ္။

96
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
တကယ္လို႕ Step ရဲ႕ value ဟာ +1 ျဖစ္ရင္ေတာ့ Step ဆိုတဲ့ keyword

ကုိမထည့္လည္းရပါတယ္။ ေအာက္ပါ code ၀ခုဟာတူတူဘဲျဖစ္ပါတယ္။

For i = 0 To 10 For i = 0 To 10 Step 1

Log(“Hello”) Log(“Hello”)

Next Next

Step ဟာ negative value လည္းျဖစ္ႏုိင္ပါတယ္။ ဥပမာ

For i = n3 To 0 Step -1

Log(“Hello”)

Next

အထက္ပါ code မွာ စစခ်င္း i ထဲမွာ n3 ကုိထည့္မယ္။ Logs area မွာ Hello ကုိထုတ္ျပမယ္။

i ထဲက 1 ကုိႏႈတ္မယ္။ (i >=0) သည္ True ျဖစ္ေနသမွ် Hello ကုိထုတ္ျပမယ္။

သာမန္အားျဖင့္ဆိုရင္ For-Next loop ဟာ သတ္မွတ္ထားတဲ့အၾကိမ္အေရအတြက္ျပည့္မွသာ

loop ထဲကထြက္ပါတယ္။ အၾကိမ္မျပည့္မီ ထြက္ခ်င္ရင္ေတာ့ Exit keyword ကုိသံုးႏုိင္ပါတယ္။ ဥပမာ

Dim A As Int

A=0

For i = 0 To 10

A=A+1

If A = 2 Then Exit

Next

97
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
အထက္ပါ example ကိုၾကည့္ရင္ A ထဲမွာ 0 ထည့္ထားပါတယ္။ For loop အစမွာ i ထဲကို 0

ထည့္ပါတယ္။ ေနာက္ (A = A + 1) statement ကုိလုပ္ပါတယ္။ ဒီ statement က A ကုိ 1 ေပါင္းျပီး

A ထဲျပန္ထည့္တာပါ။ A က 0 ဆိုေတာ့ 1 ေပါင္းရင္ 1 ျဖစ္ပါတယ္။ အဲဒီ 1 ကုိ A

ထဲျပန္ထည့္တဲ့အတြက္ A = 1 ျဖစ္ပါတယ္။

(A = 2) သည္ True ျဖစ္သလားစစ္ပါတယ္။ မျဖစ္ပါဘူး။ ဒါေၾကာင့္ Exit မလုပ္ပါဘူး။ ျပီးရင္

Next ကုိေရာက္ပါတယ္။ Next ကုိေရာက္ရင္ i ကို 1 ေပါင္းပါတယ္။ ျပီးရင္ For ကုိျပန္ေရာက္ပါတယ္။

(i >= 10) သည္ True ျဖစ္လားစစ္ပါတယ္။ ျဖစ္တဲ့အတြက္ loop ထဲျပန္ှင္ပါတယ္။ (A = A + 1)

code ကိုထပ္ျပီး execute လုပပ


္ ါတယ္။ ဒီတစ္ခါေတာ့ A = 1 +1 = 2 ျဖစ္ပါတယ္။ ဒီတစ္ခါေတာ့

(A = 2) သည္ True ျဖစ္သြားတာေၾကာင့္ Exit code ကုိ execute လုပ္ပါတယ္။ Exit က loop

အျပင္ကိုထြက္ေစပါတယ္။ ဒါေၾကာင့္ Next ကုိလည္းမေရာက္ For ကုိလည္းျပန္မေရာက္ေတာ့ဘဲ For

loop ထဲကေန ထြက္သြားပါတယ္။ ထြက္သြားခ်ိန္မွာသတ္မွတ္ထားတဲ့အၾကိမ္ မျပည့္ေသးပါဘူး။

ဒါေပမယ့္ (A = 2) သည္ True ျဖစ္တာေၾကာင့္ ထြက္သြားရတာပါ။

3.5.2. For – Each

For – Each loop ရဲ႕ structure ကေအာက္ပါအတုိင္းျဖစ္ပါတယ္။

For Each n As Type In Array

„Specific code

Next

အထက္ပါ code မွာ „n‟ ဆိုတာ variable/object ျဖစ္ပါတယ္။ Type ဆုိတာကေတာ့ „n‟ ရဲ႕

type ပါ။ Array ဆိုတာကေတာ့ values/objects ေတြပါတဲ့ array ေပါ့။ For-Each loop

ရဲ႕အလုပ္လုပ္ပံုကေတာ့ေအာက္ပါအတိုင္းျဖစ္ပါတယ္။

98
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
1. စစခ်င္းမွာ n ထဲကို Array ရဲ႕ first element ထည့္ပါတယ္။

n = Array(0)

2. ျပီးရင္ For နဲ႕ Next ၾကားက code ေတြကို execute လုပ္ပါတယ္။

3. Next ကိုေရာက္ရင္ n ရဲ႕ value ဟာ Array ရဲ႕ last element ထဲက value

ျဖစ္ျပီလားစစ္ပါတယ္။

4. Last element ရဲ႕ value မဟုတ္ေသးဘူးဆိုရင္ n ထဲကို Array ရဲ႕ second element

ကိုထည့္ပါတယ္။ ျပီးရင္ For နဲ႕ Next ၾကားက code ေတြကို execute လုပ္ပါတယ္။

5. n ရဲ႕ value ဟာ Array ရဲ႕ last element ျဖစ္သြားရင္ေတာ့ For ဆီျပန္မသြားေတာ့ပါဘူး။

For loop ထဲကေနထြက္သြားပါတယ္။ Next ေနာက္က code ေတြကိုဆက္ျပီး execute

လုပ္ပါမယ္။

ေအာက္ပါ example ကိုေလ့လာၾကည့္ရေအာင္။

Dim Numbers() As Int Numbers ဆိုတဲ့ Int type array တစ္ခု declare

လုပ္ပါတယ္။

Dim Sum As Int Sum ဆိုတဲ့ Int type variable တစ္ခု declare

လုပ္ပါတယ္။

Numbers = Array As Int(1, 3, 5 , 2, 9) Numbers ထဲကို 1, 3, 5, 2, 9

ထည့္ပါတယ္။ Array ထဲမွာ data

ေတြရွိေနမယ့္အေနအထားက

ေအာက္မွာျပထားတဲ့အတုိင္း ျဖစ္ပါတယ္။

Sum = 0 Sum ထဲကို 0 assign လုပ္ပါတယ္။

99
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
For Each n As Int In Numbers စစခ်င္း n ထဲမွာ Array ရဲ႕ first element

ကုိထည့္ပါတယ္။ ဒါေၾကာင့္ n ထဲမွာ1 ေရာက္ပါျပီ။

Sum = Sum + n Sum နဲ႕ n ကိုေပါင္းျပီး Sum ထဲထည့္ပါတယ္။

Sum = 1 ျဖစ္သြားပါျပီ။

Log(Sum) Sum ထဲက value ကုိထုတ္ၾကည့္ပါတယ္။ 1

ျဖစ္ပါမယ္။

Next n ထဲက value ဟာ Numbers ရဲ႕ေနာက္ဆံုး

element လားစစ္ပါတယ္။ မဟုတ္ေသးရင္ For

ကိုျပန္သြားျပီး အေပၚက အဆင့္ေတြအတိုင္း

ေဆာင္ရြက္ပါတယ္။ ေနာက္ဆံုး element

ဆိုရင္ေတာ့ loop ထဲကထြက္ပါသြားပါမယ္။

အထက္ပါ example ကုိေအာက္ပါအတိုင္း For-Next loop နဲ႕ေရးႏုိင္ပါေသးတယ္။

Dim Numbers() As Int

Dim Sum As Int

Numbers = Array As Int(1, 3, 5 , 2, 9)

Sum = 0

For i = 0 To Numbers.Length - 1

Sum = Sum + Numbers(i)

Next

3.5.3. Do – Loop

Execute လုပ္ရမယ့္အၾကိမ္အေရအတြက္မသိရင္ Do – Loop ကိုသံုးရပါမယ္။ Do – Loop

ရဲ႕ structure ကေအာက္ပါအတုိင္းျဖစ္ပါတယ္။

100
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Do While test

„code

Loop

„test‟ expression သည္ True ျဖစ္ေနသမွ် code ကုိ execute လုပ္ေနပါမယ္။

Do While Loop example ကုိေအာက္မွာ ေလ့လာၾကည့္ပါ။ i = 0 ကစျပီး i = i + 1 ဆိုတဲ့

statement ကုိ execute လုပ္မယ္။ (i < 10) expression True ျဖစ္ေနသမွ် Do While loop

ကုိပတ္ေနမယ္။

Dim i As Int

i=0

Do While i <10

i=i+1

Loop

Do While loop ထဲကထြက္ခ်င္ရင္ Exit keyword ကိုသံုးျပီးထြက္ႏုိင္တယ္။ ဥပမာ

Do While i <10

i=i+1

If i = 5 Then Exit (i = 5 ျဖစ္ရင္ (i <10) ဟာ True ျဖစ္ေနေသးေပမယ့္ Do

While loop ထဲကထြက္သြားပါလိမ့္မယ္။)

Loop

101
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Do Until

Do Until ကေတာ့ „test‟ expression သည္ False ျဖစ္ေနသမွ် code ကုိ execute လုပ္မယ္။

Do Until ရဲ႕ structure ကေတာ့ေအာက္ပါအတိုင္းျဖစ္ပါတယ္။

Do Until test

„code

Loop

ေအာက္ပါ Example ကိုၾကည့္ပါ။

Dim i As Int

i=0

Do Until i = 10 i = 0 ကစတယ္။ (i = 10) expression ဟာ


i=i+1
False ျဖစ္ေနသမွ် Do Until loop ကုိပတ္ေနမယ္။
Loop

3.6. Sub

Subroutine (အတိုေကာက္ Sub) ဆိုတာ program code အစိတ္အပိုင္းတစ္ခုပါ။ Sub

တုိင္းကိုအမည္ေပးရပါတယ္။ BASIC language မွာသံုးတဲ့ Sub ဆိုတာဟာ အျခား programming

language ေတြရဲ႕ procedure, function, method ေတြနဲ႕သေဘာတရားခ်င္းတူပါတယ္။

Sub အတြင္းက code ေတြကို အေပၚဆံုးကေန ေအာက္ဆံုးထိ အစီအစဥ္အတုိင္း execute

လုပ္ပါတယ္။ Sub အတြင္းမွာ ရွိရမယ့္ code အေရအတြက္ကို သတ္မွတ္မထားေပမယ့္

သိပ္မ်ားရင္ေတာ့ ဖတ္ရတာခက္ခႏ
ဲ ိုင္ပါတယ္။

102
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
3.6.1. Declaring

Sub ကို ေအာက္ပါအတုိင္း declare လုပ္ရပါတယ္။

Sub CalcInterest(Capital As Double, Rate As Double) As Double

Return Capital * Rate / 100

End Sub

Sub ဆုိတဲ့ keyword နဲ႕စရမယ္။ ေနာက္မွာ Sub ရဲ႕ name ပါရမယ္ (ဥပမာ CalcInterest)။

လိုအပ္ခ်က္ေပၚမူတည္ျပီး parameter list နဲ႕ return type ေတြထည့္ေပးရပါတယ္။ End Sub ဆိုတဲ့

keyword နဲ႕အဆံုးသတ္ရမယ္။ Sub ထဲမွာေနာက္ထပ္ Sub ေတြထည့္လို႕ေတာ့မရပါဘူး။

3.6.2. Naming

Sub name ကုိၾကိဳက္ရာအမည္ေပးႏုိင္ပါတယ္။ ဒါေပမယ့္ အဓိပၸါယ္ရွိတဲ့ အမည္မ်ိဳးသာ

ေပးသင့္ပါတယ္။ ဥပမာ အတိုးတြက္တဲ့ code ေတြေရးထားတဲ့ sub ကုိ CalcInterest

လို႕အမည္ေပးမွသင့္ေတာ္မွာေပါ့။ Program ထဲမွာ sub အေရအတြက္ဘယ္ေလာက္ရွိရမယ္ဆိုတဲ့

ကန္႕သတ္ခ်က္မရွိပါဘူး။ ဒါေပမယ့္ module တစ္ခုထဲမွာ အမည္တူ sub ၀ခုေတာ့မရွိေစရပါဘူး။

3.6.3. Calling a Sub

Sub တစ္ခုအတြင္းက code ေတြကို execute လုပ္ခ်င္ရင္ အဲဒီ sub ကို ေခၚရပါတယ္။

ေခၚနည္းကေတာ့ sub name ကုိေရးလုိက္ရံုသာျဖစ္ပါတယ္။ ဥပမာ

Sub Bank

Interest = CalcInterest(1234, 5.2)  CalcInterest sub ကုိ ေခၚလုိက္တာပါ။

End

103
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
တစ္နည္းအားျဖင့္ CalcInterest Sub ထဲက code ေတြကို execute လုပ္ခိုင္းတာပါဘဲ။

အဲဒီလို execute လုပ္ဘို႕ လိုအပ္တဲ့ parameter ေတြကို () ထဲမွာထည့္ျပီးေပးရပါတယ္။ ဥပမာ

CalcInterest(1235, 5.2)

3.6.4. Parameter

Sub တစ္ခုက data ေတြကို အျခား sub ဆီပို႕ေပးလို႕ရပါတယ္။ အျခား sub ကပို႕လာတဲ့

data ေတြကိုလက္ခံဘုိ႕ parameter ေတြကို () ထဲမွာ declare လုပ္ရမယ္။ ဥပမာ

Sub CalcInterest(Capital As Double, Rate As Double) As Double

Return Capital * Rate / 100

End Sub

Captial နဲ႕ Rate ဟာအျခား sub ကပို႕လာမယ့္ data ေတြကိုလက္ခံယူမယ့္ parameter

ေတြျဖစ္ပါတယ္။ Double ကေတာ့ အဲဒီ parameter ေတြရဲ႕ type ပါ။ အျခား sub ကေန

CalcInterest(1235, 5.2) ဆုိျပီး ေခၚလာတဲ့အတြက္ 1235 ဟာ Capital ထဲေရာက္ျပီး 5.2 က Rate

ထဲေရာက္ပါတယ္။

3.6.5. Return Value

Sub တစ္ခုဟာ သူ႕ကို ေခၚတဲ့ calling sub ဆီကို value တစ္ခုျပန္ပို႕ေပးႏုိင္ပါတယ္။ ဒါကို

return ျပန္တယ္လို႕ေခၚပါတယ္။ Return ျပန္တယ္ဆိုတာ ဥပမာ ေျပာရရင္ sub A နဲ႕ sub B

၀ခုရွိတယ္ဆိုပါစို႕။ Sub A က sub B ကုိ ေခၚလုိက္မယ္။ Sub B က value တစ္ခုကို retrun

ျပန္မယ္ဆိုရင္ return value ဟာ သူ႕ကို ေခၚခဲ့တဲ့ sub A ဆီေရာက္မယ္။ Return ျပန္မယ့္ value

ဟာ variable/object တစ္ခုျဖစ္ပါမယ္။ Value ကုိ retrun ျပန္ဘုိ႕ Return ဆိုတဲ့ keyword

ကုိသံုးရမယ္။ Return value ရဲ႕ type ကို sub declaration ရဲ႕ parameter list ေနာက္မွာေရးရမယ္။

ဥပမာ

104
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub CalcInterest(Capital As Double, Rate As Double) As Double

Return Capital * Rate / 100

End Sub

Sub CalcInterest ထဲမွာ Capital * Rate / 100 ကိုတြက္ပါတယ္။ အေျဖ 64.22 ရပါတယ္။

အေျဖကို retun ျပန္ပါတယ္။ Return ျပန္တယ္ဆိုတာ value ကုိ calling sub ဆီပို႕တာပါ။ Calling

sub က Bank ျဖစ္ပါတယ္။ ဒါေၾကာင့္ value (64.22) ဟာ Bank ဆီေရာက္သြားပါတယ္။ Sub Bank

မွာ return value (64.22) ကုိ Interest ထဲထည့္ပါတယ္။ ျပီးရင္ ထုတ္ၾကည့္ပါတယ္။ Logs area မွာ

64.22 လုိ႕ျမင္ရမွာျဖစ္ပါတယ္။

Sub Bank

Interest = CalcInterest(1234, 5.2)64.22

Log(Interest)

End Sub

Sub Bank Sub CalcInterest(Capital As Double, Rate As Double) As Double

Interest = CalcInterest(1234, 5.2) Return Capital * Rate / 100

End Sub

Log(Interest)

End Sub

3.6.6. Calling a Sub from Another Module

Module တစ္ခုမွာ declare လုပ္ထားတဲ့ sub ကိုအျခား module ကေန

လွမ္းေခၚလို႕လည္းရပါတယ္။ ဒါေပမယ့္ sub name သာမက module name ပါထည့္ေပးရပါမယ္။

ဥပမာ MyModule ထဲက CalcInterest sub ကုိ အျခား module ကေန ေခၚခ်င္ရင္

MyMoudle.CalcInerest လို႕ေရးရမယ္။

Interest = MyModule.CalcInterest(1234, 5.2)

Interest = CalcInterest(1234, 5.2)  ဒီလိုေခၚလို႕မရပါဘူး။

105
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
3.7. Event

User က view တစ္ခုကို click လိုက္ရင္ event တစ္ခုျဖစ္ေပၚပါတယ္။ Click event

လို႕ေခၚပါတယ္။ Event ဘယ္ႏွစ္မ်ိဳးရွိလည္း၊ ဘယ္လို event type ေတြရွိလည္းဆိုတာကေတာ့

object type ေပၚမူတည္ပါတယ္။

Android မွာ user interface object ေတြကို view လို႕ေခၚပါတယ္ (ဥပမာ Button, Label,

EditText အစရွိသည္တုိ႕ဟာ view ေတြျဖစ္ပါတယ္) View အမ်ိဳးမ်ိဳးအတြက္ event ေတြကို

ဇယား (၁-၂) မွာျပေပးထားပါတယ္။

ဇယား (၁-၂)

106
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
အသံုးမ်ားတဲ့ event ေတြကို ေအာက္မွာရွင္းျပေပးထားပါတယ္။

Click

User က view တစ္ခုကို click လုပ္လုိက္ရင္ Click Event ျဖစ္ေပၚပါတယ္။ ဥပမာ Button

တစ္ခုရွိမယ္။ Button ရဲ႕ event name က Button1 ဆိုပါေတာ့။ အဲဒီ Button ကုိ click လိုက္ရင္

Click event ျဖစ္ေပၚလာပါမယ္။ Click event ကို handle လုပ္မယ့္ code ကုိ Button1_Click event

sub အတြင္းမွာေရးေပးရပါမယ္။ Button1 ကုိ click ရင္ Logs area မွာ Hello လို႕ေပၚေစခ်င္ပါက

ေအာက္ပါအတုိင္းေရးေပးရပါမယ္။

Sub Button1_Click Event Event Name

Log(“Hello”) Event Handler code

End Sub

LongClick

User က view တစ္ခုကိုခပ္ၾကာၾကာဖိထားရင္ LongClick event ျဖစ္ေပၚလာပါတယ္။ ဥပမာ

Sub Button1_LongClick

' Your code

End Sub

Touch (Action As Int, X As Float, Y As Float)

User က screen ကိုလက္နဲ႕ထိရင္ Touch event ျဖစ္ေပၚပါတယ္။ Touch event မွာ action

၁မ်ိဳးရွိပါတယ္။

 Activity.Action_DOWN (User က screen ကုိ ထိရံုသာထိလိုက္တဲ့အခါျဖစ္ေပၚလာတဲ့

action)

 Activity.Action_MOVE (User က screen ကို လက္နပ


ဲ႔ ြတ္တအ
ဲ့ ခါ ျဖစ္ေပၚလာတဲ့ action)

107
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
 Activity.Action_UP (User က screen ကိုလက္နဲ႕ထိထားရာကေန လက္ကို screen

မွခြာလိုက္တဲ့အခါျဖစ္ေပၚလာတဲ့ action)

Screen မွာ လက္ထိေနတဲ့ေနရာကို X, Y coordinate system နဲ႕သတ္မွတ္ပါတယ္။ ဥပမာ

Sub Activity_Touch (Action As Int, X As Float, Y As Float)

Action = အထက္ပါ action ၁ခုအနက္ကတစ္ခုကို ကိုယ္စားျပဳတဲ့ integer value ပါ။

X, Y = User ရဲ႕လက္က screen ရဲ႕ဘယ္ေနရာကိုထိေနတယ္ဆုိတာ X, Y ထဲမွာ ထည့္ေပးပါတယ္။

Action ၁မ်ိဳးထဲက ဘယ္ action လည္းဆိုတာသိရေအာင္ „Action‟ ကို Select Case

နဲ႕စစ္ရမယ္။

Select Action Action ကို test လုပ္မယ္။

Case Activity.ACTION_DOWN Action Down လားစစ္ပါတယ္။

' Your code for DOWN action Action Down ဆိုရင္ ဒီ code ကုိ execute

လုပ္မယ္

Case Activity.ACTION_MOVE Action Move လားစစ္ပါတယ္။

' Your code for MOVE action Action Down ဆိုရင္ ဒီ code ကုိ execute

လုပ္မယ္။

Case Activity.ACTION_UP Action Up လားစစ္ပါတယ္။

' Your code for UP action Action Up ဆိုရင္ ဒီ code ကုိ execute လုပ္မယ္

End Select

108
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
CheckChanged (Checked As Boolean)

User က CheckBox သုိ႕မဟုတ္ RadioButton ေတြကို click ရင္ CheckChanged event

ျဖစ္ေပၚပါတယ္။ Check လုပ္ရင္ Checked = True ျဖစ္ျပီး uncheck လုပ္ရင္ေတာ့

Checked = False ျဖစ္မယ္။ ဥပမာ

Sub CheckBox1_CheckedChange(Checked As Boolean)

If Checked = True Then

' Your code if checked

Else

' Your code if not checked

End If

End Sub

KeyPress (KeyCode As Int) As Boolean

User က key တစ္ခုကို ရိုက္လိုက္ရင္ KeyPress event ျဖစ္ေပၚပါတယ္။ KeyCode

ဆုိတာကေတာ့ user ႏွိပ္လိုက္တဲ့ key ရဲ႕ code ပါ။ KeyPress event ျဖစ္ေပၚရင္ Boolean type

variable တစ္ခက
ု ုိ return ျပန္ပါတယ္။ ေနာက္ထပ္ေဆာင္ရြက္စရာမလိုေတာ့ရင္ False ကုိ return

ျပန္ပါတယ္။ ေနာက္ထပ္ေဆာင္ရြက္စရာရွိရင္ True ကုိ return ျပန္ပါတယ္။ ဘယ္လို KeyCode

ေတြရွိလည္းသိခ်င္ရင္ IDE code window မွာ KeyCodes. လိုရိုက္ျပီးၾကည့္ႏုိင္ပါတယ္။ (ပံု ၁-၂)

109
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု (၁.၂)

KeyPress event အတြက္ ဥပမာတစ္ခုျပပါမယ္။ ထံုးစံအတုိင္းဆိုရင္ User က back key

ကုိႏိွပ္ရင္ program ကိုပိတ္ေပးရပါတယ္။ အခုေတာ့ တန္းမပိတ္ေပးဘဲ ပိတ္မွာေသခ်ာလားဆိုတဲ့

message box ကုိျပပါမယ္။ ပိတ္မွာေသခ်ာရင္ user က Yes ကုိေရြးရမယ္။ Yes ကိုေရြးရင္ program

ကုိပိတ္ေပးမယ္။ No ကုိေရြးရင္ေတာ့မပိတ္ေပးဘူး။ အဲဒီ အတြက္ event sub တစ္ခုေရးျပပါမယ္။

User က key တစ္ခုကိုႏွိပ္လိုက္တဲ့အခါ Activty_KeyPress event ျဖစ္ေပၚပါတယ္။ Event handler

code ကုိ ေအာက္ပါအတုိင္းေရးပါမယ္။

Sub Activity_KeyPress(KeyCode As Int) As Boolean

Dim Answ As Int


Variable ၀ခု declare လုပ္ပါတယ္။
Dim Txt As String

(KeyCode ဟာ Back key လားလို႕စစ္ပါတယ္။ Back key ျဖစ္မွဆက္လုပ္မယ္။

မဟုတ္ရင္မလုပ္ဘူး)

If KeyCode = KeyCodes.KEYCODE_BACK Then

(Txt ထဲမွာ string ထည့္တယ္)

Txt = "Do you really want to quit the program?"

110
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
(Message box တစ္ခုေပၚေစျပီး user ေရြးလိုက္တဲ့ response value ကို Answ

ထဲထည့္ပါတယ္။ Yes button ကုိႏွိပ္ရင္ Answ ထဲကို DialogResponse.POSITIVE

ေရာက္မယ္)

Answ = Msgbox2(Txt,"A T T E N T I O N","Yes","","No",Null)

(Answ ထဲမွာ DialogResponse.POSITIVE (Yes) လားလို႕စစ္ပါတယ္)

If Answ = DialogResponse.POSITIVE Then

(Yes ဆိုရင္ Program ကိုပိတ္ေပးရမယ္။ ဆက္လုပ္စရာမလိုေတာ့လို႕

False ကုိ return ျပန္ပါတယ္)

Return False

Else

(Yes မဟုတ္ရင္ Program ကိုမပိတ္ေပးရဘူး။ ဆက္ရွိေနဦးမွာမုိ႕ True ကုိ

return ျပန္ပါတယ္)

Return True

End If

End If

End Sub

3.8. Library

အခ်ိဳ႕ library ေတြက B4A ကုိ install လုပ္တာနဲ႔ပါျပီးျဖစ္ေပမယ့္ အခ်ိဳ႕ကိုေတာ့ download

ဆြဲယူအသံုးျပဳရမယ္။ အခ်ိဳ႕ကိုှယ္ရျပီး အခ်ိဳ႕ကိုေတာ့အလကားရပါတယ္။

Library တစ္ခုကိုအသံုးျပဳဘို႕လိုအပ္ရင္

 ရွိုျပီးသား library ဆိုရင္ Libs tab ကေနကိုယ္သံုးခ်င္တဲ့ library ကုိ check လုပ္ပါ။

 မရွိေသးရင္ download ဆြဲျပီး unzip လုပ္ပါ။ <LibraryName>.jar

နဲ႕<LibraryName>.xml file ေတြရမယ္။ အဲဒီဖုိင္ေတြကို Additional Library folder

ထဲထည့္ပါ။

111
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
3.8.1. Standard Library

Standard B4A library ေတြကို C:\Program Files\AnywhereSoftware\

Basic4android\Libraries ထဲမွာေတြ႕ႏုိင္ပါတယ္။

Example 3.3

Phone library မွ object ေတြကိုသံုးျပီး ဖုန္းေခၚျခင္း၊ SMS ပို႕ျခင္း၊ web site ဖြင့္ျခင္း၊ eMail

ပုိ႕ျခင္းတုိ႕ကိုနမူနာမ်ားနဲ႕ျပပါမယ္။ Libs tab ကေန Phone library ကုိ check လုပ္ထားပါ။ Designer

ကေန layout တစ္ခုဖန္တီးပါ။ Layout မွာ button တစ္ခုထည့္ျပီး layout name ကုိ Layout1

လို႕ေပးပါ။ Activity_Create sub မွာ layout file ကုိ load လုပ္ပါ။

Sub Activity_Create(FirstTime As Boolean)

Activity.LoadLayout(“Layout1”)

End Sub

Calling Phone

Phone ေခၚဆုိရန္အတြက္ဆိုလွ်င္ Button1_Click event sub မွာေအာက္ပါအတုိင္းေရးပါ။

Sub Button1_Click

Dim pc As PhoneCalls

StartActivity(pc.Call("09250386961"))

End Sub

112
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sending SMS

SMS ပို႕ရန္အတြက္ဆိုလွ်င္ Button1_Click event မွာေအာက္ပါအတုိင္းေရးပါ။

Sub Button1_Click

Dim Sms1 As PhoneSms

Sms1.Send("0973140515","Hello!")

End Sub

Opening Web Site

Web site ဖြင့္ရန္အတြက္ဆိုလွ်င္ Button1_Click event မွာေအာက္ပါအတုိင္းေရးပါ။

Sub Button1_Click

Dim pi As PhoneIntents

StartActivity(pi.OpenBrowser("http://www.facebook.com"))

End Sub

Sending Email

Email ပို႕ရန္အတြက္ဆိုလွ်င္ Button1_Click event မွာေအာက္ပါအတုိင္းေရးပါ။

Sub Button1_Click

Dim Message As Email

Message.To.Add("htarhtarlwin@gmail.com") [Receiver ရဲ႕ mail address]

Message.Subject = "Testing" [Subject မွာေရးခ်င္တဲ့ text]

Message.BCC.Add("dawthansein.dts@gmail.com") [BCC address]

Message.CC.Add("htwehtwelwin@gmail.com") [CC address]

113
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Message.Body = "Good Morning" [ေရးခ်င္တဲ့ text]

StartActivity(Message.GetIntent)

End Sub

3.8.2. Additional Libraries Folder

Download ဆြဲယူထားတဲ့ library ေတြကို သီးျခား folder တစ္ခုထဲမွာထားသင့္ပါတယ္။

ဥပမာ C:\Program Files\AnywhereSoftware\Basic4android\Additional Library ထဲမွာ

ထားသင့္ပါတယ္။ Stardard library file မ်ားရွိရာ Files\AnywhereSoftware\

Basic4android\Libraries folder ထဲမွာမထားသင့္ပါဘူး။

3.8.3. Error Message "Are you missing a library reference?"

အထက္ပါ error message ျမင္ရရင္ေတာ့ Libs tab ကေနျပန္စစ္ပါ။ လိုအပ္တဲ့ library ကုိ

check မလုပ္ထားခဲ့လို႕ပါ။

3.9. String Manipulation

String ဆိုတာ တစ္ခုထက္ပိုတဲ့ character ကိုေခၚတာပါ။ ဥပမာ „h‟ ဆိုတာ character, „i‟

ဆိုတာလည္း character ပါပဲ။ “hi” က်ေတာ့ string ျဖစ္သြားျပီ။

Variable txt ထဲ string 123,234,45,23 ကုိထည့္ခ်င္ရင္ေအာက္ပါအတုိင္းထည့္ပါ။

Dim txt As String

txt = "123,234,45,23"

String နဲ႕ပတ္သက္ျပီးအသံုးျပဳႏုိင္တဲ့ function ေတြကေတာ့ ေအာက္ပါအတုိင္းျဖစ္ပါတယ္။

CharAt(Index)

String ထဲက character တစ္ခုကိုလိုခ်င္ရင္ CharAt ကုိသံုးရမယ္။ ဥပမာ

txt = “hello”

114
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ch = txt.CharAt(0) ဆိုရင္ ch ထဲကို h ေရာက္လာမယ္။ txt ရဲ႕ position 0 မွာ h

ရွိေနလို႕ပါ။

ch = txt.ChatAt(1) ဆိုရင္ ch ထဲကို e ေရာက္လာမယ္။ txt ရဲ႕ position 1 မွာ e

ရွိေနလို႕ပါ။

CompareTo (Other)

String တစ္ခုကို အျခား string တစ္ခုနဲ႕ ႏိႈင္းယွဥ္ပါတယ္။ တူရင္ True ကုိ return

ျပန္ပါတယ္။ ဥပမာ

Dim s1, s2 As String

s1 = “I go to school.”

s2 = “I go to school.”

s1.CompareTo(s2)

Contains (SearchFor)

String ထဲမွာ ကိုယ္ရွာခ်င္တဲ့ string တစ္ခရ


ု ွိမရွိ test လုပ္ပါတယ္။ ရွိရင္ True ကုိ return

ျပန္ပါတယ္။ ဥပမာ string „s1‟ ထဲမွာ “Hello” ဆိုတဲ့ စာလံုး ရွိမရွိသိခ်င္ရင္

s1 = “Hello World!”

s2 = “Hello”

s1.Contains(s2)

StartsWidth(Prefix)

String ဟာ Prefix မွာေပးထားတဲ့ string နဲ႕စထားတာဆိုရင္ True ကုိ return ျပန္ပါတယ္။

ဥပမာ

s1 = “I go to school.”

s2 = “I”

115
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
s1.StartsWidth(s2)

EndsWith(Suffix)

String ဟာ Suffix မွာေပးထားတဲ့ string နဲ႕ အဆံုးသတ္ထားရင္ true ကုိ return

ျပန္ေပးပါတယ္။ ဥပမာ

s1 = “I go to school.”

s2 = “school.”

s1.EndsWidth(s2)

EqualsIgnoreCase(Other)

String ၀ခု တူလားစစ္ပါတယ္။ တူရင္ true ကို return ျပန္ပါတယ္။

စာလံုးအၾကီးအေသးမခြဲျခားပါဘူး။ ဥပမာ APPLE နဲ႕ apple ဆိုရင္တူတယ္လို႕သတ္မွတ္ပါတယ္။

s1= “APPLE”

s2 = “apple”

s1.EqualsIgnoreCase(s2)

GetBytes(Charset)

Charset ထဲက string ကို byte array အျဖစ္ encode လုပ္ပါတယ္။ ဥပမာ

Dim ba () As Byte

ba = GetBytes(Charset)

IndexOf(SearchFor)

String ထဲမွာကိုယ္ရွာလိုတဲ့ string ကုိ ပထမဆံုးေတြ႕တဲ့ position ကို return ျပန္ပါတယ္။

ဥပမာ

Dim s1, s2 As String

116
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Dim i As Int

s1 = “It‟s a virtual device and not a real device.”

s2 = “device”

i = s1.IndexOf(s2)

i ထဲမွာ 15 ေရာက္လာပါမယ္။ s1 မွာ “device ” ၀ခါပါေပမယ့္ ပထမဆံုးေတြ႕တဲ့ device ရဲ႕

position ကိုသာ return ျပန္ပါတယ္။

IndexOf2 (SearchFor, Index)

String ထဲမွာကိုယ္ရွာလိုတဲ့ string ကုိအေစာဆံုးေတြ႕တဲ့ position ကို return ျပန္ပါတယ္။

ရွာတဲ့အခါ အစကေနစမရွာဘဲ Index မွာေပးထားတဲ့ေနရာကစရွာပါတယ္။ ဥပမာ

Dim s1, s2 As String

Dim i As Int

s1 = “It‟s a virtual device and not a real device.”

s2 = “device”

i = s1.IndexOf2(s2, 22)

i ထဲမွာ 37 ျဖစ္ပါမယ္။ Positon 15 မွာ devcie တစ္ခုရွိေပမယ့္ index 22 ကစရွာတာမုိ႕

ဒုတိယ device ရဲ႕ position ျဖစ္တဲ့ 37 ကုိသာ retrun ျပန္ပါတယ္။

LastIndexOf (SearchFor)

String ထဲမွာကိုယ္ရွာလိုတဲ့ string ကုိ ပထမဆံုးေတြ႕တဲ့ position ကို return ျပန္ပါတယ္။

ရွာတဲ့အခါ ေနာက္ဆံုးကေနစရွာပါတယ္။ ဥပမာ

Dim s1, s2 As String

Dim i As Int

s1 = “It‟s a virtual device and not a real device.”

s2 = “device”

117
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
i = s1.LastIndexOf(s2)

i ထဲမွာ 37 ျဖစ္ပါမယ္။ ေနာက္ကစရွာျပီး အရင္ဆံုးေတြ႕တဲ့ device ရဲ႕ position ျဖစ္ပါတယ္။

Length

String မွာပါတဲ့ character အေရအတြက္ကို return ျပန္ပါတယ္။

Dim s2 As String

Dim i As Int

s2 = “device”

i = s2.Length

i ထဲမွာ 6 ျဖစ္ပါမယ္။ s2 မွာ character 6 လံုးရွိလို႕ပါ။

Replace (Target, Replacement)

Target မွာေပးထားတဲ့ string ေနရာမွာ Replacement မွာေပးထားတဲ့ string

ေတြနဲ႕အစားထိုးပါတယ္။ ေနာက္ဆံုးမွာ အစားထိုးျပီးသား string ကုိ return ျပန္ပါတယ္။ ဥပမာ

Dim txt As String

txt = "123,234,45,23"

txt ထဲက , ေနရာမွာ ; ကိုအစားထိုးခ်င္ရင္ ေအာက္ပါအတုိင္းေရးပါ။

txt = txt.Replace(",", ";")

ခုဆို txt ထဲမွာ 123;234;45;23 ျဖစ္သြားပါျပီ။

Substring (BeginIndex)

မူလ string ထဲကေန BeginIndex ကေနစတဲ့ substring တစ္ခုကို return ျပန္ေပးပါတယ္။

ဥပမာ

118
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Dim text, subtext As String

text = "Hello World"

subtext = text.SubString(6)

subtext ထဲမွာ World ဆိုတဲ့ string ေရာက္လာပါမယ္။

Substring2 (BeginIndex, EndIndex)

မူလ string ထဲက BeginIndex နဲ႕စျပီး EndIndex မွာဆံုးတဲ့ string ကုိ return

ျပန္ေပးပါတယ္။ ဥပမာ

Dim text, subtext As String

text = "Hello World"

subtext = text.SubString2(6,8)

subtext ထဲမွာ Wo ေရာက္လာပါမယ္။ Index 8 ရဲ႕ character „r‟ ကိုထည့္မတြက္ပါဘူး။

ToLowerCase

Upper case letter ကို lower case letter သုိ႕ေျပာင္းေပးပါတယ္။ ဥပမာ

Dim u, l As String

u = “HELLO”

l = ToLowerCase(u)

l ထဲမွာ hello ျဖစ္ပါမယ္။

ToUpperCase

Lower case letter ကို upper case letter သို႕ေျပာင္းေပးပါတယ္။ ဥပမာ

119
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Dim u, l As String

l = “hello”

u = ToUpperCase(l)

u ထဲမွာ HELLO ျဖစ္ပါမယ္။

Trim

မူလ string ရဲ႕ ေရွ႕ဆံုးနဲ႕ေနာက္ဆံုးမွာပါလာတတ္တဲ့ space ေတြကိုဖ်က္ျပီး return

ျပန္ေပးပါတယ္။ ဥပမာ

s1 = “ I go to school. ”

s2 = s1.Trim

s2 ထဲမွာ „I go to school.‟ သာေရာက္လာပါမယ္။ ေရွ႕နဲ႕ေနာက္က space

ေတြကိုဖ်က္ေပးပါတယ္။

Number Formatting

Number ေတြကို string အျဖစ္ display လုပ္ႏုိင္ပါတယ္။

NumberFormat(Number As Double, MinimumIntegers As Int, MaximumFractions As Int)

NumberFormat(12345.6789, 0, 2) -> 12,345.68 (ဒသမ၀ေနရာျဖတ္ေပးတယ္)

NumberFormat(1, 3 ,0) -> 001 (digit ၁ခုျပည့္ေအာင္ျဖည့္ေပးတယ္)

NumberFormat(Value, 3 ,0) (variable ေတြကိုလည္းသံုးႏိုင္တယ္)

NumberFormat(Value + 10, 3 ,0) (arithmetic operations ကိုလည္း

သံုးႏုိင္တယ္)

NumberFormat((lblscore.Text + 10), 0, 0) (Variable တစ္ခုက string ျဖစ္ေနရင္ ()

ထဲမွာထည့္ပါ။ lblscore.Text က string

ျဖစ္ေနတဲ့အတြက္ + 10 ကိုပါ ()

ထည့္ေရးရပါတယ္)

120
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
NumberFormat2(Number As Double, MinimumIntegers As Int, MaximumFractions As

Int, MinimumFractions As Int, GroupingUsed As Boolean)

NumberFormat2(12345.67, 0, 3, 3, True) -> 12,345.670

(ဒသမ ၁ေနရာျဖတ္ေပးတယ္။ Digit ၁ခုကို comma (,) တစ္ခုျခားျပီး group ဖြဲ႕ေပးတယ္)

3.10. Timer

Timer object ဟာ သတ္မွတ္ထားတဲ့အခ်ိန္ (interval) ျပည့္သြားရင္ tick event

ျဖစ္ေပၚပါတယ္။ Message box ကို ျပေနစဥ္မွာေတာ့ သတ္မွတ္ထားတဲ့အခ်ိန္ျပည့္သြားသည့္တုိင္ tick

event မျဖစ္ႏုိင္ပါဘူး။ Activity ကုိ pause လုပ္ထားရင္လည္း tick event ျဖစ္မွာမဟုတ္ပါဘူး။

ဒါေၾကာင့္ activity ကို pause လုပ္ထားရင္ Timer ကုိ disable လုပ္ထားသင့္တယ္။ resume

ျပန္လုပ္မွ enable ျပန္လုပ္သင့္ပါတယ္။

Initialize

Timer ကို parameter ၀ခုနဲ႕ initialize လုပ္ရမယ္။ Event name နဲ႕ interval ပါ။

Timer1.Initialize(EventName As String, Interval As Long)

ဥပမာ

Timer1.Initialize("T1", 1000)

Interval

Timer ရဲ႕ interval ကုိ milli-seconds နဲ႕သတ္မွတ္ရမယ္။

Timer1. Interval = Interval

ဥပမာ

Timer1.Interval = 1000 (1000 = 1 second ျဖစ္ပါတယ္)

121
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Enabled

Timer ကုိ enable/disable လုပ္ႏုိင္တယ္။ Default ကေတာ့ disable ျဖစ္ပါတယ္။ Timer

ကိုစတင္အလုပ္လုပ္ရန္ enable လုပ္ေပးရမယ္။ ဥပမာ

Timer1.Enabled = True

Tick

Timer မွာ event တစ္ခုသာရွိပါတယ္။ Tick event ပါ။ သတ္မွတ္ထားတဲ့ interval

ျပည့္သြားတိုင္း Tick event ျဖစ္ေပၚပါတယ္။ Timer ကို Process_Global routine မွာ declare

လုပ္ရမယ္။

Sub Process_Globals

Dim Timer1 AsTimer

End Sub

Timer ကုိ Actvity_Create မွာ initialize လုပ္ရမယ္။ Process variable ျဖစ္တဲ့အတြက္

တစ္ၾကိမ္သာ initialize လုပ္ရမယ္။ ဒါေၾကာင့္ FirstTime = True ျဖစ္မွသာ initialize လုပ္ရမယ္။

Sub Activity_Create(FirstTime As Boolean)

If FirstTime = True Then

Timer1.Initialize("T1", 1000)

End If

End Sub

122
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Timer Tick Event Routine

Timer ရဲ႕ သတ္မွတ္ထားတဲ့အခ်ိန္ျပည့္တုိင္းမွာ Timer Tick event ျဖစ္ေပၚပါတယ္။ Event

sub ကေတာ့ေအာက္ပါပံုစအ
ံ တုိင္းျဖစ္ပါတယ္။

Sub Timer1_Tick

'Do something

End Sub

3.11. File

Application အေတာ္မ်ားမ်ားမွာ data ေတြကိုသိမ္းဆည္းထားဘို႕လိုအပ္တတ္ပါတယ္။ Data

ေတြကို file နဲ႕ database ေတြမွာသိမ္းေလ့ရွိပါတယ္။

3.11.1. File Object

File object ဟာ predefined object ျဖစ္ပါတယ္။ Program ထဲမွာ define

လုပ္စရာမလိုဘဲသံုးႏုိင္ပါတယ္။ File object မွာ ဖုိင္ေတြနဲ႕အလုပ္လုပ္ႏုိင္တဲ့ method ေတြ

မ်ားစြာရွိပါတယ္။ File ေတြကို read/write လုပ္ႏုိင္တဲ့ ေနရာေတြကေတာ့ File.DirAssets,

File.DirInternal, File.DirInternalCache, File.DirRootExternal နဲ႕ File.DirDefaultExternal

တုိ႕ျဖစ္ပါတယ္။

File.DirAssets

IDE ရဲ႕ File manager ကေနထည့္လိုက္တဲ့ဖုိင္ေတြဟာ assets folder ထဲမွာရွိေနမွာပါ။

Assets folder ဆိုတာ „Files‟ folder ပါဘဲ (Project create လုပ္ျပီးေနာက္ Files နဲ႕ Object folder

၀ခုကို create လုပ္ေပးပါတယ္) ဒီ ဖုိင္ေတြဟာ read only file ေတြျဖစ္ပါတယ္။ ဒါေၾကာင့္ Dir.Assets

folder ထဲမွာ database file ရွိေနရင္ database file ကုိမသံုးမီအျခား folder ထဲကို copy လုပ္ရမယ္။

မဟုတ္ရင္ database မွာ write operation လုပ္လုိ႕မရဘဲျဖစ္ပါမယ္။

123
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
File.DirInternal / File.DirInternalCache

ဒီ folder ၀ခုကေတာ့ device ရဲ႕ internal memory မွာရွိပါတယ္။ ဒီ folder ထဲမွာရွိတဲ့ဖိုင္ကို

create လုပ္ခဲ့တဲ့ application ကသာသံုးႏုိင္ပါတယ္။ အျခား application ေတြကမသံုးႏုိင္ပါဘူး။

Cache folder မွာရွိတဲ့ဖုိင္ေတြဆိုရင္ memory ေနရာလိုအပ္ခ်က္ရွိရင္ ဖ်က္ခံရမွာပါ။

File.DirRootExternal

SD card ရဲ႕ root folder ပါ။

File.DirDefaultExternal

SD card မွာ install လုပ္ထားတဲ့ application ေတြအတြက္ default folder ပါ။ Path

ကေတာ့

<storagecard>/Android/data/<package>/files/

Application ထဲမွာ SD card ရဲ႕ folder ေတြကိုအသံုးျပဳမယ္ဆိုရင္ manifest editor မွာ

EXTERNAL_STORAGE permission ထည့္ဘ႕ို လိုပါမယ္။

SD card ရွိမရွိနဲ႕ အသံုးျပဳလို႕ရမရဆိုတာကို

File.ExternalReadable နဲ႕

File.ExternalWritable method တုိ႕ကိုသံုးျပီးစစ္ႏုိင္ပါတယ္။

File တစ္ခုရွိလား မရွိလား စစ္ခ်င္ရင္

File.Exists (Dir As String, FileName As String)

ကိုသံုးရတယ္။ File ရွိရင္ True ကုိ return ျပန္ျပီးမရွိရင္ False ကုိ return ျပန္မယ္။

File object မွာ file read/write လုပ္ႏုိင္တဲ့ method ေတြမ်ားစြာရွိပါတယ္။ ဖိုင္ေတြကို

read/write မလုပ္ခင္အရင္ ဖြင့္ဘို႕လိုပါတယ္။

124
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
File.OpenOutput (Dir As String, FileName As String, Append As Boolean)

File တစ္ခုကိုေရးဘုိ႕ရည္ရြယ္ခ်က္နဲ႕ဖြင့္ပါတယ္။ ဖိုင္မရွိေသးရင္အသစ္ဖြင့္ေပးပါတယ္။

Append က True ျဖစ္ရင္ ဖုိင္မွာမူလရွိျပီးသား text ကိုမဖ်က္ဘဲေအာက္မွာဆက္ေရးမယ္။ False

ဆိုရင္ေတာ့ ဖိုင္မွာမူလရွိျပီးသား text ေတြကို ေနာက္ေရးတဲ့ text ေတြက overwride လုပ္သြားမွာပါ။

File.OpenInput (Dir As String, FileName As String)

ဖတ္ဘို႕ရည္ရြယ္ခ်က္နဲ႕ဖုိင္ကိုဖြင့္ပါတယ္။

File.WriteString (Dir As String, FileName As String, Text As String)

Text ကို file တစ္ခုမွာသြားေရးပါတယ္။

File.ReadString (Dir As String, FileName As String) As String

ဖုိင္တစ္ခုကိုဖတ္ျပီး ဖုိင္မွာေရးထားတာေတြကို string အျဖစ္ return ျပန္ပါတယ္။

File.WriteList (Dir As String, FileName As String, List As List)

List ထဲမွာသိမ္းထားတဲ့ value ေတြကို ဖုိင္မွာသြားေရးပါတယ္။ လိုအပ္ရင္ list ထဲက value

ေတြကို string type အျဖစ္ေျပာင္းေပးပါတယ္။ Value တစ္ခုကို တစ္လိုင္းေရးေပးပါတယ္။ Value မွာ

new line character ပါေနရင္ေတာ့ တစ္လိုင္းထက္ပိုသြားမွာေပါ့။ အဲဒါကိုျပန္ဖတ္တဲ့အခါမွာ multiple

items အျဖစ္ဖတ္ပါလိမ့္မယ္။ ဆိုလိုတာက value မွာ new line character ပါခဲ့ရင္ ဖုိင္မွာေရးစဥ္က

item တစ္ခုဆိုရင္ ျပန္ဖတ္တဲ့အခါ item ၀ခုျဖစ္သြားပါလိမ့္မယ္။

File.ReadList (Dir As String, FileName As String) As List

ဖုိင္ကိုဖတ္ျပီး တစ္လိုင္းကို item တစ္ခုအျဖစ္ list မွာသိမ္းပါတယ္။

125
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
File.WriteMap (Dir As String, FileName As String, Map As Map)

Map ထဲက key/value ေတြကိုယူျပီး text file မွာသိမ္းေပးပါတယ္။

File.ReadMap (Dir As String, FileName As String) As Map

File ကိုဖတ္ျပီး ရလာတဲ့ key/value ေတြကို Map object အျဖစ္ return ျပန္ပါတယ္။ Return

ျပန္လာတဲ့ object ေတြရဲ႕ ေရွ႕ေနာက္အစီအစဥ္ဟာ မူလေရးခဲ့စဥ္က အစီအစဥ္ (order)

နဲ႕တူခ်င္မွတူပါမယ္။

File.Copy (DirSource As String, FileSource As String, DirTarget As String, FileTarget As

String)

File ကို copy လုပ္ပါတယ္။ Assets folder ထဲကိုေတာ့ဖုိင္ေတြကို copy

လုပ္ထည့္လို႕မရပါဘူး။ Assets folder က read only ျဖစ္လို႕ပါ။

File.Delete (Dir As String, FileName As String)

File ကုိ Delete လုပ္ပါတယ္။

File.ListFiles (Dir As String) As List

Directory ထဲကဖုိင္ေတြနဲ႕ subdirectory ေတြကို List တစ္ခထ


ု ဲမွာထည့္ေပးႏုိင္ပါတယ္။

ဥပမာ DirRootExternal ထဲက file ေတြနဲ႕ subdirectory name ေတြကို List1 ထဲထည့္ရန္…

Dim List1 As List

List1 = File.ListFiles(File.DirRootExternal)

File size ကုိသိရွိရန္…

File.Size (Dir As String, FileName As String)

File ရဲ႕ size ကုိ byte နဲ႕ျပေပးပါတယ္။ Assets folder ထဲကဖိုင္အတြက္ေတာ့ ဒီ method

သံုးလို႕မရပါဘူး။

126
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
3.11.2. File Name

File name ေပးတဲ့အခါ ေအာက္ပါ character ေတြကိုသံုးႏိုင္ပါတယ္။

a to z, A to Z, 0 to 9

dot (.)

underscore (_)

ေအာက္ပါ character ေတြေတာ့မသံုးရပါဘူး။

+-%&

Spaces နဲ႕ * ?

Android file name ေတြဟာ case sensitive ျဖစ္ပါတယ္။ ဥပမာ MyFile.txt ဟာ

myfile.txt နဲ႕မတူပါဘူး။

3.11.3. Subfolder

Folder တစ္ခုေအာက္မွာ ေနာက္ထပ္ subfolder တစ္ခု create လုပ္ခ်င္ရင္

ေအာက္ပါအတုိင္းလုပ္ရပါတယ္။

File.MakeDir(File.DirInternal, "Pictures")

DirInternal ေအာက္မွာ „Pictures‟ ဆိုတဲ့ subfolder တစ္ခု create လုပ္လိုက္တာပါ။

Subfolder ကိုသံုးခ်င္ရင္ subfolder name ေရွ႕မွာ „/‟ ကိုေရးရမယ္။ ဥပမာ „Pictures‟

subfolder ထဲက test1.png file ကုိ ImageView1 မွာတင္ခ်င္ရင္ေအာက္ပါအတုိင္းေရးရမယ္။

ImageView1.Bitmap = LoadBitmap(File.DirInternal & "/Pictures", "test1.png")

သို႕မဟုတ္ subfolder name နဲ႕ file name ၾကားမွာ „/‟ ထည့္ေပးရမယ္။ ဥပမာ

ImageView1.Bitmap = LoadBitmap(File.DirInternal, "Pictures/test1.png")

127
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
3.12. TextReader/TextWriter

File ေတြ read/write လုပ္ဘို႕ File object မသံုးဘဲ TextReader နဲ႕ TextWriter object

ေတြကိုလည္းသံုးႏုိင္ပါတယ္။ TextReader နဲ႕ TextWriter ကေတာ့ file ေတြအတြက္သာမက အျခား

stream အတြက္လည္းသံုးႏုိင္ပါတယ္။ ဥပမာ Network, Bluetooth ကေန data ေတြ transfer

လုပ္ရာမွာလည္း TextReader နဲ႕ TextWriter ကိုသံုးႏုိင္ပါတယ္။ ဖုိင္ size

အၾကီးၾကီးေတြနဲ႕အလုပ္လုပ္တဲ့အခါ TextReader နဲ႕ TextWriter ကိုသံုးျခင္းဟာ File read/write

method ေတြသံုးတာထက္ပိုေကာင္းပါတယ္။ File methods ေတြကေတာ့ file တစ္ခုလံုးကုိဖတ္ျပီး

file contents ေတြကို memory ေပၚမွာ store လုပ္ပါတယ္။ ဖုိင္ေသးရင္ေတာ့

ဒီနည္းကအဆင္ေျပပါတယ္။ ဒါေပမယ့္ 1/2 MB ထက္ၾကီးတဲ့ ဖုိင္ေတြဆိုရင္ေတာ့

TextReader/TextWriter ကပိုအဆင္ေျပပါတယ္။

3.12.1. TextWriter

TextWriter ဆိုတာ output stream မွာ write လုပ္ရန္အသံုးျပဳရမယ့္ object type

ျဖစ္ပါတယ္။

Initialize

TextWriter object ကို output stream နဲ႕ initialize လုပရ


္ ပါမယ္။ Output stream ဆိုတာ

data ကို write လုပ္ရမယ့္ေနရာ (ဖုိင)္ ျဖစ္ပါတယ္။

TextWriter.Initialize (OutputStream As OutputStream)

ဥပမာ

Dim Writer As TextWriter

Writer.Initialize(File.OpenOutput(File.DirRootExternal, "Test.txt" , False))

128
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Initialize2

Encoding ပါသတ္မွတ္ေပးခ်င္ရင္ေတာ့ Initialize2 method ကိုသံုးရမယ္။ Encoding

ဆိုတာ CodePage (CharacterSet) ျဖစ္ပါတယ္။

TextWriter.Initialize2 (OutputStream As OutputStream , Encoding As String)

ဥပမာ

Dim Writer As TextWriter

Writer.Initialize2(File.OpenOutput(File.DirRootExternal,"Test.txt", False)," ISO-

8859-1")

Write (Text As String)

Text ကိo
ု utput stream မွာေရးပါတယ္။

WriteLine (Text As String)

Text ကုိ output stream မွာေရးျပီးရင္ေနာက္တစ္ေၾကာင္းဆင္းပါတယ္။

WriteList (List As List)

List ထဲက item တစ္ခုကို တစ္လုိင္းႏႈန္းနဲ႕ output stream မွာ ေရးပါတယ္။ List ထဲက

value ေတြကို string အျဖစ္ေျပာင္းေပးသြားပါလိမ့္မယ္။

Close

ေရးလို႕ျပီးရင္ output stream ကုိ ပိတ္ေပးရန္ျဖစ္ပါတယ္။ ဥပမာ

Dim Writer As TextWriter

Writer.Initialize(File.OpenOutput(File.DirDefaultExternal, "Text.txt", False))

Writer.WriteLine("This is the first line")

Writer.WriteLine("This is the second line")

Writer.Close

129
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
3.12.2. TextReader

Input stream မွ read လုပ္ရာတြင္အသံုးျပဳရမယ့္ object type ျဖစ္ပါတယ္။ TextReader

ကို input stream နဲ႕ initialize လုပရ


္ ပါတယ္။ Input stream ဆိုတာ data ေတြဖတ္ယူရမယ့္ေနရာ

(ဖုိင)္ ျဖစ္ပါတယ္။

Initialize

TextReader object ကုိ input stream နဲ႕ initialize လုပ္ေပးရပါတယ္။

TextReader.Initialize (InputStream As InputStream)

ဥပမာ

Dim Reader TextReader

Reader.Initialize(File.OpenInput(File.DirRootExternal, "Test.txt"))

Initialize2

Encoding ပါသတ္မွတ္ေပးခ်င္ရင္ေတာ့ Initialize2 method ကိုသံုးရပါတယ္။

TextReader.Initialize2 (InputStream As InputStream, Encoding As String)

ဥပမာ

Dim Reader TextReader

Reader.Initialize(File.OpenInput(File.DirRootExternal, "Test.txt"), "ISO-8859-1")

ReadAll As String

Input stream မွာရွိတဲ့ text အားလံုးကိုဖတ္ျပီး stream ကုိပိတ္ပါတယ္။ ဥပမာ

txt = Reader.ReadAll

130
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ReadLine As String

Input stream ထဲက text တစ္လိုင္းကိုဖတ္ပါတယ္။ ဖတ္စရာမရွိေတာ့ရင္ Null ကုိ return

ျပန္ပါတယ္။ ဥပမာ

Dim Reader As TextReader

Reader.Initialize(File.OpenInput(File.DirDefaultExternal, "Text.txt")

Dim line As String

line = Reader.ReadLine

Do While line <>Null

Log(line)

line = Reader.ReadLine

Loop

Reader.Close

ReadList As List

Input stream မွာရွိတဲ့ text ေတြကိုဖတ္ျပီး list ထဲကိုထည့္ပါတယ္။ အားလံုးျပီးရင္ list ကုိ

return ျပန္ပါတယ္။ ဥပမာ

List1 = Reader.ReadList

Close

Input stream ကဖတ္ျပီးရင္လည္း stream ကို close လုပ္ရမယ္။ ဥပမာ

TextReader.Close

3.13. Text Encoding (Character Encoding)

Text encoding/character encoding ဆိုတာ character တစ္ခုကို code number

တစ္ခုနဲ႕သတ္မွတ္ေပးျခင္းျဖစ္ပါတယ္။ Character set (charset), character map သုိ႕မဟုတ္ code

131
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
page လို႕လည္းေခၚၾကပါေသးတယ္။ Android ရဲ႕ default character set ကေတာ့ Unicode UTF-8

ျဖစ္ပါတယ္။ Windows မွာ အသံုးမ်ားတဲ့ character sets ေတြကေတာ့ ASCII နဲ႕ ANSI ပါ။

 ASCII မွာ character 128 လံုးအတြက္ code ေတြသတ္မွတ္ထားပါတယ္။ ဥပမာ A ရဲ႕

ASCII code က 65 ျဖစ္ျပီး B က 66 ျဖစ္ပါတယ္။ 128 လံုးထဲမွာ 33 လံုးက မျမင္ရတဲ့

control character ေတြျဖစ္ၾကပါတယ္။ ဥပမာ Esc, Tab, Ctrl, Alt

အစရွိသည္တို႕ျဖစ္ပါတယ္။

 ANSI, Windows-1252/CP-1252 ေတြကေတာ့ Latin အကၡရာေတြအတြက္ character

encoding ျဖစ္ပါတယ္။ Character 256 ခုအတြက္ျဖစ္ပါတယ္။ ပထမဆံုး 128 ခုက ASCII

encoding နဲ႕တူတူဘဲျဖစ္ပါတယ္။ ဥပမာ ASCII မွာ A ရဲ႕ code က 65 ပါ။ ANSI,

Windows-1252 နဲ႕ CP-1252 မွာလည္း A ရဲ႕ code က 65 ပါဘဲ။

Windows program ေတြက generate လုပ္တဲ့ file ေတြကို ANSI character-set နဲ႕

encode လုပ္ပါတယ္။ ဥပမာ Excel file နဲ႕ Notepad file ေတြကို ANSI character-set နဲ႕ encode

လုပ္ပါတယ္။ ဒါေပမယ့္ Notepad file ေတြကို UTF-8 encoding နဲ႕ေျပာင္း save လို႕ရပါတယ္။

Android မွာေအာက္ပါ character set ေတြကိုအသံုးျပဳႏုိင္ပါတယ္။

 UTF-8 default character-set

 UTF -16

 UTF - 16 BE

 UTF - LE

 US-ASCII ASCII character set

 ISO-8859-1 (ANSI character-set နဲ႕ေတာ္ေတာ္ေလးတူပါတယ္)

 Windows-1252

132
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ANSI နဲ႕ encode လုပ္ထားတဲ့ Windows file ေတြကိုဖတ္ဘို႕ဆိုရင္ Windows-1252

character-set ကိုသံုးရမယ္။ Windows မွာသံုးမယ့္ ဖုိင္ေတြေရးမယ္ဆိုရင္လည္း Windows-1252

character-set ကိုသံုးရမယ္။

Windows နဲ႕ Andoid ျခားနားခ်က္ကေတာ့ line တစ္လိုင္းရဲ႕အဆံုးသတ္တဲ့ပံုစံပါ။

 Android က line ရဲ႕အဆံုးမွာ LF (Line Feed) character ကိုဘဲထည့္ပါတယ္။

 Windows ကေတာ့လိုင္းရဲ႕အဆံုးမွာ character ၀ခုထည့္ပါတယ္။ CR (Carriage Return)

နဲ႕ LF တုိ႕ျဖစ္ပါတယ္။ ဒါေၾကာင့္ Windows မွာသံုးမယ္ဖိုင္ေတြေရးတဲ့အခါ CR

ကိထ
ု ပ္ထည့္ေပးရမယ္။

File ေတြကိုမတူညီတဲ့ endcoding နဲ႕ေရး/ဖတ္မယ္ဆိုရင္ TextReader/TextWriter object

ေတြကိုသံုးရမယ္။ Initialize2 method ကုိသံုးရမယ္။

Excel file တစ္ခုကိုနမူနာဖတ္ျပပါမယ္။

 Excel file တစ္ခုကို text editor တစ္ခုခု ဥပမာ (NotePad or Notepad++) နဲ႕ဖြငပ
့္ ါ။

 ျပီးရင္ ဖုိင္ကို UTF-8 encoding နဲ႕ save လုပ္ပါ။

သို႕မဟုတ္ ေအာက္ပါနည္းလမ္းအတုိင္းေဆာင္ရြက္ပါ။

 Excel file ကုိ TextReader.Initialize2 method နဲ႕ဖတ္ပါ။ "Windows-1252" encoding

ကိုသံုးပါ။

 Standard Android encoding ကိုသံုးျပီး TextWriter.Initialize method နဲ႕ျပန္ save

လုပ္ပါ။

 ဖုိင္ကို LoadCSV/LoadCSV2 mehtod နဲ႕ဖတ္ပါ။

133
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
နမူနာ code segment ကိုေအာက္မွာျပထားပါတယ္။

Dim txt As String

Dim tr As TextReader

tr.Initialize2(File.OpenInput(File.DirAssets, "Test.csv"), "Windows-1252")

txt = tr.ReadAll

tr.Close

Dim tw As TextWriter

tw.Initialize(File.OpenOutput(File.DirInternal, "Test.csv", False))

tw.Write(txt)

tw.Close

lstTest = StrUtil.LoadCSV2(File.DirInternal, "Test.csv", ";", lstHead)

File တစ္ခုကို NotePad နဲ႕ save လုပ္ရင္ byte ၁ခုထပ္ထည့္ပါတယ္။ အဲဒီ byte ေတြကို

BOM (Byte Order Mark)character လို႕ေခၚပါတယ္။ UTF-8 enconding မွာ ဒီ byte sequence

ေတြကို 0xEF,0xBB,0xBF နဲ႕ေဖာ္ျပပါတယ္။ Text editor သို႕မဟုတ္ web browser (Windows-

1252 encoding) ကေတာ့ ဒီ text ကုိ  အျဖစ္ေဖာ္ျပပါတယ္။ ဒီျပသနာကိုေျပလည္ေအာင္

NotePad အစား NotePad++ ကုိသံုးျပီး UTF-8 without BOM encoding ကုိအသံုးျပဳပါ။

Windows-1252 နဲ႕ encode လုပ္ထားတဲ့ text ကို UTF-8 encoding

ေျပာင္းဘို႕ေအာက္ပါနည္းလမ္းနဲ႕ေဆာင္ရြက္ႏုိင္ပါတယ္။

Dim var, result As String

var = "Gestió"

Dim arrByte() As Byte

arrByte = var.GetBytes("Windows-1252")

result = BytesToString(arrByte, 0, arrByte.Length, "UTF8")

134
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
3.14. List

List ဆိုတာ Dynamic Array ပံုစံမ်ိဳးပါဘဲ။ List ထဲမွာမည္သည့္ object type

မဆိုသိမ္းႏုိင္ပါတယ္။ ဒါေပမယ့္ List ကုိ Process Global object အျဖစ္ declare လုပ္ထားရင္ေတာ့

Activity object ေတြ (ဥပမာ view) ကိုမသိမ္းႏုိင္ဘူး။ ေနာက္တစ္ခုက Basic4android ဟာ Array

ေတြကို List အျဖစ္ အလိုလို ေျပာင္းေပးပါတယ္။ ဒါေၾကာင့္ List parameter လိုအပ္တဲ့ေနရာမွာ

Array ကို pass လုပ္ေပးလို႕လည္းရပါတယ္။ Basic4android က Array ကို List

အျဖစ္ေျပာင္းေပးသြားပါလိမ့္မယ္။ ပထမဆံုးထည့္တဲ့ object က position 0 မွာရွိျပီး

ေနာက္ထပ္ထည့္တဲ့ object ေတြက position 1, 2, … အစရွိသျဖင့္အစဥ္အတုိင္း List ထဲကိုေရာက္ရွိ

သြားမွာျဖစ္ပါတယ္။

Initialize

List ကိုအသံုးမျပဳမီ initialize လုပ္ရမယ္။ Empty List တစ္ခုကို initialize လုပ္ရန္…

Dim List1 As List

List1.Initialize

Initialize2

List ကုိ value ေတြနဲ႕ initialize လုပ္ဘုိ႕ Initialize2 method ကိုသံုးပါ။ Array ကေန list

ကုိေျပာင္းဘုိ႕ ဒီ method ကုိသံုးႏုိင္ပါတယ္။

ဥပမာ ဿ

Dim List1 As List

List1.Initialize2(Array As Int(1, 2, 3, 4, 5))

ဥပမာ ၀

Dim List1 As List

Dim SomeArray(5) As String

135
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
SomeArray = Array As String (“Android”,”iOS”,”Windows”,”DOS”,”UNIX”)

List1.Initialize2(SomeArray)

Add (item As Object)

List ထဲကို item ထည္ဘ


့ ို႕ Add method ကိုသံုးပါ။

Add (item As Object)

ဥပမာ List1 ရဲ႕အဆံုးမွာ Value ကုိထည္ရ


့ န္။

List1.Add(Value)

AddAll (Array As String("value1", "value2"))

List1 ရဲ႕အဆံုးေနရာမွာ array ထဲက element အားလံုးကိုထည့္ဘုိ႕ AddAll method

ကုိသံုးပါ။

AddAll (Array As String("value1", "value2"))

ဥပမာ

List1.AddAll(Array As String("value1", "value2"))

List1.AddAll(List2) ဆိုရင္ List1 ထဲကို List2 ရဲ႕ element ေတြထည့္တယ္။

List1.AddAll(Array As Int(1, 2, 3, 4, 5)) ဆုိရင္ List1 ထဲကို array ထဲက element

ေတြထည့္တယ္။

AddAllAt (Index As Int, List As List)

Array element အားလံုးကို list ထဲထည့္မယ္။ Index မွာေပးထားတဲ့ေနရာကစျပီးထည့္မယ္။

List1.AddAll(12, List2) ဆိုရင္ List1 ရဲ႕ index 12 ကစျပီး List2 ရဲ႕ item

ေတြကိုထည့္မယ္။

List1.AddAllAt(12, Array As Int(1, 2, 3, 4, 5)) ဆိုရင္ List1 ရဲ႕ index 12 ကစျပီး Array

element ေတြျဖစ္တဲ့ 1, 2, 3, 4, 5 ကိုထည့္မယ္။

136
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
InsertAt (Index As Int, Item As Object)

List ထဲမွာ item တစ္ခုကို insert လုပ္ဘို႕ InsertAt method ကုိသံုးပါ။

List1.InsertAt(Index As Int, Item As Object)

ဥပမာ List1 ရဲ႕ position 12 မွာ Value ကုိထည့္ရန္…

List1.InsertAt(12, Value)

List1 ရဲ႕ position 12 မွာ Value ကိုထည့္မယ္။ ဒါဆိုရင္ မူလ position 12 မွာရွိေနတဲ့ item

က position 13 ကုိေရႊ႕သြားမယ္။ မူလ position 13 မွာရွိေနတဲ့ item ကလည္း position 14

ကိုေရႊ႕သြားမယ္။ ဒီလိုနဲ႕တစ္ခုစီေနာက္ကိုေရႊ႕သြားပါလိမ့္မယ္။

RemoveAt (Index As Int)

List ထဲက item တစ္ခုကို remove လုပ္ဘုိ႕ RemoveAt method ကုိသံုးပါ။

RemoveAt (Index As Int)

ဥပမာ

List1.RemoveAt(12)

List1 ထဲက index 12 မွာရွိတဲ့ item ကုိ remove လုပ္ပါတယ္။

Size

List ရဲ႕ size ကုိသိခ်င္ရင္…

List1.Size

List1 ထဲမွာရွိတဲ့ item အေရအတြက္ကိုရရွိမွာျဖစ္ပါတယ္။

Get (Index As Int)

List ထဲက item တစ္ခုကိုရခ်င္ရင္…

Get(Index As Int)

137
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ဥပမာ

number = List1.Get(12)

List1 ရဲ႕ position 12 မွာ ရွိတဲ့ object က number ထဲကိုေရာက္ရွိလာမွာျဖစ္ပါတယ္။

List ထဲက item ေတြကိုတစ္ခုျပီးတစ္ခုရခ်င္ရင္ေတာ့ For Next loop ကုိသံုးႏုိငပ


္ ါတယ္။

ဥပမာ

Dim number As Int

For i = 0 To List1.Size - 1

number = List1.Get(i)

Log(number)

Next

အထက္ပါ code ကုိ run ရင္ Logs area မွာ List1 ထဲရွိသမွ် object ေတြကိုျပပါလိမ့္မယ္။

WriteList (Dir As String, FileName As String, List As List)

List ထဲက item ေတြကိုဖုိင္မွာသိမ္းႏုိင္ပါတယ္။

File.WriteList(Dir As String, FileName As String, List As List)

ဥပမာ File.WriteList(File.DirRootExternal, "Test.txt", List1)

အထက္ပါ code ဟာ List1 ရဲ႕ item ေတြကို SD card ထဲက Test.txt file မွာေရးပါတယ္။

ReadList (Dir As String, FileName As String)

ဖိုင္ကဖ
ို တ္ျပီး List ထဲထည့္လို႕လည္းရပါတယ္။

File.ReadList (Dir As String, FileName As String)

ဥပမာ List1 = File.ReadList(File.DirRootExternal, "Test.txt")

အထက္ပါ code ဟာ SD card ထဲက Test.txt ဖုိင္ကိုဖတ္ျပီး List1 ထဲထည့္တယ္။

138
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Set (Index As Int, Item As Object)

List ရဲ႕ item တစ္ခုကို အျခားတစ္ခုနဲ႕အစားထိုးႏုိင္တယ္။

List1.Set(Index As Int, Item As Object)

ဥပမာ

List1.Set(12, Value)

အထက္ပါ code ဟာ List1 ရဲ႕ position 12 မွာရွိတဲ့ object ကုိဖ်က္ျပီး Value

နဲ႕အစားထုိးပါတယ္။

Sort (Ascending As Boolean)

List ထဲက item ေတြကိုစီႏုိင္တယ္။ Item ေတြက number/string ေတြျဖစ္ရမယ္။

List1.Sort(True) (ငယ္စဥ္ၾကီးလိုက္စီမယ္)

List1.Sort(False) (ၾကီးစဥ္ငယ္လိုက္စီမယ္)

SortCaseInsensitive(Ascending As Boolean)

List ကုိ ငယ္စဥ္ၾကီးလုိက/္ ၾကီးစဥ္ငယ္လုိက္ စီရန္ျဖစ္ပါတယ္။ Ascending မွာ True ဆိုရင္

ငယ္စဥ္ၾကီးလိုက္၊ False ဆုိရင္ ၾကီးစဥ္ငယ္လိုက္စီမယ္။ Case အၾကီးနဲ႕အေသးကို တူတူဘဲလုိ႕

ယူဆမယ္။ ဥပမာ

List1.SortCaseInsensitive(True) (ငယ္စဥ္ၾကီးလိုက္စီမယ္)

List1.SortCaseInsensitive(False) (ၾကီးစဥ္ငယ္လိုက္စီမယ္)

List ကုိ Clear လုပ္ရန္

List ထဲက item အားလံုးကိုဖ်က္ရန္…

List1.Clear

139
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
3.15. Map

Map ဆိုတာ data ေတြကို key/value အစံုလိုက္သိမ္းႏိုငတ


္ ဲ့ေနရာတစ္ခုျဖစ္ပါတယ္။ Key

ဟာ unique ျဖစ္ရပါမယ္။ ဆိုလိုတာက key/value တစ္စံုကို Map ထဲမွာထည့္ျပီးသားဆိုရင္ အဲဒီ key

နဲ႕ ေနာက္ထပ္ entry တစ္ခုထပ္ထည္လ


့ ို႕မရပါ။ ထည္ရ
့ င္ မူလ entry ပ်က္ျပီး ေနာက္ entry

ဘဲက်န္ပါလိမ့္မယ္။

Key ဟာ string/number တစ္ခခ


ု ုျဖစ္ရမယ္။ Value ကေတာ့ၾကိဳက္ရာ object

တစ္ခုျဖစ္ႏုိင္တယ္။ Map ေတြဟာ List နဲ႕ဆင္တူပါတယ္။ Map ကုိ process global variable အျဖစ္

declare လုပ္ထားရင္ Activity object ေတြ (ဥပမာ view) ကိထ


ု ည့္လို႕မရပါဘူး။ Application

setting ေတြသိမ္းဘုိ႕အတြက္ Map ကိုအသံုးျပဳေလ့ရွိပါတယ္။

Initialize

Map ကိုမသံုးမီ initialize လုပ္ရမယ္။

Dim Map1 As Map

Map1.Initialize

Put(Key As Object, Value As Object)

Map ထဲကို entry တစ္ခုထည့္ခ်င္ရင္…

Put(Key As Object, Value As Object) ကိုသံုးရမယ္။ ဥပမာ

Map1.Put("Language", "English")

Key က Language, Value က English ျဖစ္ပါတယ္။ အဲဒီ Key/value တစ္စဟ


ံု ာ Map1 ရဲ႕

entry တစ္ခုျဖစ္ပါတယ္။

Get(Key As Object)

Map ထဲက entry ကုိယူခ်င္ရင္…

Get(Key As Object) သံုးရမယ္။ ဥပမာ

Language = Map1.Get("Language")

140
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
GetKeyAt(Index As Int)

Map ထဲက key ကုိယူခ်င္ရင္…

GetKeyAt(Index As Int) ကိုသံုးရမယ္။

ဥပမာ Map1 ထဲက index 12 မွာရွိတဲ့ Key ကုရ


ိ န္…

Key = Map1.GetKeyAt(12)

GetValueAt(Index As Int)

Map ထဲက value ကုိယူခ်င္ရင္…

GetValueAt(Index As Int) ကိုသံုးရမယ္။

ဥပမာ Map1 ထဲက index 12 မွာရွိတဲ့ Value ကုိယရ


ူ န္…

Value = Map1.GetValueAt(12)

ContainsKey(Key As Object)

Map ထဲမွာ ကိုယ္လိုခ်င္တဲ့ key ရွိမရွိစစ္ခ်င္ရင္…

ContainsKey(Key As Object) ကိုသံုးတယ္။ ရွိရင္ True ကုိ retrun ျပန္မယ္။

ဥပမာ Map1 ထဲမွာ Language ဆိုတဲ့ Key ရွိသလားစစ္ျပီး ရွိပါက ရွိေၾကာင္း message box

ျပရန္…

If Map1.ContainsKey("Language") Then

Msgbox("There is already an entry with this key !", "ATTENTION")

Return

End If

Remove(Key As Object)

Map ထဲက entry ကို remove လုပ္ခ်င္ရင္…

Remove(Key As Object) ကိုသံုးတယ္။

141
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ဥပမာ Map1 ထဲက Key = „Language‟ ျဖစ္ေသာ entry ကုိဖ်က္ရန္…

Map1.Remove("Language")

Clear

Map ထဲက item အားလံုးကို ဖ်က္ခ်င္ရင္ Clear ကိုသံုးတယ္။ ဥပမာ

Map1.Clear

WriteMap(Dir As String, FileName As String, Map As Map)

Map ထဲက item ေတြကုိ file တစ္ခုမွာ save လုပ္ခ်င္ရင္…

File.WriteMap(Dir As String, FileName As String, Map As Map) ကိုသံုးတယ္။ ဥပမာ

File.WriteMap(File.DirInternal, "settings.txt", mapSettings)

ReadMap(Dir As String, FileName As String)

File တစ္ခုကိုဖတ္ျပီး Map ထဲမွာထည့္ခ်င္ရင္…

ReadMap(Dir As String, FileName As String) ကိုသံုးတယ္။ File ကုိဖြင့္ျပီး တစ္လိုင္းကို

key-value တစ္စံုအျဖစ္ဖတ္တယ္။ ဥပမာ

mapSettings = File.ReadMap(File.DirInternal, "settings.txt")

အထက္ပါ code ဟာ settings.txt file ကုိဖတ္ျပီး တစ္ေၾကာင္းကို entry တစ္ခုအျဖစ္

„mapSettings‟ Map ထဲကိုထည့္ပါတယ္။

ReadMap2(Dir As String, FileName As String, Map As Map)

File.ReadMap2(Dir As String, FileName As String, Map As Map)

ကိုလည္းသံုးႏိုငတ
္ ယ္။

ဥပမာ

mapSettings = File.ReadMap2(File.DirInternal, "settings1.txt", mapSettings)

142
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Example 3.4

File, TextWriter, TextReader, List, Map object မ်ားအသံုးျပဳပံုကိုေလ့လာဘုိ႕အတြက္

project တစ္ခုလုပ္ပါ။

Sub Activity_Create(FirstTime As Boolean)

If File.ExternalWritable = False Then [SD card writable မျဖစ္ရင္]

Msgbox("Cannot write on storage card.", "") [Error message ျပမယ္]

Return [Program ကေနထြက္မယ္]

End If

SaveStringExample

ReadStringExample

WriteListExample

ReadListExample [Sub ေတြကိုေခၚမယ္]

WriteMapExample

ReadMapExample

WriteTextWriter

ReadTextReader

End Sub

SaveStringExample

This is some string

And this is another one.

ဆိုတဲ့ စာ၀ေၾကာင္းကို SD card ရဲ႕ root folder ေအာက္က String.txt file မွာေရးမယ္။ String.txt

file မရွိရင္လည္း create လုပ္ေပးလိမ့္မယ္။

143
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub SaveStringExample

File.WriteString(File.DirRootExternal, "String.txt", _

"This is some string" & CRLF & "and this is another one.")

End Sub

ReadStringExample

String.txt file မွာေရးထားတဲ့စာေတြကိုဖတ္ျပီး message box မွာျပမယ္။

Sub ReadStringExample

Msgbox(File.ReadString(File.DirRootExternal, "String.txt"), "")

End Sub

WriteListExample

List1 မွာ 1 ကေန 100 ထိထည့္မယ္။ ျပီးရင္ List1 ထဲက item ေတြကို SD card ရဲ႕ root

folder ေအာက္က List.txt file မွာေရးမယ္။ ေရးတဲ့အခါ item တစ္ခုကို

တစ္ေၾကာင္းႏႈန္းနဲ႕ေရးပါတယ္။

Sub WriteListExample

Dim List1 As List

List1.Initialize

For i = 1 To 100

List1.Add(i)

Next

File.WriteList(File.DirRootExternal, "List.txt", List1)

End Sub

144
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ReadListExample

List.txt file က text ေတြကိုဖတ္ျပီး တစ္လိုင္းကို item တစ္ခုအျဖစ္ List1 ထဲထည့္ပါတယ္။

List1 ရဲ႕ size, List1 ရဲ႕ position ၁ခုေျမာက္မွာရွိတဲ့ item ကုိ message box မွာျပပါတယ္။ List1 မွာ

item ေတြထည့္တဲ့အခါ ပထမဆံုး item ရဲ႕ postion က 0 ျဖစ္ပါတယ္။ ဒါေၾကာင့္ တတိယေျမာက္

item ရဲ႕ position က 2 ျဖစ္ပါတယ္။ (မွတ္ခ်က္ List1 ကုိ initialize လုပ္ဘို႕မလိုပါ။ List1 မွာ file

ကရလာတဲ့ item ေတြကိုသာထည့္မွာမုိ႕ပါ။)

Sub ReadListExample

Dim List1 As List

List1 = File.ReadList(File.DirRootExternal, "List.txt")

Msgbox("List1.Size = " & List1.Size & " Third item is:" & List1.Get(2), "")

End Sub

WriteMapExample

Map မွာ data ထည့္တဲ့အခါ entry တစ္ခုမွာ key-value တစ္စံုစီပါရပါတယ္။ ဥပမာ

Brand Samsung

Model GT5900 Three entries

Price 100000

Keys Values

အထက္ပါ map မွာ entry ၁ခုရွိပါတယ္။ Entry တစ္ခုမွာ key-value တစ္စံုပါပါတယ္။ ဥပမာ

(Brand Samung) entry မွာ Brand ဟာ key ျဖစ္ျပီး Samsung ဟာ value ျဖစ္ပါတယ္။

145
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
WriteMapExample sub မွာ For-Next loop ကိသ
ု ံုးျပီး Map1 ထဲကို ေအာက္ပါ entry

ေတြထည့္ပါတယ္။

Key1 Value1

Key2 Value2

… …

… …

… …

Key100 Value100

Map1 ထဲက entry ေတြကို Map.txt file မွာေရးပါမယ္။ ေရးတဲ့အခါ entry တစ္ခုကို

တစ္လုိင္းႏႈန္းနဲ႕ေရးမွာျဖစ္ပါတယ္။

Sub WriteMapExample

Dim Map1 As Map

Map1.Initialize

For i = 1 To 10

Map1.Put("Key" & i, "Value" & i)

Next

File.WriteMap(File.DirRootExternal, "Map.txt", Map1)

End Sub

ReadMapExample

Map.txt file ကုိဖတ္ျပီး Map1 ထဲမွာထည့္ပါမယ္။ Map1 ထဲမွာ ဖိုင္ရဲ႕ content

ေတြကိုထည့္မွာမို႕ initialize လုပ္စရာမလိုပါဘူး။

146
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Map1 ထဲက entry တစ္ခုရဲ႕ key ကုိဖတ္ခ်င္ရင္…

Map1.GetKeyAt(i)

လုိ႕ဖတ္ရပါတယ္။ (i ေနရာမွာ ဖတ္ခ်င္တဲ့ entry ရဲ႕ index ဥပမာ 0, 1, 2 ေရးပါ)

Map1 ထဲက entry တစ္ခုရဲ႕ value ကိုဖတ္ခ်င္ရင္…

Map1.GetValeAt(i)

လုိ႕ဖတ္ရပါတယ္။

Sub ReadMapExample

Dim Map1 As Map

Dim K, V As String

Map1 = File.ReadMap(File.DirRootExternal, "Map.txt")

K = Map1.GetKeyAt(0)

V = Map1.GetValueAt(0)

Msgbox(K,V)

End Sub

Map ရဲ႕ position 0 မွာ entry ကေအာက္ပါအတုင


ိ ္းျဖစ္ပါတယ္။

Key1 Value1

ဒါေၾကာင့္ ReadMapExample sub ကုိေခၚရင္ (ပံု ၁-၃) မွာျပထားတဲအ


့ တုိင္း message box

ကိုေတြ႕ရမယ္။

147
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၁-၃

WriteTextWriter

TextWriter object သံုးျပီး string ေတြကို ဖုိင္တစ္ခုမွာ ေရးပါမယ္။ TextWriter1 object ကုိ

declare လုပ္ပါ။ Initialize လုပ္တဲ့အခါ parameter တစ္ခုလိုမယ္။ လိုတဲ့ parameter ကေတာ့

output stream ျဖစ္ပါတယ္။ Output stream ရဘို႕အတြက္ OpenOutput method ကိုေခၚရမယ္။

ဒီ method မွာ write လုပ္မယ့္ဖုိင္ရဲ႕ folder name, file name, append လုပ္မယ္/မလုပ္ဘူး

(True/False) ေပးရမယ္။ ျပီးရင္ ဒီ output stream ကုိသံုးျပီး TextWriter1 ကုိ initialize လုပ္ရမယ္။

For-Next loop ကုိသံုးျပီး Text.txt file မွာ Line1 ကေန Line10 ထိ တစ္လို္င္းစီေရးပါမယ္။ ဥပမာ

Line1

Lint2

Line10

ေရးျပီးရင္ေတာ့ TextWriter1 ကုိပိတ္ေပးရပါမယ္။

Sub WriteTextWriter

Dim TextWriter1 As TextWriter

TextWriter1.Initialize(File.OpenOutput(File.DirRootExternal,"Text.txt", False))

148
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
For i = 1 To 10

TextWriter1.WriteLine("Line" & i)

Next

TextWriter1.Close

End Sub

ReadTextReader

File တစ္ခုကို ဖတ္ဘုိ႕အတြက္ TextReader object ကုိ declare လုပ္ျပီး initialize

လုပ္ရမယ္။ InputStream ကုရ


ိ ဘုိ႕ OpenInput method ကုိသံုးရမယ္။ ReadLine method

ကုိသံုးျပီး ဖိုင္ကို တစ္ၾကိမ္မွာ တစ္လုိင္းဖတ္ပါမယ္။ ဖုိင္ရဲ႕အဆံုးကိုေရာက္သည္ထိ Do While loop

ကိုသံုးျပီးဖတ္ပါမယ္။ ဖုိင္ရဲ႕အဆံုးေရာက္မေရာက္ကိုေတာ့ (line <> Null) နဲ႕စစ္ပါတယ္။

ဖတ္လို႕ရတဲ့ စာကို line ထဲမွာထည့္ထားတာဆိုေတာ့ line ထဲမွာ Null ျဖစ္ေနရင္

ဖုိင္ရဲ႕အဆံုးကိုေရာက္သြားလို႕ ျဖစ္ပါတယ္။ ဒါေၾကာင့္ line ထဲမွာ Null မျဖစ္မခ်င္း Do While loop

ကိုပတ္ျပီး ဖုိင္ကို တစ္လိုင္းစ ီဖတ္ေနမွာျဖစ္ပါတယ္။ ဖတ္လို႕ရတဲ့ တစ္လုိင္းခ်င္းကိုလည္း Logs area

မွာျပေပးမွာျဖစ္ပါတယ္။ ျပီးရင္ေတာ့ TextReader ကုိပိတ္ေပးရပါမယ္။

Sub ReadTextReader

Dim TextReader1 As TextReader

TextReader1.Initialize(File.OpenInput(File.DirRootExternal, "Text.txt"))

Dim line As String

line = TextReader1.ReadLine

Do While line <> Null

Log(line)

line = TextReader1.ReadLine

Loop

149
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
TextReader1.Close

End Sub

3.16. Regular Expression

Reaular Expression ဆိုတာ string တစ္ခုမွာ eMail address ပါသလား၊ user input date

ဟာသတ္မွတ္ထားတဲ့ format အတုိင္းျဖစ္သလား၊ day ဟာ 31 ထက္မ်ားေနသလား၊ month ဟာ

12 ထက္မ်ားေနလား အစရွိတာေတြကိုစစ္ေပးႏုိင္တဲ့ method ေတြျဖစ္ပါတယ္။ Regular expression

method ေတြကို သံုးဘို႕အတြက္ Regex object ကို သံုးရမယ္။ Regular expression method

ေတြမွာ pattern string ကို parameter အျဖစ္သံုးရပါတယ္။

3.16.1. Matching the Whole String

User input မွန္မမွန္စစ္ေဆးဘုိ႕အေကာင္းဆံုး method ေတြကေတာ့ IsMatch နဲ႕

IsMatch2 တုိ႕ျဖစ္ပါတယ္။ String တစ္ခုလံုးဟာ pattern နဲ႕ match ျဖစ္ရင္ True ကုိ return

ျပန္မယ္။ ေအာက္ပါ code ကေတာ့ date string တစ္ခုဟာ **-**-**** ဆိုတဲ့ format

အတုိင္းျဖစ္ရဲ႕လားလို႕စစ္တာပါ။

Regex.IsMatch("\d\d-\d\d-\d\d\d\d", "11-15-2010")

Pattern string \d\d-\d\d-\d\d\d\d နဲ႕ date string 11-15-2010 ကိုတုိက္စစ္ရာမွာ digit

၀ခု - digit ၀ခု - digit ၂ခု ပံုစံကိုက္ေနလို႕ match ျဖစ္ပါတယ္။ ဒါေၾကာင့္ True ကုိ return

ျပန္ပါမယ္။

ေအာက္ပါ code မွာေတာ့ pattern string \d\d-\d\d-\d\d\d\d နဲ႕ date string

11\15\2010 ကိုတုိက္စစ္ရာမွာ ပံုစံမကိုက္လို႕ match မျဖစ္ပါဘူး။ ဆိုလိုတာက date string မွာ „ – „

မျဖစ္ဘဲ „ \ „ ျဖစ္ေနလို႕ပါ။ ဒါေၾကာင့္ False ကုိ return ျပန္ပါမယ္။

Regex.IsMatch("\d\d-\d\d-\d\d\d\d", "11\15\2010")

150
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
3.16.2. Splitting Text

Split နဲ႕ Split2 method ေတြဟာ ေပးထားတဲ့ pattern ကုိသံုးျပီး string ထဲမွာပါတဲံ text

ေတြကိုခြဲထုတ္ေပးပါတယ္။ ဥပမာ

Dim data As String

data = "123,432,13,4,12,534"

Dim numbers() As String

numbers = Regex.Split(",", data)

အထက္ပါ code မွာ pattern က comma ( , ) ပါ။ data ထဲက ( , ) နဲ႕ျခားထားတဲ့ text

ေတြကိုခြဲထုတ္ပါတယ္။ ခြဲလို႕ရတဲတ
့ စ္ခုခ်င္းကို „numbers‟ array ထဲထည့္ပါတယ္။

numbers(0) မွာ 123

numbers(1) မွာ 432

numbers(2) မွာ 13

numbers(3) မွာ 4

numbers(4) မွာ 12

numbers(5) မွာ 534 ေရာက္ရွိသြားမွာျဖစ္ပါတယ္။

numbers ထဲက text ေတြကို List l ထဲထည့္ပါ။ l ထဲက text ေတြကို Logs area

မွာထုတ္ၾကည့္ပါ။

Dim l As List

l.Initialize2(numbers)

Log(l)

Logs area မွာ ေအာက္ပါအတုိင္းျမင္ရမယ္။ ( , ေနာက္မွာ space တစ္ခုျခားျပီး ထုတ္ျပတာ

151
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ေတြ႕မယ္)

123, 432, 13, 4, 12, 534

ေအာက္ပါအတုိင္းလည္းသံုးႏိုင္တယ္။

numbers = Regex.Split("[,\s]", data)

numbers = Regex.Split("[,\s]+", data)

3.16.3. Find Matches in String

String တစ္ခုထဲက ကိုယ္လိုခ်င္တဲ့ text ကို ရွာႏုိင္ပါတယ္။ ဥပမာအေနနဲ႕ string

တစ္ခုထဲကေန eMail addresss ေတြကိုရွာၾကည့္ရေအာင္။ eMial address ရဲ႕ pattern

ကေအာက္ပါပံုစံမ်ိဳးျဖစ္ပါတယ္။

***@***.***

Dim data As String

data = "Please contact mike@gmail.com or john@gmail.com"

Dim matcher1 As Matcher

matcher1 = Regex.Matcher("\w+@\w+.\w+", data)

Do While matcher1.Find = True

Log(matcher1.Match)

Loop

အထက္ပါ code ကို run ရင္ေအာက္ပါအတုိင္း eMail address ကိုရွာျပီးထုတ္ျပလိမ့္မယ္။

mike@gmail.com

john@gmail.com

152
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
User input date တစ္ခုဟာ ကိုယ္လိုခ်င္တဲ့ format အတုိင္းျဖစ္ရဲ႕လား၊ ျဖစ္ခဲ့ရင္လည္း

ရက္က ၁ဿ ထက္ေက်ာ္ေနလား၊ လက ဿ၀ ထက္ေက်ာ္ေနလားဆိုတာ ေအာက္ပါအတုိင္း

စစ္ေဆးႏုိင္ပါတယ္။

SubActivity_Create(FirstTime As Boolean)

Log(IsValidDate("13-31-1212")) 'false ကို return ျပန္မယ္

Log(IsValidDate("12-31-1212")) 'true ကို return ျပန္မယ္

End Sub

Sub IsValidDate(Date As String) As Boolean

Dim matcher1 As Matcher

matcher1 = Regex.Matcher("(\d\d)-(\d\d)-(\d\d\d\d)", Date)

If matcher1.Find = True Then

Dim days, months As Int

months = matcher1.Group(1)

days = matcher1.Group(2)

If months> 12 Then Return False

If days> 31 Then Return False

Return True

Else

Return False

End If

End Sub

153
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
3.17. Intent

Application တစ္ခုကေန အျခား application ရဲ႕ Activity ကုိေခၚခ်င္ရင္ Intent object ကုိ

create လုပ္ရမယ္။ Intent object မွာ Action နဲ႕ Uri ေပးျပီး initialize လုပ္ပါ။ ျပီးရင္ StartActivity

method ကိုသံုးျပီးေခၚပါ။

Intent object ကုိသံုးျပီး အျခား application ရဲ႕ Activity ေခၚျခင္းကိုနမူနာမ်ားနဲ႕ျပပါမယ္။

Example 3.5

Calling Phone

Button ကို click တာနဲ႕ ေခၚခ်င္တဲ့ number တစ္ခုကိုဖုန္းေခၚႏုိင္တဲ့ example လုပ္ျပပါမယ္။

Button တစ္ခုပါတဲ့ layout လုပ္ပါ။ Layout name ကုိ Layout1 လို႕ေပးပါ။ Activity_Create

မွာ layout file ကုိ Activity ေပၚတင္ပါ။

Sub Activity_Create(FirstTime As Boolean)

Activity.LoadLayout("Layout1")

End Sub

Phone application ရဲ႕ Activity ကုိေခၚဘုိ႕ Intent object တစ္ခုကို declare လုပ္ျပီး

initialize လုပရ
္ မယ္။ Initialize method မွာ parameter ၀ခုေပးရမယ္။ ပထမ parameter က

Action ျဖစ္ပါတယ္။ i.ACTION_VIEW လို႕ေပးပါ။ ဒုတိယ parmater မွာ Uri (phone number)

ကုိေပးပါ။ ျပီးရင္ StartActivity method ကုိသံုးျပီး Activity ကုိေခၚပါ။

Sub Button1_Click

Dim i As Intent

i.Initialize(i.ACTION_VIEW, "tel:0973140515")

StartActivity(i)

End Sub

154
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Program ကုိ run ျပီးေနာက္ Button1 ကုိ click ရင္ device မွာပါျပီးသား phone

application ရဲ႕ Activity ကုိေခၚေပးမယ္။ အဲဒီအခါ (ပံု ၁-၄) မွာ ျပထားတဲ့အတုိင္း

ဖုန္းနံပါတ္ကိုျပမယ္။ ေခၚခ်င္ရင္ထံုးစံအတုိင္း ဖုန္းပံုေလးကိုႏွိပ္လုိက္ပါ။

ပံု ၁-၄

155
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Phone number ကိုမျပဘဲ Button1 ကုိႏွိပ္လုိက္တာနဲ႕ တန္းေခၚခ်င္ရင္ Action မွာ

i.ACTION_CALL လုိ႕ေပးပါ။

Sub Button1_Click

Dim i As Intent

i.Initialize(i.ACTION_CALL, "tel:0973140515")

StartActivity(i)

End Sub

တစ္ခုပိုတာက [Project > Manifest Editor] မွာ (ပံု ၁-၅) မွာျပထားတဲအ


့ တုိင္း permission

ထည့္ေပးရပါမယ္။

AddPermission(android.permission.CALL_PHONE)

ပံု ၁-၅

Example 3.6

Sending SMS

SMS ပို႕တဲ့ application ျပဳလုပရ


္ န္ Intent object „i‟ ကို declare လုပ္ပါ။ number ထဲမွာ

receiver ရဲ႕ phone number ကိုထည့္ပါ။ „i‟ ကုိ initialize လုပ္ပါ။ Action နဲ႕ Uri ေပးရမယ္။

ပို႕ခ်င္တဲ့ message ကို PutExtra method နဲ႕ Intent object „i‟ ထဲထည့္ပါ။ ျပီးရင္ StartActivity

method နဲ႕ SMS application ရဲ႕ Activity ကုိေခၚပါ။

156
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub Button1_Click

Dim i As Intent

Dim number = "0973140515" As String

i.Initialize(i.ACTION_VIEW, "sms:" & number)

i.PutExtra("sms_body", "this is the body")

StartActivity(i)

End Sub

Example 3.7

Web site ဖြင့္ရန္

Sub Button1_Click

Dim i As Intent

i.Initialize(i.ACTION_VIEW, "http://www.facebook.com")

StartActivity(i)

End Sub

157
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

Chapter 4

Process and Activity Life Cycle

4.1. Process

User က application တစ္ခုကို ဖြငလ


့္ ုပ္လုိက္တာနဲ႕ process တစ္ခုျဖစ္ေပၚလာပါတယ္။

Process မွာ UI thread လို႕ေခၚတဲ့ main thread တစ္ခု ရွိပါတယ္။ Process ရွိေနသမွ် main

thread လည္း ရွိေနမွာပါ။ Process တစ္ခုမွာ main thread အျပင္ background task

အတြကသ
္ ံုးႏုိငတ
္ ဲ့ အျခား thread ေတြလည္းရွိႏုိင္ပါတယ္။

Process တစ္ခု ဘယ္အခ်ိန္မွာ အဆံုးသတ္လည္းဆုိေတာ့… user/system က application

မွာပါတဲ့ Activity အားလံုးကိုပိတ္လုိက္ရင္ process အဆံုးသတ္သြားတာမ်ိဳးလည္းျဖစ္တတ္ပါတယ္။

Activity တစ္ခုသာပါတဲ့ application မွာ user က back key ကုိႏွိပ္လုိက္ရင္ activity

ပိတ္သြားပါမယ္။ ဒါေပမယ့္ process ကေတာ့ memory ေပၚမွာရွိေနႏိုင္ပါေသးတယ္။ ဖုန္းမွာ

memory လိုအပ္ခ်က္ရွိလာမွ process ကို kill လုပ္ပါလိမ့္မယ္။ User က application ကုိ

ေနာက္တစ္ၾကိမ္ ျပန္ run တဲ့အခါ ခုနက process ကိုလည္း kill မလုပ္ရေသးဘူးဆိုရင္ အဲဒီ

process ကုိဘဲ ျပန္သံုးပါလိမ့္မယ္။ (မွတ္ခ်က္ Basic4android application တစ္ခုမွာ Activity တစ္ခု

သို႕မဟုတ္ တစ္ခုထက္ ပိုျပီး ပါရွိႏိုင္ပါတယ္)

4.2. Activity

Activity ဆိုတာ Windows form ပံုစံမ်ိဳးပါ။ Activity နဲ႕ Windows form မတူတာတစ္ခု

ကေတာ့ အျခား process မ်ားအတြက္ memory လိုအပ္ခ်က္ရွိလာတဲ့အခါ background မွာေရာက္

ေနတဲ့ Activity ေတြကုိ kill လုပ္ပါတယ္။

Activity ကုိ kill မလုပ္မီသူ႕ရဲ႕ state ကို save လုပ္ထားဘို႕လုိပါတယ္။ Memory မွာေသာ္

လည္းေကာင္း၊ persistent storage (အျမဲတမ္းသိမ္းထားႏုိင္တဲ့ file/database) မွာေသာ္လည္း

ေကာင္း save လုပ္ထားႏုိင္ပါတယ္။ လိုအပ္တဲ့အခ်ိန္မွာ save လုပ္ထားတဲ့ data ေတြနဲ႕တကြ ဒီ

Activity ကုိ ျပန္ create လုပ္ႏုိင္ပါတယ္။

158
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Device ရဲ႕ orientation (ေထာင္လုိက/္ အလ်ားလုိက)္ ေျပာင္းသြားတဲ့အခါမ်ိဳးမွာ

configuration ေျပာင္းသြားမွာပါ။ Configuration ေျပာင္းသြားတဲ့အခါတုိင္းမွာ လက္ရွိ Activity ကုိ

destroy လုပ္ျပီး Activity အသစ္တစ္ခုျပန္ create လုပ္ပါတယ္။

Activity module တစ္ခုမွာပါတဲ့ Sub routine ေတြက ေအာက္ပါအတုိင္း ျဖစ္ပါတယ္။

Sub Process_Globals

End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

4.3. Variable

Variables ေတြမွာ global variable နဲ႕ local variable ဆိုျပီး ၀မ်ိဳးရွိပါတယ္။ Sub တစ္ခုအ

တြင္းမွာ declare လုပ္တဲ့ variable ေတြကုိ local variable လို႕ေခၚပါတယ္။ သူ႕ကို declare လုပ္

ထားတဲ့ sub အတြင္းမွာသာ အသံုးျပဳႏုိင္ပါတယ္။

Global variable ေတြကိုေတာ့ sub အားလံုးက access လုပ္ႏုိင္ပါတယ္။ Global variable

159
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
၀မ်ိဳး ရွိပါတယ္။ Process variable နဲ႕ Activity variable ပါ။

4.3.1. Process Variable

Process တည္ရွိေနသမွ်ကာလပတ္လံုး ဒီ variable ေတြ ရွိေနမွာပါ။ ဒီ variable ေတြကို

Sub Process_Globals

End Sub

အတြင္းမွာ declare လုပ္ရပါမယ္။ ဥပမာ

Sub Process_Globals

Dim TTS1 As TTS

End Sub

Process တစ္ခု စတင္တဲ့အခ်ိန္တုိင္းမွာ Process_Globals sub ကို ေခၚပါတယ္။ ဆိုလိုတာ

က process တစ္ခုစတင္လုိက္တာနဲ႕ Process_Globals sub ထဲက code ေတြကုိ execute လုပ္ပါ

တယ္။ ဒီ variable ေတြဟာ public variable ေတြ ျဖစ္ပါတယ္။ ဒါေၾကာင့္ ဒီ variable ေတြကို အျခား

module ေတြကပါ access လုပ္ႏုိင္ပါတယ္။ ဒါေပမယ့္ object တုိင္းကိုေတာ့ process variable

အျဖစ္ declare မလုပ္ႏုိင္ပါဘူး။ ဥပမာ view ေတြကို process variable အျဖစ္ declare မလုပ္ႏုိင္

ပါဘူး။ ဘာေၾကာင့္လည္းဆိုေတာ့ view ဆိုတာ Activity ရဲ႕ အစိတ္အပိုင္းတစ္ခုပါ။ Activity ကုိ

destroy လုပ္လုိက္ရင္ view ေတြပါ destroy ျဖစ္သြားပါမယ္။ ဒါေၾကာင့္ Activity destroy ျဖစ္တဲ့အခါ

Activity နဲ႕အတူ destroy ျဖစ္သြားမယ့္ view ေတြကို (Activity destroy ျဖစ္သြားသည့္တုိင)္

process အဆံုးမသတ္မခ်င္းရွိေနမယ့္ process global variable အျဖစ္ declare မလုပ္သင့္ပါဘူး။

လုပ္ခဲ့ရင္ေတာ့ garbage collector ဟာ view ေတြကို မရွင္းလင္း မဖယ္ရွားႏုိင္ေတာ့ဘဲ

ျဖစ္သြားမွာပါ။ အဲဒီလို မဖယ္ရွားေပးရင္ memory ေပၚမွာ (မလိုအပ္ေတာ့တ)ဲ့ resources ေတြကပါ

160
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ေနရာယူထားတဲ့အတြက္ memory မလံုေလာက္မႈကို ျဖစ္ေစႏုိင္ပါတယ္။

4.3.2. Activity Variable

Activity variable ေတြကို

Sub Globals

End Sub

ထဲမွာ declare လုပ္ရမယ္။ ဥပမာ

Sub Globals

Dim btnOk As Button

End Sub

Activity variable ေတြဟာ private ျဖစ္ပါတယ္။ Declare လုပ္ထားတဲ့ Activity module

အတြင္းမွာသာ access လုပ္ႏုိင္ပါတယ္။ View ေတြကို Activity variable အျဖစ္ declare လုပ္ရပါ

မယ္္။ Activity ကုိ create လုပ္တုိင္းမွာ Globals sub ကို ေခၚပါတယ္။ Activity ရွိေနသမွ် Activity

variable ေတြ ရွိေနမွာပါ။

4.4. Activity_Create

Activity ကုိ create လုပ္တဲ့အခ်ိန္မွာ Globals sub ကို ေခၚျပီးေနာက္ Activity_Create sub

ကုိ ေခၚပါတယ္။ Activity ကုိ ဘယ္အခ်ိန္ create လုပ္သလဲဆိုေတာ့… user က application ကုိ

ဖြငလ
့္ ုိက္တဲ့အခ်ိန္မွာ activity ကုိ create လုပ္ပါတယ္။ Device configuration change သြားတဲ့အခါ

(ေထာင္လိုက္ကေန အလ်ားလိုက္၊ အလ်ားလိုက္ကေန ေထာင္လုိက္ေျပာင္းသြားတဲ့အခါမ်ဳိး) မွာလည္း

activity ကုိ create လုပ္ပါတယ္။ ဒါေၾကာင့္ application ကုိ ဖြင့္တာနဲ႕ ေပၚလာရမယ့္ layout file ကို

161
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Activity Create sub အတြင္းမွာ load လုပ္ရပါတယ္။ ဥပမာ

Sub Activity_Create (FirstTime As Boolean)

Activity.LoadLayout(“Layout1”)

End Sub

4.5. FirstTime

FirstTime ဆုိတဲ့ parameter ကေတာ့ Activity ကုိ ပထမဆံုးအၾကိမ္ create လုပ္တာလား

ဆိုတာ ေဖာျ္ပဘုိ႕သံုးတာပါ။ FirstTime ရဲ႕ value ဟာ True or False ျဖစ္ပါမယ္။ Activity ကုိ

ပထမဆံုးအၾကိမ္ create လုပ္တာဆိုရင္ FirstTime ရဲ႕ value က True ျဖစ္မွာပါ။ Process variable

ေတြကို initialize လုပ္မယ္/မလုပ္ဘူးဆိတ


ု ာ FirstTime ေပၚ မူတည္ျပီးဆံုးျဖတ္ရပါတယ္။ ဘာ

ေၾကာင့္လည္းဆုိေတာ့ process variable ေတြကို တစ္ၾကိမ္သာ initialize လုပ္ရမွာ ျဖစ္ပါတယ္။

ဒါေၾကာင့္ process variable ေတြကို initialize လုပ္ဘုိ႕ FirstTime ရဲ႕ value ကုိ ၾကည့္ရပါတယ္္။

FirstTime ရဲ႕ value က True ျဖစ္ရင္ ဒါ Activity ကုိ ပထမဆံုးအၾကိမ္ create လုပ္တာ ျဖစ္တယ္။

ဒါေၾကာင့္ process variable ေတြကုိ initialize လုပ္ရမယ္။ ဥပမာ

Sub Activity_Create (FirstTime As Boolean)

If FirstTime Then

TTS1.Initialize(“TTS1”)

End If

End Sub

တကယ္လို႕ FirstTime ရဲ႕ value ဟာ False ျဖစ္ေနရင္ေတာ့ ဒါ Activity ကို

ပထမဆံုးအၾကိမ္ create လုပ္တာ မဟုတ္ေတာ့ဘူး။ ဒီ့မတုိင္မီက create လုပ္ျပီးသား၊ process

variable ကုိ initialize လုပ္ျပီးသားျဖစ္တယ္။ ဒါေၾကာင့္ FirstTime = False ျဖစ္ရင္ process

variable ေတြကို initialize လုပ္စရာမလိုေတာ့ပါဘူး။

162
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
4.6. Activity_Resume

Activity_Create ျပီးသြားတဲ့အခါမွာ Activity_Resume ကို ေခၚပါတယ္။ အခ်ိဳ႕အေျခအေန

ေတြမွာ Activity_Pause ျပီးရင္လည္း Activity_Resume ကို ေခၚတတ္ပါတယ္။ (Resume လုပ္တယ္

ဆိုတာ Activity ကုိ background ကေန foreground ကုိ ျပန္ပို႕တာပါ)

4.7. Activity_Pause

Activity တစ္ခုဟာ foreground ကေန background ကိုေရာက္သြားတုိင္း Activity_Pause

ကုိ ေခၚပါတယ္။ Activity_Pause ဟာ ျပန္သံုးဘုိ႕လိုအပ္တဲ့ data ေတြကို save လုပ္ႏုိင္တဲ့

ေနာက္ဆံုးေနရာပါ။

Activity state ကုိ save လုပ္ႏုိငတ


္ ဲ့ mechanism ၀မ်ိဳး ရွိပါတယ္။

 Current application နဲ႕သာ သက္ဆိုင္တဲ့ data ေတြကိုေတာ့ process variable ေတြမွာ

save ႏုိင္ပါတယ္။

 အျခား data ေတြကိုေတာ့ persistent storage (file/database) မွာ ထားသင့္ပါတယ္။

ဥပမာ user က setting အခ်ိဳ႕ကို ေျပာင္းခဲ့တယ္ဆိုရင္ ဒီေျပာင္းထားတာေတြကုိ persistent

storage မွာ save လုပ္သင့္ပါတယ္။ မ save ခဲ့ရင္ ေျပာင္းထားတဲ့ setting ေတြ ေပ်ာက္သြား

မွာျဖစ္ပါတယ္။

Activity တစ္ခုဟာ ဘယ္လိုအေျခအေနမွာ background ကုိ ေရာက္မွာလဲ။ အေျခအေန

၂မ်ိဳးေၾကာင့္ ျဖစ္ႏိုင္ပါတယ္။

1. ေနာက္ထပ္ Activity တစ္ခုကို ေခၚျခင္း

2. Home button ကို ႏွိပ္ျခင္း

3. Configuration change သြားျခင္း (ဥပမာ device orientation change သြားျခင္း)

4. Back button ကို ႏိွပ္ျခင္း

အထက္ပါအေၾကာင္း ၂ခုေၾကာင့္ လက္ရွိ Activity ဟာ foreground ကေန background ကုိ

ေရာက္သြားႏုိင္ပါတယ္ (Background ေရာက္သြားရင္ screen ေပၚမွာ မျမင္ရေတာ့ပါ)

အမွတ္စဥ္ 1 နဲ႕ 2 ေၾကာင့္ဆိုရင္ေတာ့ Activity ကုိ pause လုပ္ျပီး memory ေပၚမွာ

163
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
သိမ္းထားမယ္။ user က လိုအပ္တဲ့အခါ ျပန္သံုးႏိုင္ေအာင္ေပါ့။

အမွတ္စဥ္ 3 ေၾကာင့္ဆိုရင္ေတာ့ Activity ကို pause လုပ္ျပီးေနာက္ destroy လုပ္မယ္။

ျပီးရင္ create ျပန္လုပ္မယ္ (ေနာက္ထပ္ layout variant တစ္ခုကိုသံုးျပီး create လုပ္မွာပါ။ ဥပမာ

portrait ကေန landscape ေျပာင္းတာဆိုရင္ landscape အတြက္လုပ္ထားတဲ့ layout variant နဲဲ႕ျပန္

create လုပ္မွာပါ) Create ျပီးရင္ resume လုပ္မယ္။

အမွတ္စဥ္ 4 ေၾကာင့္ဆိုရင္ေတာ့ Activity ကို pause လုပ္ျပီးေနာက္ destroy လုပ္မယ္။

Back button ကို ႏွိပ္တာဟာ Activity ကို အျပီးပိတ္လိုက္တာပါဘဲ။ ဒါေၾကာင့္ destroy

လုပ္ပစ္တာပါ။ ဒီလိုအေျခအေနမွာေတာ့ ဘယ္ data ကုိမွ save လုပ္စရာ မလုိေတာ့ပါဘူး။

4.8. UserClosed

Back button ကုိ ႏွိပ္တာေၾကာင့္ pause ျဖစ္တာဆုိရင္ UserClosed parameter ဟာ True

ျဖစ္ျပီး အျခားအေျခအေနေတြမွာဆိုရင္ေတာ့ False ျဖစ္ပါမယ္။ Activity.Finish method ဟာ လက္ရွိ

Activity ကုိ pause လုပ္တယ္။ ျပီးရင္ destroy လုပ္ပါတယ္။ Back button ကုိ ႏိွပ္တာနဲ႕ အတူတူဘဲ

ျဖစ္ပါတယ္။ ဒါေၾကာင့္ Activity.Finish ကုိ ေခၚတဲ့အခါမွာလည္း UserClosed parameter ဟာ True

ျဖစ္ပါတယ္။

UserClosed parameter ကုိ ၾကည့္ျပီး data ေတြကို save လုပ္မလား/မလုပ္ဘူးလား ဆံုး

ျဖတ္ႏုိင္တယ္။ UserClosed = True ျဖစ္ေနရင္ save လုပ္စရာမလိုဘူး (ျပန္ create လုပ္မွာ မဟုတ္

ေတာ့လို႕ပါ) UserClosed = False ျဖစ္ေနရင္ေတာ့ process variable ေတြကို သူ႕ရဲ႕ မူလ initial

state မွာ ျပန္ထားေပးရပါမယ္။

4.9. Activity Life Cycle

Application တစ္ခုကို စတင္သည္ကေန ပိတ္လိုက္သည္အထိ Activity တစ္ခု ျဖတ္သန္းရတဲ့

အဆင့္ေတြကို Activity life cycle လို႕ေခၚပါတယ္။ ပထမဆံုး Activity_Create subroutine ကို

ေခၚပါတယ္။ ဒုတိယ Activity_Resume subroutine ကို ေခၚပါတယ္။ Back key ကုိ ႏွိပ္လိုက္ရင္

Activity_Pause subroutine ကို ေခၚပါတယ္။

လက္ေတြ႕သိရေအာင္ code editor မွာ ေအာက္ပါအတုိင္း ရိုက္ထည့္ပါ။

164
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub Activity_Create(FirstTime As Boolean)

Log("Create")

End Sub

Sub Activity_Resume

Log("Resume")

End Sub

Sub Activity_Pause (UserClosed As Boolean)

Log("Pause")

End Sub

မ run ခင္ device ကို computer နဲ႕ ခ်ိတ္ပါ။ ျပီးရင္ Tool bar ေပၚက ကို click ျပီး run

ပါ။

ပံု ၂-ဿ

ပံု (၂-ဿ) မွာျပထားတဲ့အတုိင္း „Completed successfully.‟ ျဖစ္ရင္ Close button ကုိႏွိပ္ျပီး

dialog ကုိပိတ္လုိက္ပါ။ ျပီးရင္ ပံု (၂-၀) မွာျပထားတဲ့အတုိင္း Tabs area မွာ Logs tab ကုိေရြးပါ။

165
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၂-၀

Log area မွာ

Create

Resume

ဆိုျပီးေပၚေနပါလိမ့္မယ္။

ဒါကိုၾကည့္ျခင္းအားျဖင့္ application ကုိ စ run လုိက္တာနဲ႔ Activity_Create ျပီးရင္

Activity_Resume ကို ေခၚေၾကာင္း သိႏုိင္ပါတယ္။ ဘာေၾကာင့္လည္းဆိုေတာ့ Activity_Create sub

မွာ Log (“Create”) လို႕ ေရးခဲ့တယ္ေလ။ „Log‟ keyword ကေတာ့ (“”) ထဲမွာ ေရးထားတဲ့ string

ကုိ Logs area မွာ ေဖာ္ျပေပးတာပါ။ Activity_Create sub ကို ေခၚလုိက္လို႕သာ Logs area မွာ

Create ဆိုတာကိထ
ု ုတ္ျပေပးတာပါ။

Logs area မွာ „Resume‟ လုိ႕လည္းျမင္ရပါမယ္။ ဒါကိုၾကည့္ျခင္းျဖင့္ Activity_Create sub

ျပီးတာနဲ႕ Activity_Resumeကို ေခၚမွန္း သိႏုိင္ပါတယ္။ ဒါဟာ Activity တစ္ခု ျဖတ္သန္းရတဲ့ life

cycle ပါ။ ဒီ sub ေတြထဲမွာ လုိအပ္တဲ့ code ေတြ ေရးျခင္းျဖင့္ လိုအပ္တဲ့ application ကုိ develop

လုပ္ရမွာ ျဖစ္ပါတယ္။

ဘယ္အခ်ိန္မွာ Activity_Pause sub ကုိေခၚလည္းသိခ်င္ရင္ device မွာ back key ကုိ

ႏွိပ္လိုက္ပါ။ ျပီးရင္ Logs area မွာ ၾကည့္လုိက္ပါဦး။

Pause

ဆိုတာေလးျပပါလိမ့္မယ္။ ဒါကိၾု ကည့္ျပီးေတာ့ back key ကုိ ႏွိပ္လိုက္ရင္ Activity_Pause sub

ကုိေခၚတယ္ဆိုတာ သိႏိုင္ပါတယ္။

ေနာက္တစ္ခါ device ေပၚမွာ ခု install လုပ္ျပီးသြားတဲ့ application ကုိ ျပန္ run ပါ။ (Device

မွာ install လုပ္ျပီးသား application ကုိ ျပန္ဖြင့္ခုိင္းတာပါ။ PC ကေန run ခုိင္းတာ မဟုတ္ပါဘူး)

166
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Logs area မွာ…

Create

Resume

လို႕ျပမယ္။ Application ကုိ အစကေနျပန္ run တာမုိ႕ Activity_Create နဲ႕ Activity_Resume

ေတြကို call တာပါ။

ဒီတစ္ခါေတာ့ Back key မႏွိပ္ဘဲ Home key ကုိ ႏွိပ္လုိက္ပါ။ ျပီးရင္ Logs area မွာ ၾကည့္ပါ။

Pause

ဆိုတာေတြ႕မယ္။ ဒါကိုၾကည့္ျပီး Home key ႏွိပ္ရင္လည္း Activity_Pause ကို ေခၚတယ္ဆိုတာ

သိႏုိငပ
္ ါတယ္။ အခုတစ္ခါ application ကုိ ျပန္ run ပါဦး။ ျပီးရင္ Logs area မွာ ၾကည့္ပါ။

Resume

ဆိုတာဘဲေတြ႕မယ္။ Create မပါေတာ့ဘူး။ ဘာေၾကာင့္လည္းဆိုေတာ့ Home key ကုိ ႏွိပ္ရင္

Activity ဟာ background ကိုေရာက္သြားေပမယ့္ memory ေပၚမွာ ရွိေနေသးတဲ့အတြက္ ျပန္ run

တဲ့အခါ Activity_Resume ကေနဘဲ ျပန္စပါတယ္။

Device ကုိ ေထာင္ထားရာကေန လွဲလိုက္ပါ။ ျပီးရင္ Logs area ကုိ ၾကည့္ပါ။

Pause

Create

Resume

ဆိုျပီးေတြ႕ပါမယ္။ ျပီးရင္ device ကုိ လွဲထားရာကေန ျပန္ေထာင္လိုက္ပါဦး။

Pause

Create

Resume

ဆိုျပီးထပ္ေတြ႕ပါမယ္။ ဆိုလိုတာက device ကုိ ေထာင္/လွဲလိုက္တိုင္း device configuration ေျပာင္း

ပါတယ္။ ဒီလိုေျပာင္းတုိင္း Activity pause ျဖစ္ပါတယ္။ ျပီးရင္ configureation အသစ္နဲ႕ create,

resume ျပန္ျဖစ္ပါတယ္။ ခုတစ္ခါ ေအာက္ပါအတိုင္း လိုအပ္တဲ့ code ကို ထပ္ထည့္လက


ို ္ပါဦး။

167
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub Activity_Create(FirstTime As Boolean)

Log(FirstTime)

Log("Create")

End Sub

Sub Activity_Resume

Log("Resume")

End Sub

Sub Activity_Pause (UserClosed As Boolean)

Log(UserClosed)

Log("Pause")

End Sub

ဒီတစ္ခါေတာ့ FirstTime နဲ႕ UserClosed ဟာဘယ္အခ်ိန္မွာ True ျဖစ္ျပီး ဘယ္အခ်ိန္မွာ

False ျဖစ္မယ္ဆိုတာ လက္ေတြ႕သိရေအာင္ ေလ့လာပါမယ္။ Application ကို run လုိက္ပါ။ Logs

area မွာ

True

Create

Resume

ဆိုျပီး ေတြ႕ပါမယ္္။ Application ကုိ စ run ရင္ FirstTime ဟာ True ျဖစ္မယ္ဆိုတာ ေတြ႕ျပီေနာ္။

Back key ႏွိပ္လိုက္ပါ။ Logs area မွာ

„True

Pause

ဆိုျပီး ေတြ႕မယ္။ ဒါဆို Back key ႏွိပ္ရင္ UserClosed ဟာ True ျဖစ္တယ္ဆိုတာ ေတြ႕ျပီေနာ္။

Screen ေပၚက application ကို ျပန္ run ပါ (Device မွာ install လုပ္ျပီးသား application ကုိ

168
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ျပန္ဖြင့္ခုိင္း တာပါ။ PC ကေန run ခုိင္းတာ မဟုတ္ပါဘူး)

False

Create

Resume

ဆိုျပီး ေတြ႕မယ္ေနာ္။ ဒါကေတာ့ Activity ကုိ ပထမဆံုးအၾကိမ္ run တာမဟုတ္ဘဲ Back key ႏွိပ္ျပီး

ျပန္ run တာမုိ႕ FirstTime ဟာ False ျဖစ္ေနတာပါ။ ဒီတစ္ခါ Home key ကို ႏွိပ္လိုက္ပါဦး။

„False

Pause

ဆိုျပီး ေတြ႕မယ္ေနာ္။ ဒါကေတာ့ Home key ကုိ ႏွိပ္ရင္ UserClosed ဟာ False ျဖစ္လို႕ပါဘဲ။

အားလံုးျခံဳျပီး ျပန္ေျပာပါမယ္။

စ run ရင္

Create FirstTime = True

Resume

Back key ႏွိပ္ရင္

Pause UserClosed = True

ျပန္ run ရင္

Create FirstTime = False

Resume

Home key ႏွိပ္ရင္

Pause UserClosed = False

ျပန္ run ရင္

Resume

အထက္ပါ application ကို run ထားျပီး device ကုိ ေထာင္လိုက္၊ လဲွလိုက္ လုပ္ၾကည့္ပါဦး။

169
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ေထာင္လိုက္၊ လွဲလိုက္တိုင္းမွာ UserClosed က False ေတြခ်ည္း ျဖစ္ေနပါလိမ့္မယ္။ ဒါေၾကာင့္

Home key ႏိွပ္ျခင္း၊ device configuration ေျပာင္းျခင္းတုိ႕ေၾကာင့္ဆိုရင္ UserClosedဟာ False

ျဖစ္ျပီး Back key ႏွိပ္ရင္ေတာ့ UserClosed ဟာ True ျဖစ္ေၾကာင္း မွတ္သားထားပါ။

4.10. Activity.Finish/ExitApplication

Activity.Finish နဲ႕ ExitApplication ေတြဟာ Activity ကုိ ပိတ္တဲ့ method ေတြျဖစ္ပါတယ္။

Application အမ်ားစုမွာ ExitApplication ထက္ Activity.Finish ကုိ ပုိျပီးသံုးသင့္ပါတယ္။ Process

ကုိ memory ေပၚမွာ လံုးှမထားေတာ့ဘဲ kill လုပခ


္ ်င္မွသာ ExitApplication ကုိ သံုးသင့္ပါတယ္။

ဘယ္အခ်ိန္မွာ Activity.Finish ကုိ သံုးျပီး ဘယ္အခ်ိန္မွာ မသံုးသင့္ဘူးဆိုတာကို ဥပမာနဲ႕ ေလ့လာပါ

မယ္။ Activity.Finish မသံုးတဲ့ example ကို အရင္ေလ့လာပါမယ္။

Activity ၁ခုလုပ္ပါ။

 Main activity

 SecondActivity

 ThirdActivity

Main Activity ထဲမွာ SecondActivity ကုိေခၚပါ။

StartAcivity(SecondActivity)

SecondActivity Activity ထဲမွာ ThirdActivity ကိုေခၚပါ။

StartAcivity(ThirdActivity)

ThirdActivity ျမင္ေနရခ်ိန္မွာ Back button ကုိႏွိပ္ရင္ SecondActivity ကို

ျပန္ေရာက္သြားပါလိမ့္မယ္။

SecondActivity ကို ျမင္ေနရခ်ိန္မွာ Back button ကုိႏွိပ္ရင္ Main Activity ဆီ

ျပန္ေရာက္သြားပါမယ္။

Main Activity ကို ျမင္ေနရခ်ိန္မွာ Back button ႏွိပ္ရင္ ျပန္သြားစရာမရွိေတာ့လို႕ program

170
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ထဲကထြက္သြားပါလိမ့္မယ္။

ဒီတစ္ခါ Activity.Finish အသံုးျပဳပံုကုိ ေလ့လာမယ္။ Main Activity ထဲမွာ Activity.Finish

ကိုသံုးျပီး Main Activity ကုိ အဆံုးသတ္လုိက္ပါ။ ျပီးမွ SecondActivity ကုိ ေခၚလိုက္ပါ။

Activity.Finish

StartAcivity(SecondActivity)

SecondActivity မွာ Activity.Finish ကို သံုးျပီး SecondActivity ကိအ


ု ဆံုး သတ္လုိက္ပါ။

ျပီးေတာ့မွ ThirdActivity ကုိ ေခၚလိုက္ပါ။

Activity.Finish

StartAcivity(ThirdActivity)

ThirdActivity မွာ Back button ကုိ ႏွိပ္လိုက္ရင္ ျပန္သြားစရာ Activity မရွိေတာ့တဲ့အတြက္

program ထဲကေန ထြက္သြားပါလိမ့္မယ္။

ဒါေၾကာင့္ Back button သံုးျပီး Activity တစ္ခုဆီ ျပန္သြားစရာ မလိုအပ္တဲ့အခါမ်ိဳးမွသာ

Activity.Finish ကို အသံုးျပဳသင့္ပါတယ္။ (Example 4.1 ကုိၾကည့္ပါ)

Example 4.1

Project တစ္ခု create လုပ္ပါ။ Activity module ၀ခုထပ္ထည့္ပါ။ SecondActivity နဲ႕

ThridActivity လို႕အသီးသီးအမည္ေပးပါ။ Layout file ၁ခုလုပ္ပါ။ Layout1, Layout2, Layout3

လုိ႕အသီးသီးအမည္ေပးပါ။ Layout1 နဲ႕ Layout2 မွာ button တစ္ခစ


ု ထ
ီ ည့္ပါ။ Property ေတြကို

ပံုမွာျပထားတဲ့အတုိင္းသတ္မွတ္ေပးပါ။

171
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Layout Name: Layout1

ပံု ၂-၁

Layout Name: Layout2

ပံု ၂-၂

172
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Layout: Layout3

ပံု ၂-၃

Main Activity မွာေအာက္ပါအတုိ္င္းေရးပါ။

#Region Project Attributes

#ApplicationLabel: B4A Example

#VersionCode: 1

#VersionName:

#SupportedOrientations: unspecified

#CanInstallToExternalStorage: False

#End Region

#Region Activity Attributes

#FullScreen: False

#IncludeTitle: True

#End Region

Sub Process_Globals

End Sub

Sub Globals

End Sub

173
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub Activity_Create(FirstTime As Boolean)

Activity.LoadLayout("Layout1")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click

StartActivity(SecondActivity)

Activity.Finish

End Sub

SecondActivity မွာေအာက္ပါအတုိ္င္းေရးပါ။

#Region Activity Attributes

#FullScreen: False

#IncludeTitle: True

#End Region

Sub Process_Globals

End Sub

Sub Globals

End Sub

174
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub Activity_Create(FirstTime As Boolean)

Activity.LoadLayout("Layout2")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click

StartActivity(ThirdActivity)

Activity.Finish

End Sub

ThirdActivity မွာေအာက္ပါအတုိ္င္းေရးပါ။

#Region Activity Attributes

#FullScreen: False

#IncludeTitle: True

#End Region

Sub Process_Globals

End Sub

Sub Globals

End Sub

175
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub Activity_Create(FirstTime As Boolean)

Activity.LoadLayout("Layout3")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Program ကုိ run ျပီးအမ်ိဳးမ်ိဳးစမ္းၾကည့္ပါ။

176
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

Chapter 5

Module

Project တစ္ခုမွာ အနည္းဆံုး module တစ္ခုေတာ့ရွိတယ္။ အဲဒါကေတာ့ main activity

module ပါ။ အမည္ကေတာ့ Main ျဖစ္ပါတယ္။ ဒီအမည္ကို ေျပာင္းပစ္လို႕မရဘူး။ ဒီ module ကို

ဖ်က္လုိ႔လည္း မရပါဘူး။ ေအာက္ပါအတုိင္း module အမ်ိဳးအစား (၂)မ်ိဳးရွိပါတယ္။

 Activity module

 Class module

 Code module

 Service module

Project ထဲကို module အသစ္တစ္ခု ထည့္ခ်င္ရင္ IDE menu မွာ [Project > Add New

Module] ကိုေရြးပါ။ (ပံု ၃-ဿ)

ပံု ၃-ဿ

ရွိျပီးသား module ကို project ထဲ ထည့္ခ်င္တာဆိုရင္ေတာ့ IDE menu မွာ [Project >

Add Existing Module] ကိုေရြးပါ။ (ပံု ၃-၀)

ပံု ၃-၀

177
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
5.1. Activity Module

Activity တစ္ခုမွာ Activity module တစ္ခုရွိပါမယ္။ Module မွာ declare လုပ္ထားတဲ့

object/variable ေတြကို အျခား module ကေန access လုပ္ခ်င္ရင္ object/variable name ေရွ႕မွာ

module name ပါ ထည့္ေပးရမယ္။ ေအာက္ပါ example မွာ ၾကည့္ပါ။

Main module

Sub Process_Globals

Dim Value1, Value2, Value3 As String

End Sub

Main module ထဲမွာ declare လုပ္ထားတဲ့ Value1, Value2, Value3 တို႕ကို အျခား

module မွာ သံုးခ်င္ရင္ Main.Value1, Main.Value2, Main.Value3 လို႕ ေရးရမယ္။

Other module

Sub Activity_Pause (UserClosed As Boolean)

Log(Main.Value2)

End Sub

Activity module တစ္ခုအတြင္းက view ေတြကို အျခား Activity ကေန access မလုပ္ႏုိင္

ပါဘူး။ ဘာလို႕လည္းဆိုေတာ့ Activity အသစ္တစ္ခု စတင္လုိက္တာနဲ႕ လက္ရွိ Activity ကို pause

လုပ္လက
ို ္ပါတယ္။ Pause လုပ္ထားတဲ့ Activity ထဲက view ေတြကို အျခား Activity ကေန access

လုပ္လို႕ မရႏိုင္ေတာ့ပါဘူး။ ဒါေၾကာင့္ပါ။

5.2. Class Module

Version 2.0 ကစျပီး Class module ေတြကို အသံုးျပဳႏုိင္ခဲ့တာပါ။

178
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
5.3. Code Module

Basic4android v1.2 ကစျပီး static code module ဆိုတဲ့ module type အသစ္တစ္ခု

ပါှင္လာပါတယ္။ Static code module ကုိ ထည့္ဘုိ႕ကေတာ့ [Project > Add New Module

>CodeModule] ကို ေရြးပါ။ ပံု (၃-၁) မွာ ျပထားတဲ့အတုိင္း dialog box ေပၚလာရင္ module name

ကုိ ရိုက္ထည့္ပါ။

ပံု ၃-၁

Module name ထည့္ျပီး OK လုပ္ရင္ ေအာက္ပါအတုိင္း Code module တစ္ခုေရးဘုိ႕

အသင့္ျဖစ္လာပါမယ္။

'Code module

'Subs in this code module will be accessible from all modules.

Sub Process_Globals

'These global variables will be declared once when the application starts.

'These variables can be accessed from all modules.

End Sub

Activitie နဲ႕ Service module ေတြနဲ႕မတူတဲ့အခ်က္က Code module ဟာ Android

process life cycle နဲ႕ ဆက္စပ္မႈ လံုးှမရွိပါဘူး။ Code module ဟာ code ေတြေရးထားတဲ့

container တစ္ခုသာျဖစ္ပါတယ္။ Code module အတြင္းမွာရွိတဲ့ sub အားလံုးဟာ public

179
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ျဖစ္ပါတယ္။ ဒါေၾကာင့္ အျခား module ေတြကပါ access လုပ္ႏုိင္ပါတယ္။ Code module ေတြ

သံုးျခင္းအားျဖင့္ ေအာက္ပါ အက်ိဳးေက်းဇူးေတြရွိပါတယ္။

 Code module မွာေရးျပီးသား code ေတြကို အျခား module ေတြမွာ ျပန္ေရးစရာ မလုိေတာ့

ဘဲ အသင့္ယူသံုးႏုိင္ပါတယ္။

 အျခား project ေတြဟာ code ေတြကို share လုပ္ျပီး သံုးႏုိင္ပါတယ္။ (ဥပမာ XML file ကို

parse လုပ္တဲ့ Code module တစ္ခု ေရးထားရင္ အဲဒီ Code module ကုိ အျခား

application ေတြမွာ အလြယ္တကူယူျပီးသံုးႏုိင္ပါတယ္)

 Application logic ကုိ သီးျခားခြဲထုတ္ႏုိင္ပါတယ္။ သက္ဆိုင္ရာလုပ္ငန္းတစ္ခုအတြက္ Code

module တစ္ခစ
ု သ
ီ ံုးႏုိင္ပါတယ္။ ဒါေၾကာင့္ program ကုိ ပိုမိုရွင္းလင္းေစျပီး maintain လုပ္ရ

တာလည္း လြယ္ကူေစပါတယ္။

Code module တစ္ခုကို ဘယ္ Activity အတြက္ ဘယ္ Service အတြက္ရယ္လို႕ သတ္မွတ္

ထားတာမဟုတ္တဲ့အတြက္ Code module တစ္ခုကို လုိအပ္တဲ့အခ်ိန္မွာ component context

ကုိသံုးျပီးေခၚရပါတယ္။

Activity တစ္ခုကေန message box dialog ကုိျပဘုိ႕အတြက္ Code module sub တစ္ခုကို

ေခၚလို႕ရပါတယ္။ ဒါေပမယ့္ အဲဒီ Code module sub ကုိ Service ကေန ေခၚလို႕ေတာ့ မရပါဘူး။

ဘာေၾကာင့္လဲဆိုေတာ့ Service ေတြဟာ dialog ကုိ ျပခြင့္မရွိလို႕ပါ။

Code module ေတြဟာ event ေတြကို catch မလုပ္ႏုိင္ပါဘူး။ ဆိုလိုတာက Button1 ကုိ

click လုိက္ရင္ message box ေပၚခ်င္တယ္ဆိုပါေတာ့။ ဒီအတြက္ Button1_Click event sub မွာ

code ေရးရမယ္။ ဒါမ်ိဳးကို Code module မွာေရးလို႕မရပါဘူး။

Button တစ္ခုကို initialize လုပ္ဘုိ႕ Code module အသံုးျပဳပံုကို ေအာက္မွာေလ့လာ

ၾကည့္ပါ။

Code Module

Sub ButtonsCreator(Text As String) As Button

Dim b As Button

180
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
b.Initialize("But")

b.Text = Text

Return b

End Sub

Activity module ကေနအထက္ပါ Code module ကုိ ေခၚဘု႕ုိ ဆိုရင္ Activity_Create sub

မွာေအာက္ပါအတုိင္းေရးပါ။

Activity.AddView(CodeModule.ButtonsCreator("press here"), 10dip, 10dip,

200dip, 200dip)

Click event ကို catch လုပ္ဘို႕အတြက္ကေတာ့ But_Click လို႕အမည္ရတဲ့ sub တစ္ခုကို

create လုပ္ရပါမယ္။ ဒီ sub ကိုေတာ့ Activity module အတြင္းမွာ ေရးရမယ္ေနာ္။ Code module

ထဲမွာ သြားေရးလို႕မရဘူး။ ဘာေၾကာင့္လည္းဆိုေတာ့ Code module ဟာ event ကို catch မလုပ္ႏုိင္

လို႕ပါ (အထက္မွာလည္း ေျပာခဲ့ပါတယ္ေနာ္)

Sub But_Click

Msgbox(“Hello World!”,”Greeting”)

End Sub

ျပီးရင္ program ကုိ run ၾကည့္ပါ။ press me ဆိုတဲ့ button တစ္ခုေတြ႕မယ္။ Button ကုိ

click လုိက္ရင္ Hello World! ဆိုတဲ့ message box ေပၚလာပါလိမ့္မယ္္။

5.4. Service Module

Basic4android v1.2 ကစျပီး Service module ကိအ


ု သံုးျပဳႏုိင္ခဲ့ပါတယ္။ Service module

ေတြဟာ application နဲ႕ process life cycle ေတြမွာ အေရးပါတဲ့က႑က ပါှင္ပါတယ္။ Activity

181
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
တစ္ခု invisible ျဖစ္သြားတာနဲ႕ Activity module ထဲမွာေရးထားတဲ့ code ေတြကုိ pause

လုပ္ပါတယ္။ ဒါေၾကာင့္ application invisible ျဖစ္ေနစဥ္မွာ Activity ထဲက ဘယ္ code ကုိမွ run

လုိ႕ရမွာမဟုတ္ပါဘူး။

ေလာေလာဆယ္ screen မွာျမင္ေနရတဲ့ Activity ဟာ Service life cycle အေပၚ

ဘယ္လုိသက္ေရာက္မႈမွ မရွိႏုိင္ပါဘူး။ ဆိုလိုတာက Activity invisible ျဖစ္သြားသည့္တုိင္ Service

module အတြင္းမွာေရးထားတဲ့ code ေတြကို pause မလုပ္ပါဘူး။ ဆက္ျပီး run ေနႏုိင္ပါတယ္။

Activity ကို screen ေပၚမွာ မျမင္ရေတာ့ေပမယ့္ task ေတြကို background မွာ ဆက္ျပီး run

ေနႏိုင္ပါတယ္။

ဥပမာ music player လိုမ်ိဳးေပါ့။ music file တစ္ခုကို play ျပီးေနာက္ အျခား application

တစ္ခုကို ထပ္ဖြင့္လိုက္မယ္ဆိုပါေတာ့။ အဲဒီအခ်ိန္မွာ music player ရဲ႕ Activity က invisible

ျဖစ္သြားျပီး ေနာက္ဖြင့္တဲ့ application ရဲ႕ Activity က visible ျဖစ္သြားမယ္။ ဒါေပမယ့္ သီခ်င္းသံ

ကိုေတာ့ ဆက္ၾကားေနဦးမယ္။ ဘာေၾကာင့္လည္းဆိုေတာ့ music player ရဲ႕ Service module ထဲက

code ေတြကို background မွာဆက္ run ေပးေနလုိ႕ပါ။ Background မွာ run မယ့္ code ေတြကုိ

service module မွာ ေရးရပါမယ္။

Services ေတြမွာ user interface မရွိဘူးဆိုေတာ့ user နဲ႕ဘယ္လို interact လုပ္မလဲ။

Status bar notification ကိုသံုးျပီး interact လုပ္ႏိုင္ပါတယ္။ Service ေတြမွာ visible element ေတြ

မရွိပါဘူး။ Service ေတြဟာ dialog ေတြကိုလည္း မျပႏုိင္ပါဘူး။ Message box dialog ကုိ

မျပႏုိင္ဘူး။ ဒါေပမယ့္ Toast message ေတာ့ျပႏုိင္တယ္။ ဒါေၾကာင့္ Service code ထဲမွာ error

တစ္ခုခုျဖစ္ခဲ့ရင္ "Do you want to continue?" dialog မ်ိဳးကို ျမင္ရမွာမဟုတ္ဘူး။ Dialog အစား

"Process has crashed" message ပဲေပၚလာပါလိမ့္မယ္။

Service ေတြကုိ သံုးရတာ လြယ္ကူပါတယ္။ အလုပ္ေတာ္ေတာ္မ်ားမ်ားကို Activity နဲ႕လုပ္

တာထက္ Service နဲ႕လုပ္တာက ပိုလြယ္ပါတယ္။ ဘာေၾကာင့္လဲဆိုေတာ့ Service ေတြကို ဘယ္အ

ခ်ိန္မွာမွ pause လုပ္တာ resume လုပ္တာ မရွိလို႕ပါ။

ျပီးေတာ့ user က screen ကုိ rotate လုပ္လိုက္တာေၾကာင့္ Service ကို destroy လုပ္တာ

မ်ိဳးလည္း မရွိပါဘူး။ Activity ကိုေတာ့ screen rotate လုပ္တုိင္း destroy လုပ္ပါတယ္။ Service

182
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
module ထဲက code ေတြဟာ အျခား module ထဲက code ေတြလိုဘဲ same process, same

thread မွာ run ပါတယ္။

Pause လုပ္ထားတဲ့ process ေတြဟာ memory ေပၚမွာ ဆက္ရွိေနႏုိငပ


္ ါေသးတယ္။

Memory ေနရာနည္းလာျပီဆိုရင္ေတာ့ pause လုပ္ထားတဲ့ process ေတြထဲက အခ်ိဳ႕ကို kill

လုပ္ပစ္ပါတယ္။

Android ဟာ kill လုပ္ရမယ့္ process ကုိ ဘယ္လိုေရြးခ်ယ္တယ္ဆိုတာလည္း သိထားဘို႕

လိုမယ္ေနာ္။ Process တစ္ခုဟာ ေအာက္ပါအေျခအေန ၁ခုအနက္က တစ္ခုခမ


ု ွာ ရွိေနႏုိင္ပါတယ္။

 Foreground

o Process Activity ေတြထဲက တစ္ခုခုဟာ screen ေပၚမွာ ျမင္ေနတဲ့အေျခအေန

 Background

o Process ထဲက Activity တစ္ခုမွ visible ျဖစ္မေနဘူး။ ဒါေပမယ့္ Service ေတာ့ run

ေနတဲ့အေျခအေန

 Paused

o Visible Activity လည္းမရွိ Service လည္း run မေနတဲ့အေျခအေန

အထက္ပါအေျခအေနေတြထဲကမွ low memory ျဖစ္သြားလို႕ process ေတြကို kill လုပ္ဘို႕

လိုတဲ့အခါ paused process ေတြကို အရင္ဆံုး kill လုပ္ပါတယ္။ ဒါနဲ႕မွ low memory ျဖစ္ေနတံုး

ဘဲဆိုရင္ background process ေတြကုိ kill လုပ္ပါတယ္။ Foreground processes ေတြကိုေတာ့

သာမန္အားျဖင့္ kill လုပ္ေလ့မရွိပါဘူး။

Service ဟာ process တစ္ခုကို foreground ကုိ ျပန္ပို႕ေပးႏုိင္တယ္ဆိုတာလည္း သိထား

ဘုိ႕လိုပါတယ္။

Service module ထည့္ဘုိ႕ကေတာ့ [Project ->Add New Module -> Service] ကို

ေရြးပါ။ Service module တစ္ခု ထည့္လုိက္ရင္ ေအာက္ပါအတိုင္း code မ်ားေရးရန္ template တစ္ခု

အသင့္ ျဖစ္ေနပါမယ္။

183
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
#Region Service Attributes

#StartAtBoot: False

#End Region

Sub Process_Globals

End Sub

Sub Service_Create

End Sub

Sub Service_Start (StartingIntentAsIntent)

End Sub

Sub Service_Destroy

End Sub

Sub Process_Globals ထဲမွာ Service global variable ေတြကို declare လုပ္ရပါမယ္။

Service ဟာ Activity object ေတြကို support မလုပ္တဲ့အတြက္ Globals sub ေတာ့ မပါပါဘူး။ Sub

Process_Globals ကို variable ေတြ declare လုပ္ဘုိ႕သာ သံုးသင့္ပါတယ္။ အျခား code ေတြကို

မေရးသင့္ပါဘူး။ Process global variable ေတြဟာ process run ေနသမွ် တည္ရွိေနျပီး အျခား

module ေတြကလည္း အသံုးျပဳႏုိင္ပါတယ္။

Service ကို ပထမဆံုး start လုပ္တဲ့အခါမွာ Sub Service_Create ကို ေခၚပါတယ္။ ဒီ sub

ထဲမွာ process global variable ေတြကို initialize လုပ္ရပါတယ္။ Service တစ္ခု စတင္ျပီးတဲ့ေနာက္

StopService ကုိ မေခၚခင္ထိ သို႕မဟုတ္ process တစ္ခုလံုးကို destroy မလုပ္ခင္ထိ process

global variable ေတြ တည္ရွိေနႏုိင္ပါတယ္။

184
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
StartService (or StartServiceAt) ကို ေခၚတုိင္းမွာ Sub Service_Start ကို ေခၚပါတယ္။

Sub Service_Start ကို ေခၚတဲ့အခ်ိန္မွာ process ကုိ foreground state ကုိ ေရႊ႕လိုက္ပါတယ္။

ဆုိလိုတာက ဒီ sub ကုိ run လို႕မျပီးမခ်င္း Android operating system ဟာ process ကုိ kill မလုပ္

ေတာ့ဘူးဆိုတဲ့သေဘာပါ။ Foreground state မွာရွိေနတဲ့ process ေတြကို kill လုပ္ေလ့မရွိဘူးေလ။

သတ္မွတ္ထားတဲ့အခ်ိန္ေရာက္တုိင္း run ေစခ်င္တဲ့ code ေတြကို ဒီ sub ထဲမွာ ေရးရမွာပါ။ Alarm

service ေတြကို ဒီ sub ထဲမွာ ေရးရမွာေပါ့။

StopService ကုိ ေခၚလုိက္တဲ့အခါမွာ Sub Service_Destroy ကို ေခၚပါတယ္။ ဒီ sub ကို

ေခၚျပီးေနာက္မွာ service ကုိ run မွာ မဟုတ္ေတာ့ပါဘူး။ Service ကုိ destroy လုပ္လိုက္ျပီျဖစ္ပါ

တယ္။

Service Use Cases

Service အသံုးျပဳပံုေတြကို ေလ့လာၾကည့္ရေအာင္။

o UI code နဲ႕ logic code ကုိ သီးျခားစီခြဲထားပါတယ္။ Service ထဲမွာ non-UI code ကို

ေရးသားျခင္းဟာ Activity module ထဲမွာ ေရးရတာထက္ ပိုလြယ္ပါတယ္။ ဘာေၾကာင့္လဲဆို

ေတာ့ Service ကို pause လုပ္ျခင္း၊ resume လုပ္ျခင္း၊ recreate လုပ္ရျခင္းေတြ မရွိလို႕ပါ။

Activity_Create sub အတြင္းမွာ StartService ကို ေခၚႏုိင္ပါတယ္။ Activity_Resume sub

မွာ Service ထဲက လိုအပ္တဲ့ data ေတြကို ရယူႏုိငပ


္ ါတယ္။ Activity ဟာ Service ရဲ႕

process global variable ေတြမွာ store လုပ္ထားတဲ့ data ေတြကို ရယူႏင


ုိ ္ပါတယ္။

CallSub method ကုိသံုးျပီး Service ရဲ႕ sub ေတြကလ


ုိ ည္းေခၚႏုိင္ပါတယ္။

o အခ်ိန္ၾကာတဲ့ လုပ္ငန္းေတြကို လုပ္တဲ့အခါ Service ကို သံုးသင့္တယ္။ ဥပမာ အင္တာနတ္

ကေန ဖုိင္ဆုိက္အၾကီးၾကီးေတြကို download ဆြဲတာမ်ိဳးေပါ့။ အခ်ိန္ၾကာမယ္ေလ။ ဒီလို

operation အတြက္ဆိုရင္ Service.StartForeground ကို ေခၚထားသင့္တယ္။ ဒီလို

ေခၚထားရင္ Activity ကို foreground state ကိုပို႕ထားတဲ့အတြက္ OS က kill

မလုပ္ႏုိင္ေတာ့ဘူးေပါ့။ Operation ျပီးသြားရင္ေတာ့ Service.StopForeground ကို ေခၚျပီး

foreground ကေနျပန္ခ်ေပးရမယ္ေနာ္။

185
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
o ထပ္တစ္လဲလဲလုပ္ရမယ့္ task ေတြအတြက္ Service ကုိ သံုးႏိင
ု ္တယ္။ StartServiceAt

method ကုိ ေခၚျပီး Service ကုိ သတ္မွတ္ထားတဲ့ အခ်ိန္တုိင္းမွာ run ေအာင္လုပ္ႏုိင္တယ္။

(Alarm clock လိုဟာမ်ိဳးေပါ့)

Notification

Status bar notification ေတြကို Activitie ေတြကေသာ္လည္းေကာင္း၊ Service ေတြက

ေသာ္လည္းေကာင္း display လုပ္ျပႏုိင္ပါတယ္။ Service ေတြဟာ user နဲ႕ interact လုပ္ဘို႕

notification ကိုသံုးေလ့ရွိပါတယ္။ Notification ကို status bar ေပၚမွာ icon တစ္ခုနဲ႕အတူ

ျပပါႏုိင္ပါတယ္။ User က status bar ကုိ ဆြဲခ်လိုက္ရင္ notification message ကို ျမင္ရပါလိမ့္မယ္။

ပံု (၃-၂) မွာ notification နမူနာကို ျပထားပါတယ္။

ပံု ၃-၂

User က notification message ကုိ click လုပ္လုိက္ရင္ notification object မွာ configure

လုပ္ထားတဲ့ Activity ဟာ foreground ကုိေရာက္လာပါလိမ့္မယ္။ Notification icon ဟာ image

file တစ္ခု ျဖစ္ျပီး <project folder>\Object\res\drawable မွာ ထည့္ထားရပါမယ္။

186
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Accessing other Modules

Process global object ေတြဟာ public ျဖစ္တာေၾကာင့္ အျခား module ေတြကေန access

လုပ္ႏုိင္ပါတယ္။ အျခား module ထဲက sub ေတြကို ေခၚခ်င္ရင္ CallSub method ကုိ သံုးႏုိင္ပါတယ္။

ဒါေပမယ့္ non-paused module ေတြကိုသာေခၚႏုိင္ပါတယ္။ ဆိုလိုတာက Activity တစ္ခု

ဟာ အျခား Activity တစ္ခုရဲ႕ sub ကုိ access မလုပ္ႏုိင္ပါဘူး။ ဘာေၾကာင့္လည္းဆိုေတာ့ device

တစ္ခုဟာ တစ္ခ်ိန္မွာ Activity တစ္ခုကိုသာ run ေနႏုိင္တာမုိ႕ပါ။

ဒါေပမယ့္ Activity တစ္ခုဟာ running service ကို access လုပ္ႏုိင္ျပီး service ကလည္း

running Activity တစ္ခုကို access လုပ္ႏုိင္ပါတယ္။ အဲဒီလို access လုပ္လိုက္တဲ့အခ်ိန္မွာ target

component က pause ျဖစ္ေနရင္ေတာ့ empty (ဘာမွမပါတဲ)့ string တစ္ခုကိုသာ return

ျပန္လာမွာ ျဖစ္ပါတယ္။

Target module ဟာ pause ျဖစ္ေနလားဆိတ


ု ာကို စစ္ေဆးဘို႕ „IsPause‟ ကို

သံုးႏုိင္ပါတယ္။ ဥပမာ Service တစ္ခုဟာ new data အခ်ိဳ႕ကုိ download ဆြဲထားတယ္ ဆုိပါစို႕။

ေအာက္ပါအတုိင္း CallSub ကုိ သံုးျပီး Main Activity ကုိ ေခၚႏုိ္င္ပါတယ္။

CallSub(Main, "RefreshData")

ေခၚလိုက္တဲ့အခ်ိန္မွာ Main Activity ဟာ foreground မွာရွိေနတယ္ဆိုရင္ process global

variables ထဲက data ေတြကို ရယူျပီး display လုပ္ျပႏိုင္တာေပါ့။

New data ေတြကို Activity sub ထံ ပို႕ေပးလို႕လည္းရပါတယ္။ ဒါေပမယ့္ data ေတြကုိ

process global variable ထဲမွာ ထားတာပိုေကာင္းပါတယ္။ ဘာေၾကာင့္လဲဆိုေတာ့ new data

ေရာက္လာတဲ့အခ်ိန္မွာ Activity ဟာ pause ျဖစ္ေနတာမ်ိဳးလည္း ျဖစ္ႏိုင္လို႕ပါ။ Pause ျဖစ္ေနခ်ိန္မွာ

new data ေတြကို Activity sub ထံလွမ္းပို႕မိရင္ အခ်ည္းႏွီးသာျဖစ္ပါမယ္။ Process global

variables ထဲ မွာထားရင္ေတာ့ Activity ဟာ လိုအပ္တဲ့အခါတုိင္း RefreshData method ကုိ ေခၚျပီး

new data ေတြကိုရယူႏုိင္ပါတယ္။ တစ္ခုမွတ္ထားရမွာက Code module ရဲ႕ sub ေတြကို access

လုပ္ဘုိ႕ CallSub ကို အသံုးျပဳလုိ႕မရပါဘူး။

187
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (၁)

Chapter 6

Screen Size and Resolution

Device ေတြဟာ screen size, resolution, pixel density အမ်ိဳးမ်ိဳးနဲ႕ ရွိေနၾကပါတယ္။

 Physical screen size: Screen ရဲ႕ ေထာင့္ျဖတ္အက်ယ္ပါ။ (ဥပမာ 3.5 '')

 Resolution: Screen ရဲ႕ width နဲ႕ height ကုိ pixel အေရအတြက္နဲ႕ ေဖာ္ျပျခင္း

ျဖစ္ပါတယ္။ (ဥပမာ 320/480 pixels)

 Pixel density: Screen တစ္လက္မမွာရွိတဲ့ pixel အေရအတြက္ျဖစ္ပါတယ္။ (ဥပမာ 160 dpi

ဆိုရင္ တစ္လက္မမွာ 160 pixels ရွိပါတယ္။ dpi က dots per inch ပါ။ pixels per inch

လို႕လည္း သံုးပါတယ္။ တူတူပါဘဲ)

Standard screen resolution ဟာ 320/480 pixels ျဖစ္ျပီး density ကေတာ့ 160

pixels/inch ျဖစ္ပါတယ္။ Scale = Density/160 ျဖစ္ပါတယ္။

ေအာက္ပါအတုိင္း screen အမ်ိဳးမ်ိဳးရွိပါတယ္။

Diagonal Resolution Density Scale W / H Ratio

3.5 320 / 480 160 1 3/2

3.5 480 / 720 240 1.5 3/2

3.9 480 / 800 240 1.5 5/3

3.5 240 / 320 120 0.75 4/3

5 1080 / 1920 480 3 4/3

7 640 / 960 160 1 3/2

7 800 / 1280 160 1 16 / 9

10 768 / 1024 160 1 4/3

10 800 / 1280 160 1 16 / 10

10 1200 / 1920 240 1.5 16 / 10

188
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (၁)
6.1. Working with different Screen Sizes

Application တစ္ခုကို screen အမ်ိဳးမ်ိဳးမွာ အဆင္ေျပေအာင္ run ႏုိင္ဘုိ႕အတြက္ နည္းလမ္း

အမ်ိဳးမ်ိဳး သံုးႏုိင္ပါတယ္။

6.1.1. Layout Variant

Layout variant ေတြ create လုပ္ရတာ လြယ္ေပမယ့္ မ်ားရင္ ထိန္းသိမ္းရတာ ခက္ခဲပါ

တယ္။ Layout variant ျပဳလုပ္နည္းကို Chapter 7 Designer မွာၾကည့္ပါ။

6.1.2. %x and %y

View တစ္ခုရဲ႕ width ကုိ %x နဲ႕ သတ္မွတ္ျပီး height ကုိ %y နဲ႕ သတ္မွတ္ႏုိင္ပါတယ္။

 Activity ေပၚက view အတြက္ဆိုရင္

Button1.Width = 50%x

ဆိုတာဟာ Button1 ရဲ႕ width ကို Activity width ရဲ႕ 50% လို႕သတ္မွတ္တာပါ။ Activity

width 320 ရွိရင္ Button1 width က 160 ျဖစ္ပါမယ္။

 Panel ေပၚက view အတြက္ဆိုရင္

Button1.Height = 20%y

ဆိုတာဟာ Button1 ရဲ႕ height ကို Panel height ရဲ႕ 20% လို႕သတ္မွတ္တာပါ။ Panel

height 480 ရွိရင္ Button1 height က 96 ျဖစ္ပါမယ္ (480 * 0.2 = 96)

6.1.3. dip (Density Independent Pixel)

dip unit ကို သံုးျခင္းျဖင့္ device ရဲ႕ scale ေပၚမူတည္ျပီး dimension ကုိ တြက္ေပးပါတယ္။

dip unit ကုိ သံုးရင္ view ရဲ႕ (physical position နဲ႕ size) ဟာ ဘယ္ device မွာမဆို အတူတူဘဲ

ျဖစ္ေနမွာပါ။ ဥပမာ Button1 ရဲ႕ width ကို 160dip လို႕သတ္မွတ္လိုက္ရင္ (resolution = 320

/480, scale = 1 device မွာ) 1” က်ယ္မွာျဖစ္ျပီး၊ (resolution = 480 / 720, scale = 1.5 device

မွာလည္း) 1” ဘဲ ျဖစ္မွာပါ။ Device အမ်ိဳးအစား ေျပာင္းေပမယ့္ width က မေျပာင္းသြားပါဘူး။

189
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (၁)
တြက္နည္းကေတာ့…

320 / 480, scale = 1 Device (160 pixels = 1”)

Width * Device Scale = 160 * 1 = 160 pixels = 1”

480 / 720, scale 1.5 Device (240 pixels = 1”)

Width * Device Scale = 160 * 1.5 = 240 pixels = 1”

Example 6.1

Project တစ္ခု create လုပ္ျပီး Globals နဲ႕ Activity_Create sub တို႕မွာ ေအာက္ပါအတုိင္း

ေရးျပီး run ပါ။ (ပံု ၆-၁) မွာ ျပထားတဲ့အတုိင္း ျမင္ရမယ္။

Sub Globals

Dim Button1 As Button

End Sub

Sub Activity_Create(FirstTime As Boolean)

Button1.Initialize("Button1")

Activity.AddView(Button1, 20%x, 30%y, 200dip, 100dip)

End Sub

190
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (၁)

Activity

30 %y

Button1 100dip

200dip

20 %x

ပံု ၆-၁

Text size ကုိေတာ့ physical unit နဲ႕ တုိင္းတာျပီးသားျဖစ္တာမုိ႕ dip unit ကုိ မသံုးရပါဘူး။

6.2. Screen Orientation

Screen orientation value ၃မ်ိဳး သတ္မွတ္ႏုိင္ပါတယ္။

 Portrait only

 Landscape only

 Both

191
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (၁)
Orientation သတ္မွတ္ႏိုင္တဲ့ေနရာေတြကေတာ့…

Project Attributes region မွာသတ္မွတ္ျခင္း

#Region Project Attributes

#ApplicationLabel: MyFirstProgram

#VersionCode: 1

#VersionName:

#SupportedOrientations: unspecified ဒီေနရာမွာသတ္မွတ္ရမယ္။

#CanInstallToExternalStorage: False

#End Region

Orientation ၂မ်ိဳးလံုးနဲ႕ run ရန္ > unspecified

Portrait mode အတြက္သာ > portrait

Landscape mode အတြက္သာ > landscape

Phone object ရဲ႕ SetScreenOrientation method ကိုသံုးျပီးသတ္မွတ္ျခင္း

Landscape အတြက္

SetScreenOrientation(0)

Portrait အတြက္

SetScreenOrientation(1)

၂မ်ိဳးလံုးအတြက္

SetScreenOrientation(-1)

192
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (၁)
Example 6.2

Project တစ္ခု create လုပ္ပါ။ Libs tab ကေန Phone library ကုိ check လုပ္ပါ။ Globals

နဲ႕ Activity_Create sub တို႕မွာ ေအာက္ပါအတုိင္းေရးျပီး run ပါ။ ေနာက္တစ္ခါ

SetScreenOrientation value ကုိ 0 ထားျပီး run ပါ။ ေနာက္တစ္ခါ 1 ထားျပီး run ပါ။ Run

ျပီးတုိင္းမွာ screen orientation ကုိ ေထာင္လုိက္၊ လွဲလိုက္လုပၾ္ ကည့္ပါ။

Sub Globals

Dim Button1 As Button

Dim p1 As Phone

End Sub

Sub Activity_Create(FirstTime As Boolean)

Button1.Initialize("Button1")

p1.SetScreenOrientation(-1)

Activity.AddView(Button1, 20%x, 30%y, 200dip, 100dip)

End Sub

193
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

Chapter 7

Designer

Application တစ္ခုဖန္တီးတဲ့အခါ အနည္းဆံုး user interface တစ္ခုေတာ့

လိုအပ္တတ္ပါတယ္၉ User interface ကုိ နည္းလမ္း ၀မ်ိဳးနဲ႕ျပဳလုပ္ႏုိင္ပါတယ္၉ ပထမတစ္ခုကေတာ့

Designer ကိသ
ု ံုးျပီး ျပဳလုပ္ျခင္းျဖစ္ပါတယ္၉ ဒုတိယ တစ္ခုကေတာ့ program code ေရးျပီး

ျပဳလုပ္ျခင္းျဖစ္ပါတယ္၉

IDE window မွာ Desinger menu ကုိေရြးလုိက္ရင္ (ပံု ၅-ဿ) မွာျမင္ရတဲ့အတုိင္း Designer

နဲ႕ Abstract Designer window ၀ခုပြင့္လာပါမယ္၉ User interface layout ေတြကို Abstract

Designer/emulator/device ေတြမွာ ဖန္တီးႏုိင္ပါတယ္၉

ပံု ၅-ဿ

194
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
7.1. Menu

7.1.1. File Menu

ပံု ၅-၀

New

Layout အသစ္တစ္ခုျပဳလုပ္ရန္၉

Open

ရွိျပီးသား layout ကုိဖြင့္ရန္၉ အျခား project မွ layout ကို လက္ရွိ project

ထဲထည့္ရန္အတြက္လည္း သံုးႏုိင္ပါတယ္၉

Save

လက္ရွိဖြင့္ထားေသာ layout ကုိသိမ္းရန္၉

Save As

လက္ရွိဖြင့္ထားေသာ layout ကုိ အမည္ေပးျပီး သိမ္းရန္၉

Remove Layout

ရွိျပီးသား layout file ကုိ „Files‟ directory ထဲမွ ဖ်က္ရန္၉

195
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
7.1.2. Edit Menu

ပံု ၅-၁

7.1.3. AddView Menu

Layout ထဲကို view ေတြ ထည့္ရန္၉

ပံု ၅-၂

196
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
User interface layout တစ္ခုျဖစ္လာဘုိ႕ Activity ေပၚမွာ view ေတြကို

ေနရာတက်ထည့္ျပီး ျပဳလုပ္ရပါတယ္၉ ဒါေၾကာင့္ Activity နဲ view ေတြအေၾကာင္း ရွင္းျပပါမယ္၉

Activity

Activity ဆိုတာ user interface layout ကုိေဖာ္ျပတဲ့ window တစ္ခုျဖစ္ပါတယ္၉ Activity

တစ္ခုအတြက္ Activity module တစ္ခု create လုပ္ရပါတယ္၉ Activity module တုိင္းမွာ

predefined activity object တစ္ခုပါရွိပါတယ္၉ အျခား object ေတြကိုအသံုးျပဳလိုပါက define

လုပ္ျပီးမွသံုးလို႕ရပါတယ္၉ Activity object ကေတာ့ predefined object (ၾကိဳတင္္ define

လုပ္ျပီးသား object) ျဖစ္လုိ႕ define မလုပ္ဘဲ သံုးႏုိင္ပါတယ္၉ Activity ဟာ application ရဲ႕

အဓိကအစိတ္အပိုင္းျဖစ္ပါတယ္၉ Activity တစ္ခုမွာ life cycle ေၾကာင့္ျဖစ္ေပၚလာတဲ့ event sub

၁ခုရွိပါတယ္၉

Sub Activity_Create

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause

End Sub

View ေတြကို Activity ထဲထည့္ဘုိ႕အတြက္ Designer ကေန Add View ကုိသံုးျပီး

ထည့္ႏုိင္သလို code ေရးျပီးလည္း ထည့္ႏုိင္ပါတယ္၉ Code ေရးျပီးထည့္မယ္ဆရ


ို င္ „AddView‟

method ကုိသံုးရမယ္၉

Activity.AddView (View, Left, Top, Widht, Height)

197
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
View = Activity ထဲထည့္မယ့္ view ျဖစ္ပါတယ္၉

Top

Left Width

View
Width Height

Activity
ပံု ၅-၃

ဥပမာ Width = 75, Height = 50 ရွိတဲ့ „btnOk‟ button ကုိ Activity ရဲ႕ Left = 10,

Top = 100 မွာထည့္ရန္…

Dim btnOk As Button

Activity.AddView (btnOk, 10, 100, 75, 50)

View ေတြကို Activity ထဲက ျပန္ဖ်က္ဘုိ႕အတြက္ RemoveViewAt method ကို သံုးရမယ္၉

Activity.RemoveViewAt (index)

ဥပမာ Activity ထဲက ပထမဆံုး view ကုိဖ်က္ရန္…

Activity.RemoveViewAt (0)

Layout file ကုိ Activity ထဲထည့္ဘို႕ကိုေတာ့ LoadLayout method ကိုသံုးရမယ္၉

Activity.LoadLayout (Layout file name)

198
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ဥပမာ Layout1.bal file ကုိ Activity ထဲထည့္ရန္

Activity.LoadLayout (“Layout1”)

User က Activity ကုိလက္နဲ႕ထိလိုက္တဲ့အခါ Touch event ျဖစ္ေပၚပါတယ္၉ Touch event

ကုိ handle လုပ္မယ့္ event sub ကေအာက္ပါအတုိင္းျဖစ္ပါတယ္၉

Sub Activity_Touch (Action As Int, X As Float, Y As Float)

End Sub

Touch event ရဲ႕ ပထမဆံုး parameter က Action ျဖစ္ပါတယ္၉ Parameter value

ေတြကေတာ့ ACTION_DOWN, ACTION_MOVE, ACTION_UP ထဲက တစ္ခုခုျဖစ္ႏိုင္ပါတယ္၉ ဒီ

value ကုိၾကည့္ျပီး user ရဲ႕ action ကုိသိႏို္င္ပါတယ္၉ ACTION_DOWN ဆုိရင္ Activity

ကုိလက္နဲ႕ထိတာ၈ ACTION_MOVE ဆိုရင္ Activity ေပၚမွာ လက္ကိုေရႊ႕ေနတာ၈ ACTION_UP

ဆိုရင္ လက္ကိုျပန္ခြာလိုက္တာ ျဖစ္ပါတယ္၉ X နဲ႕ Y ကေတာ့ လက္နဲ႕ထိတဲ့ေနရာ ျဖစ္ပါတယ္၉ ဥပမာ

လက္က Activity ရဲ႕ Left = 10, Top = 30 ကိုထိေနရင္ X = 10, Y = 30 ျဖစ္ပါမယ္၉ User

ကလက္နဲ႕ထိပြတ္လိုက္တုိင္း Touch event ျဖစ္ေပၚလာျပီး parameter value ေတြကို

ရရွိမွာျဖစ္ပါတယ္၉

User က key တစ္ခုကို ႏွိပ္တဲ့အခါ KeyPress event ျဖစ္ေပၚျပီး လက္ကို ျပန္ခြာလိုက္တဲ့အခါ

KeyUp event ျဖစ္ေပၚပါတယ္၉

Activity ထဲကို menu item ေတြထည့္ဘုိ႕ AddMenuItem method ကိုသံုးရမယ္၉

Activity.AddMenuItem (Title, EventName)

ဥပမာ

Activity.AddMenuItem (“Save”,”mnuSave”)

Activity.AddMenuItem (“Settings”,”mnuSettings”)

199
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Menu button ကုိႏွိပ္ရင္ (ပံု ၅-၄) မွာကဲ့သုိ႕ menu item ၀ခုကိုေတြ႕ရမယ္၉

ပံု ၅-၄

AddMenuItem method ကုိ Activity_Create sub အတြင္းမွာဘဲ ေခၚသင့္ပါတယ္၉

AutoCompleteEditText

Text box ထဲမွာ စာလံုးတစ္လံုး ရိုက္လိုက္တာနဲ႕ အဲဒီစာလံုးနဲ႕စတဲ့ က်န္စာလံုးေတြကို

အလိုလိုေဖာ္ျပေပးမယ့္ text box ကုိ AutoCompleteEditText လို႕ေခၚပါတယ္၉ ဥပမာ A

လုိ႕ရိုက္လုိက္ရင္ A နဲ႕စတဲ့ item ေတြကိုျပေပးပါတယ္၉ AutoCompleteEditText အတြက္ item

ေတြကိုေတာ့ SetItems method ကုိသံုးျပီး ၾကိဳတင္ထည့္ထားရပါမယ္၉ User က item တစ္ခုကို click

လိုက္ရင္ ItemClick event ျဖစ္ေပၚပါမယ္၉

Example 7.1

Globals

AtuoCompleteEditText view „auto1‟ကို declare လုပ္ပါ၉

Sub Globals

Dim auto1 As AutoCompleteEditText

End Sub

Activity_Create

auto1 ကုိ initialize လုပ္ပါ၉

200
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
auto1.Initialize ("")

auto1 ကုိ Activity ထဲထည့္ပါ၉

Activity.AddView (auto1, 0dip, 10dip, 300dip, 70dip)

String Array „nomi‟ ကို declare လုပ္ျပီး nomi ထဲကို "angela", "andrea", "angelo",

"alberto", "carlo", "Clotilde", "filippo", "giorgio", "Lyndon" အစရွိတဲ့ String item မ်ားထည့္ပါ၉

Dim nomi () As String

nomi = Array As String( "angela", "andrea", "angelo", "alberto", "carlo",

"Clotilde", "filippo", "giorgio", "Lyndon")

auto1 ထဲကို nomi ထဲက item မ်ားထည့္ပါ၉

auto1.SetItems (nomi)

Sub Activity_Create (FirstTime As Boolean)

auto1.Initialize ("")

Activity.AddView (auto1, 0dip, 10dip, 300dip, 70dip)

Dim nomi () As String

nomi = ArrayAsString( "angela", "andrea", "angelo", "alberto", "carlo", "Clotilde",

"filippo", "giorgio", "Lyndon")

auto1.SetItems (nomi)

End Sub

Application ကို run ရင္ AutoCompleteEditText တစ္ခုေပၚလာမယ္၉ အဲဒီမွာ a

လုိ႕ရိုက္လိုက္တာနဲ႕ a နဲ႕စတဲ့ item မ်ားကိုျပလိမ့္မယ္၉ (ပံု ၅-၅)

201
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-၅

Button

CheckBox

CheckBox ကုိ check/uncheck ျပဳလုပ္ျပီးအလိုရွိရာေရြးခ်ယ္ႏုိင္ပါတယ္၉

202
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
EditText

EditText ထဲမွာ user က text ေတြ ရိုက္ထည့္ႏုိင္ပါတယ္၉ EditText မွာ mode

၀မ်ိဳးရွိပါတယ္၉ SingleLine နဲ႕ MultiLine ပါ၉ MultiLine ျဖစ္ေစရန္ ေအာက္ပါအတုိင္းေရးပါ၉

EditText1.SingleLine = False

User က EditText ကို ထိလိုက္တာနဲ႕ soft keyboard အလိုလို ေပၚလာပါလိမ့္မယ္၉

InputType နဲ႕ keyboard type ကိုလည္း ေျပာင္းေပးႏိုင္ပါတယ္၉ ဥပမာ EditText1.InputType =

EditText1.INPUT_NUMBERS ဆိုရင္ user က EditText ကုိ ထိလုိက္တဲ့အခါ numeric keyboard

ေပၚလာပါမယ္၉ အဲဒါဆုိရင္ EditText1 မွာ number ေတြကိုသာ ရိုက္ထည့္ႏုိင္မွာျဖစ္ပါတယ္၉

Text ေျပာင္းသြားတုိင္းမွာ TextChanged event ျဖစ္ေပၚပါတယ္၉ User က enter key

သုိ႕မဟုတ္ action key (Done/Next) ကိုႏွိပ္လိုက္တာနဲ႕ EnterPressed event ျဖစ္ေပၚပါတယ္၉

View ကုိ focus/unfocus ျဖစ္တုိင္း FocusChanged event ျဖစ္ေပၚပါတယ္၉

HorizontalScrollView

HorizontalScrollView ထဲမွာအျခား view ေတြ ထည့္ႏုိင္ပါတယ္၉ User က အလ်ားလိက


ု ္

scroll လုပ္ျပီး အဲဒီ view ေတြကို ၾကည့္ႏုိင္ပါတယ္၉ HorizontalScrollView မွာ inner panel

တစ္ခုပါရွိပါတယ္၉ Inner panel မွာ view ေတြကိုထည့္ရမွာပါ၉ HorizontalScrollView1 ရဲ႕ inner

panel ထဲကို view ထည့္ရန္ HorizontalScrollView1.Panel.AddView(…) method ကုိေခၚရပါမယ္၉

ScrollView ထဲကို ေနာက္ထပ္ ScrollView ေတာ့ထည့္လို႕မရပါဘူး၉

ImageView

Image ကို ျပေပးႏုိင္တဲ့ view ျဖစ္ပါတယ္၉ Gravity.FILL ဆိုရင္ image ဟာ view

တစ္ခုလံုးအျပည့္ေနရာယူပါမယ္၉ Gravity.CENTER ဆိုရင္ေတာ့ image ဟာ view ရဲ႕ center

မွာေနရာယူပါမယ္၉

203
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

Label

User သိေစခ်င္တဲ့ text ေတြကိုျပဘုိ႕သံုးပါတယ္၉

ListView

Item ေတြ အမ်ားအျပားကို ထည့္ဘို႕ ListView ကုိ သံုးႏုိင္ပါတယ္၉ ListView မွာ event ၀ခု

ရွိပါတယ္၉ Item တစ္ခုကို click ရင္ ItemClick event ျဖစ္ေပၚျပီး item ကုိ ၾကာၾကာဖိထားရင္ေတာ့

ItemLongClick event ျဖစ္ေပၚပါမယ္၉

Example 7.2

ListView မွာ item ေတြကို လုိသေလာက္ ထည့္လို႕ရပါတယ္၉ ListView ကို code

နဲ႕ေသာ္လည္းေကာင္း၈ Designer ကေသာ္လည္း generate လုပ္ႏုိင္ပါတယ္၉ ဒီ example မွာေတာ့

code နဲ႕လုပ္ျပပါမယ္၉

Globals

ListView object ကို declare လုပ္ပါ၉

Sub Globals

Dim ListView1 As ListView

End Sub

204
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Activity_Create

ListView1 ကုိ initialize လုပ္ပါ၉ Event name ကုိ lv1 လို႕ေပးပါ၉

ListView1.Initialize ("lv1")

For Next loop သံုးျပီး ListView1 ထဲကို item အခု 300 ထည့္ပါ၉ ဥပမာ Item #1, Item

#2, …, Item #300

For i = 1 To 300

ListView1.AddSingleLine ("Item #" & i)

Next

ျပီးရင္ ListView1 ကို Activity ထဲထည့္ပါ၉

Activity.AddView (ListView1, 0, 0, 100%x, 100%y)

ListView1_ItemClick

ListView1 ရဲ႕ item တစ္ခုကို click တဲ့အခါ initialize လုပ္စဥ္က ေပးခဲတ


့ ဲ့ event name

(lv1) နဲ႕ Click event ျဖစ္ေပၚပါလိမ့္မယ္၉ Event sub မွာ Position နဲ႕ Value parameter ၀ခု

ကိရ
ု ရွိပါမယ္၉ Position မွာ (click လိုက္တဲ့ item ရဲ႕ position) ပါရွိျပီး Value မွာေတာ့ (click လုိက္တဲ့

item ရဲ႕ value) ပါရွိပါမယ္၉ ဥပမာ Item #25 ကုိ click ရင္ (Position = 24) ျဖစ္ျပီး (Value = Item

#25) ျဖစ္ပါမယ္၉

Activity ရဲ႕ title မွာ Value ကုိ set လုပ္ပါ၉ Item #25 ကုိ click ရင္ Activity ရဲ႕ title မွာ

Item #25 ဆိုျပီးျဖစ္ပါမယ္၉ (ပံု ၅-၆)

Sub lv1_ItemClick (Position As Int, Value As Object)

Activity.Title = Value

End Sub

205
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-၆

ListView model ၁မ်ိဳးရွိပါတယ္၉

 SingleLineLayout

 TwoLinesLayout

 TwoLinesAndBitmap

၁မ်ိဳးလံုးကို နမူနာျပပါမယ္၉

Example 7.3

Button.gif file ကုိ Files tab (File manager) ကေန ထည့္ေပးပါ၉ Globals sub မွာ Bitmap

object တစ္ခုကို declare လုပ္ပါ၉

Dim Bitmap1 As Bitmap

Activity_Create sub မွာ Bitmap1 ကုိ initialize လုပ္ပါ၉ Initialize method မွာ

206
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
parameter ၀ခု ရွိပါတယ္၉ ပထမတစ္ခုက image file ရွိရာ folder ျဖစ္ျပီး ေနာက္တစ္ခုကေတာ့

image file name ျဖစ္ပါတယ္၉ File manager ကထည့္လက


ို ္တဲ့ file ဟာ File.DirAssets မွာရွိမွာမုိ႕

folder ေနရာမွာ File.DirAssets လို႕ေပးပါ၉

Bitmap1.Initialize (File.DirAssets, "Button.gif")

AddSingleLine method ဟာ ListView ထဲကို string တစ္လိုင္းထည့္တာျဖစ္ျပီး

AddTwoLines က ၀လိုင္းထည့္ပါတယ္၉ AddTwoLinesAndBitmap ကေတာ့ string ၀လုိင္းနဲ႕

ပံုတစ္ခုထည့္မွာပါ၉ Activity_Create sub မွာ ေအာက္ပါအတုိင္းေရးျပီး program ကုိ run ပါ၉

For i = 1 To 300

ListView1.AddSingleLine ("Item #" & i)

ListView1.AddTwoLines ("Item #" & i, "This is the second line.")

ListView1.AddTwoLinesAndBitmap ("Item #" & i, "This is the second line.",

Bitmap1)

Next

ပံု ၅-၇

Loop တစ္ၾကိမ္ပတ္တုိင္း SingleLine, TwoLines, TwoLinesAndBitmap

ကိုထည့္မွာျဖစ္လုိ႕ ListView1 ထဲမွာ စုစုေပါင္း item 900 ျဖစ္သြားပါမယ္၉ ဒီ example မွာ item

အားလံုးအတြက္ bitmap က တစ္ခုထဲျဖစ္ေနပါတယ္၉ Item တစ္ခခ


ု ်င္းအတြက္ bitmap တစ္ခစ
ု ီ

သတ္မွတ္ခ်င္လည္း သတ္မွတ္လို႕ရပါတယ္၉

207
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Customizing Each Type

ListView ကို customize (မိမိစိတ္တုိင္းက်ပံုစ)ံ လုပ္ႏုိင္ပါတယ္၉ ListView model

တစ္မ်ိဳးစီမွာ ItemHeight, Background နဲ႕အျခား property ေတြကို မိမိစိတ္တုိင္းက်

ျပင္ဆင္သံုးႏုိင္ပါတယ္၉

Example 7.4

SingleLineLayout ရဲ႕ item height ေတြကို 100dip ထားပါ၉

ListView1.SingleLineLayout.ItemHeight = 100dip

Label ရဲ႕ TextSize ကုိ 20 လုိ႕ထားပါ၉

ListView1.SingleLineLayout.Label.TextSize = 20

TextColor ကို အျပာေရာင္ထားပါ၉

ListView1.SingleLineLayout.Label.TextColor = Colors.Blue

Gravity ကို center ထားပါ၉

ListView1.SingleLineLayout.Label.Gravity = Gravity.CENTER

Run လုိက္ရင္ (ပံု ၅-ဿှ) မွာ ျပထားတဲ့အတုိင္းျမင္ရပါမယ္၉

အခုက SingleLineLayout အတြက္သာျဖစ္ပါတယ္၉ SingleLineLayout ကိုနမူနာျပဳျပီး က်န္

model ၀ခုအတြက္လည္း ျပဳလုပၾ္ ကည့္ပါ၉

208
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-ဿှ

Return Value

User က ListView item တစ္ခုကို click လုိက္ရင္ ItemClick event ျဖစ္ေပၚပါလိမ့္မယ္၉

ItemClick event ျဖစ္ေပၚတဲ့အခါ click ခံရေသာ item ရဲ႕ value ကုိ parameter အျဖစ္ pass

လုပ္ပါတယ္၉ Item ရဲ႕ value ဆိုတာက first line မွာ ထားတဲ့ String ကုိေျပာတာပါ၉ ဒါေပမယ့္

ကိုယ္ၾကိဳက္ရာ object ကုိ return ျပန္ေအာင္ ေအာက္ပါ method ေတြသံုးျပီးလုပ္လို႕ရပါတယ္၉

AddSingleLine2

AddTwoLines2

AddTwoLinesAndBitmap2

ဥပမာ

ListView1.AddSingleLine2 ("Item #1, 1)

ListView1.AddTwoLines2 ("Item #1”, "This is the second line.", 1)

ListView1.AddTwoLinesAndBitmap2 ("Item #1", "This is the second line.",

Bitmap1, 1)

209
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ဒီ method ေတြမွာ ေနာက္ထပ္ parameter တစ္ခုအပိုပါ ပါတယ္၉ အဲဒါကေတာ့ return

value ပါ၉ Item တစ္ခုကို click လုိက္ရင္ return ျပန္ေစခ်င္တဲ့ value ကုိေပးထားလုိ႕ရပါတယ္၉

အထက္ပါ code မွာ return value ကို 1 လို႕ေပးထားတဲ့အတြက္ item ကို click ရင္ event sub မွာ

Value = 1 ျဖစ္ပါမယ္၉

Background Optimization

Actiivty ရဲ႕ background color သတ္မွတ္ျပပါမယ္၉ Gradient color ဆိုတာအေရာင္

၀ခုစပ္ျပီးသံုးရတဲ့ color ျဖစ္ပါတယ္၉

GradientDrawable object တစ္ခု declare လုပ္ပါ၉

Dim GD As GradientDrawable

GD ကုိ initialize လုပ္ပါ၉ Parameter ၀ခုသံုးရမယ္၉ ပထမ parameter က orientation ပါ၉

TR = Top Right , BL = Bottom Left ျဖစ္ပါတယ္၉ ဒုတိယ parameter ကေတာ့ အေရာင္ ၀မ်ိဳး

ပါှင္တဲ့ Array ျဖစ္ပါတယ္၉ အေရာင္ ၀မ်ိဳးကေတာ့ၾကိဳက္ရာအေရာင္ေရြးႏုိင္ပါတယ္၉

GD.Initialize ("TR_BL", Array As Int(Colors.Gray, Colors.LightGray))

အေရာင္ ၀မ်ိဳးစပ္ထားတဲ့ object „GD‟ ကုိ Activity ရဲ႕ background

အျဖစ္သတ္မွတ္လိုက္ပါ၉ Activity ရဲ႕ background color ဟာ အေရာင္ ၀မ်ိဳးစပ္နဲ႕ျဖစ္ေနပါလိမ့္မယ္၉

Activity.Background = GD

Tips

 AddTwoLinesAndBitmap method ဟာ String ၀လိုင္းနဲ႕ bitmap ကို ListView

ထဲထည့္ေပးပါတယ္၉ (String တစ္လုိင္း + bitmap) သာလိခ


ု ်င္ရင္ေတာ့ second label ရဲ႕

visible property ကုိ false လုပပ


္ ါ၉

210
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ListView1.TwoLinesAndBitmap.SecondLabel.Visible = False

 ListView မွာထည့္တဲ့ item မ်ားေနရင္ fast scroller ကုိ enable လုပ္ထားသင့္ပါတယ္၉

ဥပမာ

ListView1.FastScrollEnabled = true

Complete Code

#Region Project Attributes

#ApplicationLabel: B4A Example

#VersionCode: 1

#VersionName:

#SupportedOrientations: unspecified

#CanInstallToExternalStorage: False

#End Region

#Region Activity Attributes

#FullScreen: False

#IncludeTitle: True

#End Region

Sub Process_Globals

End Sub

Sub Globals

Dim ListView1 As ListView

Dim Bitmap1 As Bitmap

211
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Dim GD As GradientDrawable

End Sub

Sub Activity_Create (FirstTime As Boolean)

ListView1.Initialize ("lv1")

Bitmap1.Initialize (File.DirAssets, "Capture.png")

ListView1.SingleLineLayout.ItemHeight = 100dip

ListView1.SingleLineLayout.Label.TextSize = 20

ListView1.SingleLineLayout.Label.TextColor = Colors.Blue

ListView1.SingleLineLayout.Label.Gravity = Gravity.CENTER

GD.Initialize ("TR_BL", Array As Int(Colors.Green, Colors.Red))

Activity.Background = GD

For i = 1 To 300

ListView1.AddSingleLine ("Item #" & i)

ListView1.AddTwoLines ("Item #" & i, "This is the second line.")

ListView1.AddTwoLinesAndBitmap ("Item #" & i, "This is the second

line.", Bitmap1)

' ListView1.AddSingleLine2 ("Item #" & i, i)

' ListView1.AddTwoLines2 ("Item #" & i, "This is the second line.",i)

' ListView1.AddTwoLinesAndBitmap2 ("Item #" & i, "This is the

„ second line.", Bitmap1,i)

„ ListView1.TwoLinesAndBitmap.SecondLabel.Visible = False

Next

Activity.AddView (ListView1, 0, 0, 100%x, 100%y)

End Sub

212
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub lv1_ItemClick (Position As Int, Value As Object)

Activity.Title = Value

End Sub

Panel

သူကိုယ္တုိင္ view တစ္ခုျဖစ္သလို သူ႕ထဲမွာ အျခား view ေတြကလ


ို ည္း ထည့္ႏုိငပ
္ ါတယ္၉

Panel ထဲကိုအျခား view ေတြထည့္တဲ့အခါ program code ေရးျပီးထည့္ႏုိင္သလို layout

file ကုိ Panel ေပၚတင္လို႕လည္းရပါတယ္၉ ဥပမာ

Panel1.LoadLayout (“Layout1”)

Panel ကို touch လုပ္ရင္ Touch event ျဖစ္ေပၚပါမယ္၉

ProgressBar

Task တစ္ခုရဲ႕ ျပီးေျမာက္မႈအေျခအေနကိုျပတဲ့ view ျဖစ္ပါတယ္၉ Progress value ကေတာ့

0 နဲ႕ 100 ၾကားျဖစ္ပါတယ္၉

213
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
RadioButton

RadioButton group ထဲမွာ RadioButton တစ္ခုမကရွိေနေပမယ့္ တစ္ခုကိုသာ

ေရြးခ်ယ္ႏုိင္ပါတယ္၉ RadioButton တစ္ခုကို check လုပ္ျပီးေနာက္ အျခား RadioButton တစ္ခုကို

check လုပ္လုိက္ရင္ မူလ check လုပ္ထားတဲ့ RadioButton ဟာအလုိအေလွ်ာက္ uncheck

ျဖစ္သြားပါမယ္၉

ScrollView

အျခား view ေတြထည့္ျပီး ေထာင္လုိက္ scroll လုပ္ၾကည့္ႏုိင္တဲ့ view ျဖစ္ပါတယ္၉

ScrollView မွာ inner panel တစ္ခုရွိျပီး inner panel ေပၚမွာ အျခား view

ေတြကိုထည့္ရမွာျဖစ္ပါတယ္၉ ScrollView1 ရဲ႕ inner panel ထဲကို view ထည့္ရန္

ScrollView1.Panel.AddView (…) method ကုိေခၚရပါမယ္၉ ScrollView ထဲကိုေနာက္ထပ္

ScrollView ေတာ့ ထည့္လို႕မရပါဘူး၉

Example 7.5

Layout file တစ္ခု create လုပ္ပါ၉ Layout file name ကုိ Layout1 လို႕ေပးပါ၉ Layout မွာ

ScrollView တစ္ခုထည့္ပါ၉ ScrollView1 လို႕အမည္ေပးပါ၉

ScrollView ေပၚကို object/view ေတြတင္ႏုိင္ပါတယ္၉ ဒီ example မွာေတာ့ Label ဿ၄ ခုကို

code နဲ႕ create လုပ္ျပီး ScrollView ေပၚတင္ျပမွာပါ၉ Activity မွာ Label ဿ၄ ခုလံုး

တစ္ျပိဳင္နက္ျပဘုိ႕မဆန္႕ပါဘူး၉ ဒါေၾကာင့္ ScrollView ေပၚတင္မွာပါ၉ ScrollView ရဲ႕ internal panel

ေပၚမွာ Label view ဿ၄ ခုတင္ရပါမယ္၉ ျပီးရင္ ScrollView ကုိ scroll လုပ္ျပီး Label ေတြကို

ၾကည့္ႏုိင္ပါတယ္၉ Code editor မွာ ေအာက္ပါအတုိင္းေရးပါ၉

214
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub Globals

Dim ScrollView1 As ScrollView

End Sub

Sub Activity_Create (FirstTime AsBoolean)

Activity.LoadLayout ("Layout1")

Dim j As Int

j=0

For i = 1 To 16

Dim lblLabel As Label

lblLabel.Initialize ("")

lblLabel.Text = "Label"& i

ScrollView1.Panel.AddView (lblLabel, 0, j, 100%x, 12.5%y)

j = lblLabel.Top + lblLabel.Height

Next

ScrollView1.Panel.Height = lblLabel.Height * 16

End Sub

Example 7.6

SD card ထဲမွာ „Images‟ folder တစ္ခုျပဳလုပ္ျပီး image file မ်ားထည့္ထားပါ၉ အဲဒီ image

ေတြကို ScrollView မွာတင္ျပမွာပါ၉ ပံုေတြဘယ္ေလာက္မ်ားမ်ား ScrollView မွာတင္ထားတဲ့အတြက္

scroll လုပ္ျပီးၾကည့္ႏုိင္ပါမယ္၉ Scroll မလုပ္ဘဲ Up, Down button ေတြသံုးျပီးလည္း

ပံုေတြကိုၾကည့္ႏုိင္ေအာင္လုပ္ပါမယ္၉ Project တစ္ခု create လုပ္ျပီးေအာက္ပါအတုိင္းဆက္လုပ္ပါ၉

215
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Layout Name: 1

ပံု ၅-ဿဿ

216
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-ဿ၀

ပံု ၅-ဿ၁

ပံု ၅-ဿ၂

217
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Process_Globals

List object „Bitmaps‟ ကုိ declare လုပ္ပါ၉

Sub Process_Globals

Dim Bitmaps As List

End Sub

Globals

ScrollView နဲ႕ Button ၀ခုကို declare လုပ္ပါ၉

Sub Globals

Dim ScrollView1 As ScrollView

Dim btnDown As Button

Dim btnUp As Button

End Sub

Activity_Create

Image file ေတြကို load လုပ္ရန္ အခ်ိန္အနည္းငယ္ ၾကာမွာျဖစ္လို႕ load လုပ္ေနစဥ္အတြင္း

user ကို progress dialog တစ္ခုျပထားပါမယ္၉ ဒါေၾကာင့္ FirstTime = True ျဖစ္ရင္

ProgressDialog တစ္ခုျပပါ၉

ProgressDialogShow ("Loading images")

SD card ထဲကပံုေတြကို load လုပ္ရန္ LoadImages sub ကုိေခၚပါ၉ „Bitmaps‟ List ထဲမွာ

image ေတြ ေရာက္လာပါလိမ့္မယ္၉

LoadImages

218
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Load လုပ္ျပီးရင္ progress dialog ကုိ hide ျပန္လုပ္ပါ၉

ProgressDialogHide

Layout file „1‟ ကုိ Activity ေပၚတင္ပါ၉

Activity.LoadLayout ("1")

ScrollView မွာထည့္မယ့္ image အေရအတြက္ေပၚ မူတည္ျပီး ScrollView ရဲ႕ inner panel

height ကိုသတ္မွတ္ပါ၉ Image တစ္ခုစီရဲ႕ height ကို 200dip ထားပါမယ္၉ ဒါေၾကာင့္ inner panel

ရဲ႕ height ကုိသတ္မွတ္ရန္ image အေရအတြက္ကို 200dip နဲ႕ေျမွာက္ရပါမယ္၉ Image

အေရအတြက္ကေတာ့ Bitmaps.Size ျဖစ္ပါတယ္၉ „Bitmaps‟ List ထဲမွာ image ေတြရွိေနမွာမုိ႕

Bitmaps.Size ဟာ image အေရအတြက္ဘဲ ျဖစ္ပါတယ္၉

ScrollView1.Panel.Height = 200dip * Bitmaps.Size

Bitmaps ထဲက image ေတြကို ImageView တစ္ခုစီမွာထည့္ပါမယ္၉ Image

အားလံုးအတြက္ လုပ္ရမွာမုိ႕ For loop သံုးပါမယ္၉ For loop ကုိ i = 0 ကေန Bitmaps.Size – 1

ထိပတ္ရပါမယ္၉ Image တစ္ခုအတြက္ ImageView တစ္ခုစီ create လုပ္ပါမယ္၉ ImageView „iv‟

ကုိ initialize လုပ္ပါ၉

For i = 0 To Bitmaps.Size - 1

Dim iv As ImageView

iv.Initialize ("")

Image ေတြကို ImageView ရဲ႕ background မွာထားပါမယ္၉ ဒါေၾကာင့္ BitmapDrawable

„bd‟ ကို declare လုပ္ပါ၉ „bd‟ ကုိ initialize လုပ္တဲ့အခါ Bitmaps ထဲက image တစ္ခုကိုယူျပီး

parameter အျဖစ္ေပးလိုက္ပါ၉ „bd‟ ကုိ ImageView „iv‟ ရဲ႕ background အျဖစ္

သတ္မွတ္ေပးလုိက္ပါ၉

219
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Dim bd As BitmapDrawable

bd.Initialize (Bitmaps.Get(i))

iv.Background = bd

ImageView ကို ScrollView1.Panel ေပၚတင္ပါ၉

ScrollView1.Panel.AddView (iv, 5dip, 5dip + i * 200dip, ScrollView1.Width -

10dip, 190dip)

ဒီလိုနဲ႕ Bitmaps ထဲက image အားလံုးအတြက္ loop ပတ္ျပီးလုပ္သြားရပါမယ္၉

btnUp နဲ႕ btnDown ေတြရဲ႕ Tag ကို -200dip နဲ႕ 200dip အသီးသီး သတ္မွတ္ထားပါ၉

btnUp.Tag = -200dip

btnDown.Tag = 200dip

LoadImages

SD card ထဲက image ေတြကိုယူျပီး Bitmaps ထဲထည့္ရမယ္၉ ဒါေၾကာင့္ Bitmaps ကုိ

initialize လုပ္ပါ၉

Bitmaps.Initialize

Image file name ေတြကိုထည့္ရန္ „files‟ List တစ္ခု declare လုပ္ပါ၉

Dim files As List

SD card ေပၚက image folder name ကုိထည့္ရန္ „imagesFolder‟ String ကို declare

လုပ္ပါ၉

Dim imagesFolder As String

220
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ImagesFolder ထဲမွာ SD card ေပၚက image file မ်ားထည့္ထားတဲ့ folder name

ကုိထည့္ပါ၉ Folder name က Images ေနာ္၉

imagesFolder = File.DirRootExternal & "/Images"

အဆိုပါ folder name ရွိမရွိစစ္ပါ၉ မရွိရင္ "Images folder not found: " ဆိုတဲ့ toast

message ျပျပီးထြက္ပါ၉

If File.Exists (imagesFolder, "") = False Then

ToastMessageShow ("Images folder not found: " & CRLF & imagesFolder,

True)

Return

End If

Folder ရွိရင္ေတာ့ folder ထဲကဖုိငအ


္ မည္ေတြကို „files‟ List ထဲထည့္ပါ၉

files = File.ListFiles(imagesFolder)

SD card ထဲက image ဖုိင္ေတြကိုစဖတ္ပါမယ္၉ ရွိသမွ်ဖိုင္အားလံုးကို တစ္ဖိုင္ျပီးတစ္ဖုိင္

ယူမွာမုိ႕ For loop သံုးပါ၉ ဖတ္ေနစဥ္အတြင္း ProgressDialog ကို animate ျဖစ္ေအာင္

(အှိုင္းေလးလည္ေနေအာင္) DoEvents ကုိေခၚလုိက္ပါ၉

For i = 0 To files.Size - 1

DoEvents

„files‟ List ထဲက item တစ္ခုကိုယူျပီး String „f‟ ထဲထည့္ပါ၉

f = files.Get (i)

221
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
f ထဲမွာ image file name တစ္ခုေရာက္လာပါမယ္၉ f ကို lower case ေျပာင္းျပီး .jpg

နဲ႕ဆံုးသလားစစ္ပါ၉

If f.ToLowerCase.EndsWith (".jpg") Then

(.jpg file ေတြကိုသာယူမွာမုိ႕ပါ) .jpg နဲ႕ဆံုးရင္ InitializeSample method သံုးျပီး

အဲဒီဖုိင္ကုိ Bitmap „b‟ ထဲထည့္ပါ၉

Dim b As Bitmap

b.InitializeSample (imagesFolder, f, 200dip, 200dip)

"out of memory" errors မျဖစ္ရေလေအာင္ InitializeSample method

ကိုသံုးျခင္းျဖစ္ပါတယ္၉ InitializeSample method မွာ image folder name, image file name,

MaxWidth နဲ႕ MaxHeight parameter ၂ခရွိပါတယ္၉ မူလ image size ဟာ

MaxWidth/MaxHeight ထက္ၾကီးခဲ့ေသာ္ size ကုိ MaxWidth/MaxHeight

ထက္မေက်ာ္ေအာင္ခ်ံဳ႕လိုက္ပါမယ္၉ ဒီလိုလုပ္လိုက္ရင္ မူလ bitmap ထက္ေတာ့ resolution

နိမ့္သြားမွာပါ၉

„b‟ ကို Bitmaps array ထဲထည့္ပါ၉

Bitmaps.Add (b)

ListView မွာတင္မယ့္ image အေရအတြက္ကအ


ို မ်ားဆံုး 50 သာလက္ခံမွာမုိ႕

Bitmaps.Size > 50 ျဖစ္သလားစစ္ပါတယ္၉ 50 ထက္ေက်ာ္ရင္ေတာ့ Exit နဲ႕ထြက္သြားမယ္၉

If Bitmaps.Size > 50 Then Exit

ဒီလုိနဲ႕ image ေတြအားလံုးကို ListView ေပၚတင္ေပးသြားပါမယ္၉ အားလံုးျပီးသြားရင္ SD

card ထဲက Images folder ထဲမွာ .jpg image file ဘယ္ႏွစ္ခုရတ


ွိ ယ္ဆိုတာ toast message

နဲ႕ျပပါမယ္၉

ToastMessageShow ("Found " & Bitmaps.Size & " images", True)

222
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
btn_Click

btnUp/btnDown button ၀ခုလံုးရဲ႕ event name ကုိ btn လုိ႕ေပးခဲ့တဲ့အတြက္ btnUp ကုိ

click သည္ျဖစ္ေစ btnDown ကုိ click သည္ျဖစ္ေစ btn Click event ျဖစ္ေပၚပါမယ္၉ Click

လုပခ
္ ံရတဲ့ button ရဲ႕ reference ဟာ Sender ထဲမွာရွိေနမွာပါ၉ ဒါေၾကာင့္ Sender ကုိ View „ v ‟

ထဲထည့္ပါ၉ ဒါဟာ Object type ကေန View type အျဖစ္ cast လုပ္လုိက္တာျဖစ္ပါတယ္၉

Dim v As View

v = Sender

btnUp.Tag = -200dip, btnDown.Tag = 200dip ျဖစ္ပါတယ္၉ ဒါေၾကာင့္ btnUp ကုိ click

ရင္ v.Tag = -200dip ျဖစ္ပါမယ္၉ btnDown ကုိ click ရင္ v.Tag = 200dip ျဖစ္ပါမယ္၉

ScrollView1.ScrollPosition ကို v.Tag နဲ႕ေပါင္းျပီး ScrollView1 ရဲ႕ ScrollPosition

အျဖစ္သတ္မွတ္ေပးလုိက္ပါ၉

ScrollView1.ScrollPosition = ScrollView1.ScrollPosition + v.Tag

btnUp ကုိ click တာဆိုရင္ လက္ရွိ position ထဲက -200dip ႏႈတ္တာမုိ႕ ScrollPosition ဟာ

အေပၚကို 200dip တက္သြားပါမယ္၉ 200dip ဆိုတာ image တစ္ခုစီရဲ႕ height ျဖစ္ပါတယ္၉

ဒါေၾကာင့္ လက္ရွိပံုရဲ႕ အေပၚကပ္လွ်က္ပံုကို ျမင္လာရမွာျဖစ္ပါတယ္၉

btnDown ကုိ click တာဆိုရင္ လက္ရွိ position ကို 200dip ေပါင္းတာမုိ႕ ScrollPosition

ဟာ ေအာက္ကို 200dip ဆင္းသြားပါမယ္၉ အဲဒီအခါ လက္ရွိပံုရဲ႕ ေအာက္ကပ္လွ်က္ပံုကို

ျမင္လာရမွာျဖစ္ပါတယ္၉

Complete Code

#Region Module Attributes

#FullScreen: False

#IncludeTitle: True

223
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
#ApplicationLabel: ScrollView

#VersionCode: 1

#VersionName:

#SupportedOrientations: unspecified

#CanInstallToExternalStorage: True

#End Region

Sub Process_Globals

Dim Bitmaps As List

End Sub

Sub Globals

Dim ScrollView1 As ScrollView

Dim btnDown As Button

Dim btnUp As Button

End Sub

Sub Activity_Create (FirstTime As Boolean)

If FirstTime Then

ProgressDialogShow("Loading images")

LoadImages

ProgressDialogHide

End If

Activity.LoadLayout ("1")

ScrollView1.Panel.Height = 200dip * Bitmaps.Size

224
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
For i = 0 To Bitmaps.Size - 1

Dim iv As ImageView

iv.Initialize ("")

Dim bd As BitmapDrawable

bd.Initialize (Bitmaps.Get (i))

iv.Background = bd

ScrollView1.Panel.AddView (iv, 5dip, 5dip + i * 200dip,

ScrollView1.Width - 10dip, 190dip)

Next

btnUp.Tag = -200dip

btnDown.Tag = 200dip

End Sub

Sub LoadImages

Bitmaps.Initialize

Dim files As List

Dim imagesFolder As String

imagesFolder = File.DirRootExternal & "/Images"

If File.Exists (imagesFolder, "") = False Then

ToastMessageShow ("Images folder not found: " & CRLF & imagesFolder,

True)

Return

End If

files = File.ListFiles(imagesFolder)

For i = 0 To files.Size - 1

225
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
DoEvents

Dim f As String

f = files.Get (i)

If f.ToLowerCase.EndsWith (".jpg") Then

Dim b As Bitmap

b.InitializeSample (imagesFolder, f, 200dip, 200dip)

Bitmaps.Add (b)

If Bitmaps.Size > 50 Then Exit

End If

Next

ToastMessageShow ("Found " & Bitmaps.Size & " images", True)

End Sub

Sub btn_Click

Dim v As View

v = Sender

ScrollView1.ScrollPosition = ScrollView1.ScrollPosition + v.Tag

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

226
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-ဿ၃

SeekBar

Slider ကုိ ဆြဲေရႊ႕ျခင္းျဖင့္ value သတ္မွတ္ေပးႏုိင္တဲ့ view ျဖစ္ပါတယ္၉ Value

ေျပာင္းသြားတုိင္းမွာ ValueChanged event ျဖစ္ေပၚပါမယ္၉ User က slider ကုိဆြဲေရႊ႕ျပီး vaue

change တာလား program code နဲ႕ value change တာလား ဆိုတာကို ခြဲျခားဘုိ႕ UserChanged

parameter ကုိၾကည့္ရမယ္၉ UserChanged = True ျဖစ္ရင္ user က value ကုိ change တာျဖစ္ျပီး

UserChanged = False ျဖစ္ရင္ေတာ့ program code နဲ႕ change တာျဖစ္ပါတယ္၉

Spinner

Item ေတြကၾို ကိဳတင္ထည့္ထားျပီးျဖစ္ေသာ္လည္း user က drop down arrow ကုိ click

လိုက္မွသာ ထည့္ထားတဲ့ item ေတြကိုျပမယ့္ view ျဖစ္ပါတယ္၉ အဲဒီ item ေတြထဲက တစ္ခုကို user

ကေရြးခ်ယ္ယူႏုိင္ပါတယ္၉ Item တစ္ခုကို click ရင္ ItemClick event ျဖစ္ေပၚမွာပါ၉

227
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

TabHost

Tab page ေတြတစ္ခမ


ု ကထည္ႏ
့ ုိင္တဲ့ view ျဖစ္ပါတယ္၉ Tab page တုိင္းမွာ လိုအပ္တဲ့

view ေတြနဲ႕ design လုပ္ထားရပါမယ္၉

Example 7.7

TabHost view ကိုသံုးျပီး Activity တစ္ခုေပၚမွာ layout ေတြအမ်ားၾကီး တင္လို႕ရပါတယ္၉

TabHost ထဲကို tab page ေတြထည့္တဲ့နည္းမ်ားစြာရွိတဲ့အနက္ tab page တစ္ခအ


ု တြက္ layout file

တစ္ခုစီ ျပဳလုပ္ျပီး အဲဒီ layout file ေတြကို TabHost မွာတင္တဲ့နည္း ကိုျပပါမယ္၉

TabHost ထဲကို tab ေတြထည့္ဘုိ႕အတြက္ AddTab method ကိုသံုးရမယ္၉ User interface

layout file ၂ခုလုပ္ထားပါ၉ Main, P1, P2, P3 လို႕အမည္ေပးထားပါ၉ Main မွာ TabHost view

တစ္ခုထည့္ျပီး အမည္ကို „tbhPages‟ လို႕ေပးထားပါ၉

က်န္တဲ့ layout file ေတြမွာေတာ့ Label တစ္ခစ


ု ီထည့္ျပီး Label text ကို Page 1, Page 2,

Page 3 လို႕အသီးသီးေပးထားပါ၉ Activity color ကိုလည္း တစ္မ်ိဳးစီ သတ္မွတ္ေပးထားပါ၉

Globals sub မွာ TabHost object „tbhPages‟ ကုိ declare လုပ္ပါ၉

Sub Globals

Dim tbhPages As TabHost

End Sub

Activity_Create sub မွာ Main layout file ကုိ Activity ေပၚတင္ပါ၉ „AddTab‟ method

ကုိသံုးျပီး „tbhPages‟ ထဲကို tab ေတြထည့္ပါ၉ AddTab method မွာ parameter ၀ခု ေပးရပါတယ္၉

တစ္ခုကေတာ့ tab မွာျမင္ရမယ့္ string ျဖစ္ျပီး (ဥပမာ Page 1) ေနာက္တစ္ခုကေတာ့ tab ကုိႏွိပ္ရင္

TabHost ေပၚတင္ရမယ့္ layout file name ပါ၉ (ဥပမာ P1)

228
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Layout file ၁ခုျဖစ္တဲ့ P1, P2, P3 တုိ႕ကို TabHost ေပၚေရာက္ေအာင္

ေအာက္ပါအတုိင္းတင္ေပးပါ၉

Sub Activity_Create (FirstTime AsBoolean)

Activity.LoadLayout ("Main")

tbhPages.AddTab("Page 1","Page1")

tbhPages.AddTab("Page 2","Page2")

tbhPages.AddTab("Page 3","Page3")

End Sub

Program ကုိ run ျပီး tab တစ္ခုခ်င္းစီကိုႏွိပ္ၾကည့္ပါ၉ သက္ဆိုင္ရာ layout file

ေတြကိုျမင္ရပါလိမ့္မယ္၉

ပံု ၅-ဿ၄

229
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Example 7.8

TabHost ထဲကို tab ေတြထည့္ဘုိ႕ AddTab method သံုးတာျပခဲ့ျပီးျဖစ္ပါတယ္၉

ဒီတစ္ခါေတာ့ AddTabWithIcon method သံုးနည္းကိုျပပါမယ္၉ AddTabWithIcon method

နဲ႕ထည့္ရင္ icon ပါထည့္ႏိုင္တယ္၉ AddTab method ဆိုရင္ေတာ့ icon မပါဘူး၉

Layout file ၁ခုလုပ္ပါ၉ P1, P2, P3 လို႕အသီးသီးအမည္ေပးပါ၉ Layout file ေတြမွာ

ကိုယ္ထည့္ခ်င္တဲ့ view ေတြထည့္ထားပါ၉ AddTabWithIcon method မွာ icon အျဖစ္သံုးဘုိ႕

image file ၀ခုကို File manager ကေန ထည့္ထားပါ (ဥပမာ ic.png, ic_selected.png)၉ တစ္ခုက

Tab ကုိ select မလုပ္မီျပမယ့္ icon ျဖစ္ျပီး ေနာက္တစ္ခုက tab ကုိ select လုပ္ျပီးေနာက္ျမင္ရမယ့္

icon ျဖစ္ပါတယ္၉

#Region Module Attributes

#FullScreen: False

#IncludeTitle: True

#ApplicationLabel: UserInterfaceTabHost

#VersionCode: 1

#VersionName:

#SupportedOrientations: unspecified

#CanInstallToExternalStorage: False

#End Region

Sub Process_Globals

End Sub

230
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Globals

TabHost object „TabHost1‟ ကို declare လုပ္ပါ၉

Sub Globals

Dim TabHost1 As TabHost

End Sub

Activity_Create

Bitmap object „bmp1‟ နဲ႕ „bmp2‟ ကို declare လုပ္ပါ၉

Dim bmp1, bmp2 As Bitmap

ic.png နဲ႕ ic_selected.png image ေတြကို bmp1 နဲ႕ bmp2 ထဲအသီးသီး ထည့္ပါ၉

bmp1 = LoadBitmap (File.DirAssets, "ic.png")

bmp2 = LoadBitmap (File.DirAssets, "ic_selected.png")

TabHost ထဲကို icon ၀ခုပါတဲ့ tab ထည့္ပါ၉ AddTabWithIcon method မွာ parameter

၂ခုယူပါတယ္၉

 Event name

 Bitmap

 Bitmap

 Layout file name တုိ႕ျဖစ္ပါတယ္၉

Icon ၀ခု (Bitmap ၀ခု) သံုးထားပါတယ္၉ ပထမတစ္ခုကေတာ့ tab ကုိ select

မလုပ္ခင္အခ်ိန္မွာ ျပဖုိ႕ပါ၉ ဒုတိယတစ္ခုကေတာ့ select လုပ္ျပီးတဲ့အခ်ိန္မွာျပဖုိ႕ပါ၉ Selected icon

အတြက္ dark ျဖစ္တဲ့ icon ကိုသံုးျပီး not selected icon အတြက္ light ျဖစ္တဲ့ icon

ကုိသံုးသင့္ပါတယ္၉

231
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
AddTab method မွာ parameter ၀ခုယူပါတယ္၉ Event name နဲ႕ layout file name ပါ၉

TabHost1.AddTabWithIcon ("Name", bmp1, bmp2, "Page1")

TabHost1.AddTab ("Color", "Page2")

TabHost1.AddTab ("Animal", "Page3")

အထက္ပါ code မွာ TabHost1 ထဲကို tab ၁ခုထည့္ပါတယ္၉ ပထမတစ္ခုမွာ icon ၀ခုပါျပီး

က်န္ ၀ခုမွာေတာ့ icon မထည့္ထားပါဘူး၉ ပထမ tab ကုိ ႏွိပ္ရင္ P1 layout ျမင္ရမယ္၉ ဒုတိယ tab

ကိုႏွိပ္ရင္ P2 နဲ႕ တတိယ tab ကုိႏွိပ္ရင္ P3 layout ကိုျမင္ရမယ္၉

ပံု ၅-ဿ၅

ToogleButton
ON/OFF mode ၀ခုပါတဲ့ button ျဖစ္ပါတယ္၉ ToogleButton ကို click လုပ္ရင္ မူလက ON
ဆုိရင္ OFF ျဖစ္သြားျပီး၈ မူလက OFF ဆုိရင္ ON ျဖစ္သြားပါမယ္၉

232
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Example 7.9

MediaPlayer object ကို audio file ေတြ play ဘုိ႕အတြက္အသံုးျပဳပါတယ္၉ MediaPlayer

က support လုပ္ေပးတဲ့ format ေတြကေတာ့ mp3, midi, wave, ogg အစရွိသည္တုိ႕ျဖစ္ပါတယ္၉

Play ခ်င္တဲ့ audio file ကုိ File manager ကေနထည့္ထားပါ၉ (ပံု ၅.ဿ၆) အတုိင္း layout

တစ္ခုဖန္တီးပါ၉

ပံု ၅-ဿ၆

233
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Properties of Views

ပံု ၅-ဿ၇ ပံု ၅-၀ှ

234
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-၀ဿ ပံု ၅-၀၀

235
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-၀၁

Layout Name: 1

File: MyMusic.mp3 (သို႕မဟုတ)္ မိမိ play လုိေသာဖိုင္

MediaPlayer နဲ႕ Timer object ေတြကို Process_Globals မွာ Dim လုပ္ပါ၉

Sub Process_Globals

Dim MediaPlayer1 As MediaPlayer

Dim Timer1 As Timer

End Sub

236
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
View ေတြကုိ Globals မွာ Dim လုပ္ပါ၉

Sub Globals

Dim barPosition As SeekBar

Dim barVolume As SeekBar

Dim lblPosition As Label

Dim Looping As ToggleButton

End Sub

FirstTime = True ျဖစ္ရင္ MediaPlayer1 နဲ႕ Timer1 တုိ႕ကို initialize လုပ္ပါ၉ Play ခ်င္တဲ့

audio file ကုိ load လုပ္ပါ၉ Timer1 ကုိ initialize လုပ္တဲ့အခါ interval ပါ သတ္မွတ္ေပးပါ၉ (ဥပမာ

1000)

If FirstTime Then

MediaPlayer1.Initialize ( )

MediaPlayer1.Load (File.DirAssets, "MyMusic.mp3")

Timer1.Initialize ("t1", 1000)

End If

Layout file „1‟ ကို Activity ေပၚတင္ပါ၉

Activity.LoadLayout ("1")

Toogle button ရဲ႕ Looping_CheckedChange event sub ကုိေခၚပါ၉ Looping.Checked

ကို pass လုပ္ေပးလိုက္ပါ၉ Looping.Checked value ကို True လို႕ထားခဲ့တာေၾကာင့္

Looping.Checked = True ျဖစ္ေနပါမယ္၉ Looping.Checked = True ျဖစ္ရင္ audio file ကို

တစ္ၾကိမ္ play ျပီးတုိင္းထပ္မံ play ေနမွာျဖစ္ပါတယ္၉

237
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Looping_CheckedChange (Looping.Checked)

Activity resume လုပ္စဥ္မွာ autdio file ကုိ စတင္ play ပါမယ္၉ အဲဒီအခ်ိန္မွာ

အသံစတင္ၾကားရပါမယ္၉

MediaPlayer1.Play

Timer1 ကုိစတင္ရန္ enable လုပ္ေပးပါ၉

Timer1.Enabled = True

ျပီးရင္ t1_Tick event ကုိ ေခၚပါတယ္၉ t1 ရဲ႕ သတ္မွတ္ထားတဲ့ interval (ဥပမာ 1 second)

ျပည့္မွ ျဖစ္ေပၚရမယ့္ Tick event ကုိ အခ်ိန္မေစာင့္ဘဲ code ေရးျပီး event sub

ကိုေခၚလိုကတ
္ ာျဖစ္ပါတယ္၉

t1_Tick

Timer ကုိ အသံုးျပဳတဲ့ရည္ရြယ္ခ်က္က သတ္မွတ္ထားတဲ့အခ်ိန္ျပည့္တုိင္း (ဥပမာ 1 second

ၾကာတုိင္း) barPosition ရဲ႕ status ကိုလုိက္ေျပာင္းေပးဘုိ႕ျဖစ္ပါတယ္၉ (ပံု ၅-၀၂)

ပံု ၅-၀၂

t1_tick event ျဖစ္ေပၚတဲ့အခါ barPosition ရဲ႕ Value ကိုတြက္ရမယ္၉ အရင္ဆံုး

MediaPlayer1 ဟာ play ေနသလားစစ္ပါ၉

If MediaPlayer1.IsPlaying Then

Play ေနတာဆုိရင္ MediaPlayer1 ရဲ႕ လက္ရွိေရာက္ေနတဲ့ Positon (Position

238
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ဆိုေသာ္လည္းေနရာမဟုတ္ဘဲ အခ်ိန္သာျဖစ္ပါတယ္၉ ဥပမာ Audio ဖုိင္ကုိ play ေနတာ 5 seconds

ၾကာျပီဆုိရင္ MediaPlayer1.Position = 5000 ျဖစ္ပါတယ္) ကို Duration (audio တစ္ဖုိင္လံုးကို

play ရန္ၾကာခ်ိန)္ နဲ႕စားျပီး 100 နဲ႕ေျမွာက္ပါ၉ ရလာဒ္ကို barPosition ရဲ႕ Value

အျဖစ္သတ္မွတ္ေပးပါ၉

barPosition.Value = MediaPlayer1.Position /MediaPlayer1.Duration * 100

အဲဒီအခါမွာ barPosition ရဲ႕ slider ေလးကလည္း Position value ေပၚမူတည္ျပီး

လိုက္ေရြ႕သြားမွာျဖစ္ပါတယ္၉

လက္ရွိေရာက္ေနတဲ့ position နဲ႕ duration ကုိလည္း time format ေျပာင္းျပီး Label

မွာျပပါ၉

ပံု ၅-၀၃

lblPosition.Text = "Position: " & _

ConvertToTimeFormat (MediaPlayer1.Position) & _

" (" & ConvertToTimeFormat(MediaPlayer1.Duration) & ")"

Activity_Pause

Activity pause ျဖစ္တဲ့အခါ MediaPlayer1 ကိုလည္း pause လုပ္ေပးရမယ္၉ ဒါေၾကာင့္

MediaPlayer1 ဟာ play ေနသလား အရင္စစ္ပါ၉ (MediaPlayer1 ကုိ play ေနတဲ့ အခ်ိန္မွာသာ

pause လုပ္ႏုိင္မွာေၾကာင့္ပါ) Play ေနတာဆိုရင္ MediaPlayer1 ကုိ pause လုပ္ပါ၉ timer1 ကုိလည္း

disable လုပပ
္ ါ၉ ဒါေၾကာင့္ Activity pause လုပ္ထားစဥ္မွာ timer1 လည္း အလုပ္မလုပ္ေတာ့ပါဘူး၉

239
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub Activity_Pause (UserClosed As Boolean)

If MediaPlayer1.IsPlaying Then MediaPlayer1.Pause

timer1.Enabled = False

End Sub

barVolume_ValueChanged

အသံ အတိုးအက်ယ္လုပ္ခ်င္ရင္ barVolume မွာ လက္နဲ႕ဆြဲေရႊ႕ျပီး ေျပာင္းႏုိင္ပါတယ္၉

ပံု ၅-၀၄

barVolume ရဲ႕ Value ေျပာင္းရင္ barVolume_ValueChanged event ျဖစ္ေပၚပါတယ္၉

Parameter ၀ခုရွိပါတယ္၉ Value နဲ႕ UserChanged ပါ၉ Value မွာ အခုေျပာင္းလုိကတ


္ ဲ့ value

ေရာက္သြားပါမယ္၉ UserChanged ကေတာ့ True/False ျဖစ္ပါမယ္၉ barVolume_ValueChanged

event ျဖစ္ေပၚလာရင္ SetVolume method ကုိ ေခၚပါ၉ SetVolume method မွာ parameter

၀ခုယူပါတယ္၉ Left နဲ႕ Right volume ပါ၉ 0 နဲ႕ 1 ၾကားက value တစ္ခုေပးရမယ္၉ ဒီေတာ့

barVolume ရဲ႕ အခုေျပာင္းလုိက္တဲ့ value ကုိ 100 နဲ႕စားျပီး ရလာဒ္ကို Left volume အျဖစ္ေရာ၈

Right volume အျဖစ္ပါ သတ္မွတ္ေပးလိုက္ပါ၉

Sub barVolume_ValueChanged (Value As Int, UserChanged As Boolean)

MediaPlayer1.SetVolume (barVolume.Value / 100, barVolume.Value /100)

End Sub

barPosition_ValueChanged

barPosition ေျပာင္းလုိက္တုိင္း barPosition_ValueChanged event ျဖစ္ပါမယ္၉ ဒီ event

sub မွာ parameter ၀ခုရွိပါတယ္၉ Value နဲ႕ UserChanged ပါ၉ Value က barPosition

240
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ရဲ႕အခုေျပာင္းလုက
ိ ္တဲ့ value ျဖစ္ျပီး UserChanged ကေတာ့ True/False ျဖစ္ပါမယ္၉ UserChanged

= False ျဖစ္ရင္ေတာ့ ဘာမွမလုပ္ဘဲထြက္ပါ၉ UserChanged = True ဆိုရင္ေတာ့ MediaPlayer1 ရဲ႕

Position ကုိတြက္ေပးပါ၉

Sub barPosition_ValueChanged (Value As Int, UserChanged As Boolean)

If UserChanged = False Then Return

MediaPlayer1.Position = Value / 100 * MediaPlayer1.Duration

ဖုိင္ရဲ႕အဆံုးကိုေရာက္ေနစဥ္မွာ MediaPlayer1.IsPlaying = False ျဖစ္ေနပါမယ္၉

အဲဒီအခ်ိန္မ်ိဳးမွာ user က position change လိုက္ရင္ audio ကုိ play ပါ၉ ျပီးရင္ barPosition ကုိ

update လုပ္ဘုိ႕ t1_Tick ကုိေခၚပါ၉

If MediaPlayer1.IsPlaying = False Then

MediaPlayer1.Play

End If

t1_Tick

End Sub

Looping_CheckedChange

Toogle button ကုိ check/uncheck လုပ္ရင္ Looping_CheckedChange event

ျဖစ္ေပၚပါမယ္၉ Toogle button ကုိ check လုပ္ထားရင္ Checked = True ျဖစ္မယ္၉ Uncheck

လုပ္ထားရင္ Checked = False ျဖစ္ပါမယ္၉ Looping_CheckedChange event ျဖစ္ေပၚတဲ့အခါ

MediaPlayer1 ရဲ႕ Looping property ကုိ Checked ထဲက value (True/Fale) နဲ႕ set လုပ္ပါတယ္၉

Checked = True ဆုိရင္ Looping ကုိ True လုိ႕သတ္မွတ္တာျဖစ္တဲ့အတြက္ play

ျပီးသြားလည္းထပ္ play ေနဦးမွာျဖစ္ပါတယ္၉ Checked = False ျဖစ္ေနရင္ေတာ့ တစ္ၾကိမ္သာ play

မွာျဖစ္ပါတယ္၉

241
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub Looping_CheckedChange (Checked As Boolean)

MediaPlayer1.Looping = Checked

End Sub

ConvertToTimeFormat

Millisecond ကို m:ss format (မိနစ္၈ စကၠန္႕) သုိ႕ေျပာင္းေပးမယ့္ sub ျဖစ္ပါတယ္၉

Millisecond ကို second ေျပာင္းရန္ 1000 နဲဲ႕စားပါတယ္၉ Round method က ဒႆမ

ေနာက္ကတန္ဘိုးကိုျဖဳတ္ျပီး အနီးစပ္ဆံုးကိန္းျပည့္ကို return ျပန္ပါတယ္၉ ဥပမာ Round (5.5) = 6

ျဖစ္ပါတယ္၉ Round (5.4) = 5 ျဖစ္ပါတယ္၉

seconds = Round(ms / 1000)

Second ကို minute ဖြဲ႕ဘုိ႕ 60 နဲဲ႕စားပါတယ္၉ Floor method ကေတာ့ (ဥပမာ Floor (5.3)

ဆိုရင္ 5 ကိုဘဲ return ျပန္ပါတယ္)

minutes = Floor(seconds / 60)

Minute ဖြဲ႕ျပီးက်န္တဲ့ အၾကြင္းစကၠန္႕ကိုရရန္ seconds ကုိ 60 နဲ႕ Mod လုပ္ပါတယ္၉ Mod

ဆိုတာကေတာ့ အၾကြင္းကိုတြကတ
္ ာပါ၉ ဥပမာ 6 Mod 5 = 1 ျဖစ္ပါတယ္၉

seconds = seconds Mod 60

ဥပမာ ms = 150300 milliseconds ဆုိရင္

ms/1000 = 150.3 [second ဖြဲ႕ျခင္း]

Round (150.3) = 150 [အနီးဆံုးကိန္းျပည့္ယူျခင္း]

150/60 = 2.5 [minute ဖြဲ႕ျခင္း]

Floor (2.5) = 2 [အနည္းဆံုးကိန္းျပည့္ယူျခင္း]

150 Mod 60 = 30 [အၾကြင္း second တြက္ျခင္း]

Result ကေတာ့ 2 minutes 30 seconds ျဖစ္ပါတယ္၉

242
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
NumberFormat method မွာ parameter ၁ခုရွိပါတယ္၉ Number, MinimumIntegers နဲ႕

MaximumFractions တို႕ျဖစ္ပါတယ္၉ Number ကေတာ့ format ေျပာင္းခ်င္တဲ့ number

ျဖစ္ပါတယ္၉ MinimumIntegers က အနည္းဆံုးရွိရမယ့္ digit ပါ၉ MaximumFractions ကေတာ့

ဒသမဘယ္ႏွစ္ေနရာ ျဖတ္မယ္ဆိုတာ သတ္မွတ္ေပးဘို႕ပါ၉

NumberFormat (Number, MinimumIntegers, MaximumFractions)

ပံု ၅-၀၅

Minute ကို digit တစ္ခု၈ second ကုိ digit ၀ခုနဲ႕ျပေပးဘုိ႕အတြက္ ေအာက္ပါအတုိင္း

format လုပ္ေပးပါ၉

NumberFormat (minutes, 1, 0)

NumberFormat (seconds, 2, 0)

Complete Code

#Region Module Attributes


#FullScreen: False
#IncludeTitle: True
#ApplicationLabel: Media Player
#VersionCode: 1
#VersionName:
#SupportedOrientations: portrait
#End Region

Sub Process_Globals
Dim MediaPlayer1 As MediaPlayer
Dim timer1 As Timer
End Sub

243
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub Globals
Dim barPosition As SeekBar
Dim barVolume As SeekBar
Dim lblPosition As Label
Dim Looping As ToggleButton
End Sub

Sub Activity_Create (FirstTime As Boolean)


If FirstTime Then
MediaPlayer1.Initialize ( )
MediaPlayer1.Load (File.DirAssets, "TuPoh.mp3")
timer1.Initialize ("t1", 1000)
End If
Activity.LoadLayout ("1")
Looping_CheckedChange (Looping.Checked)
End Sub

Sub Activity_Resume
MediaPlayer1.Play
timer1.Enabled = True
t1_Tick
End Sub

Sub Activity_Pause (UserClosed As Boolean)


If MediaPlayer1.IsPlaying Then MediaPlayer1.Pause
timer1.Enabled = False
End Sub

Sub t1_Tick
If MediaPlayer1.IsPlaying Then
barPosition.Value = MediaPlayer1.Position / MediaPlayer1.Duration * 100

244
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
lblPosition.Text = "Position: " & _
ConvertToTimeFormat (MediaPlayer1.Position) & _
" (" & ConvertToTimeFormat(MediaPlayer1.Duration) & ")"
End If
End Sub

Sub ConvertToTimeFormat (ms As Int) As String


Dim seconds, minutes As Int
seconds = Round(ms / 1000)
minutes = Floor(seconds / 60)
seconds = seconds Mod 60
Return NumberFormat(minutes, 1, 0) & ":" & NumberFormat(seconds, 2, 0)
End Sub

Sub barVolume_ValueChanged (Value As Int, UserChanged As Boolean)


MediaPlayer1.SetVolume (barVolume.Value / 100, barVolume.Value / 100)
End Sub

Sub barPosition_ValueChanged (Value As Int, UserChanged As Boolean)


If UserChanged = False Then Return
MediaPlayer1.Position = Value / 100 * MediaPlayer1.Duration
If MediaPlayer1.IsPlaying = False Then
MediaPlayer1.Play
End If
t1_Tick
End Sub

Sub Looping_CheckedChange (Checked As Boolean)


MediaPlayer1.Looping = Checked
End Sub

245
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-၀၆

WebView
Html page ေတြကိုျပႏုိင္တဲ့ view ျဖစ္ပါတယ္၉

Example 7.10

Application မွာလႈပ္ရွားေနတဲ့ animated image ေလးတစ္ခုထည့္ၾကည့္ရေအာင္၉ Project

တစ္ခု create လုပ္ပါ၉ Designer မွာ WebView တစ္ခုထည့္ပါ၉ Layout ကို Layout1 အမည္နဲ႕ save

ပါ၉ Notepad မွာေအာက္ပါ html code ကိုရိုက္ျပီး web.html အမည္နဲ႕ project folder ရဲ႕ Files

folder ေအာက္မွာထားပါ၉

<html>

<body>

<img src="dogrun.gif" />

</body>

</html>

dogrun.gif file ကုိလည္း project folder ရဲ႕ Files folder ေအာက္မွာထားပါ၉

IDE code editor မွာ ေအာက္ပါအတိုင္း ေရးျပီး run ပါ၉ ေခြးကေလးတစ္ေကာင္ ေျပးေနတာ

ေတြ႕ရမယ္၉ (ပံု ၅-၀၇)

246
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub Globals

Dim WebView1 As WebView

End Sub

Sub Activity_Create (FirstTime As Boolean)

Activity.LoadLayout ("Layout1")

WebView1.Width = 100%x

WebView1.Height = 100%y

WebView1.LoadURL ("file:///android_asset/web.html")

End

ပံု ၅-၀၇

247
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
7.1.4. Tools Menu

ပံု ၅-၁ှ

Generate Members: View ေတြကို declare လုပ္ရန္နဲ႕ event sub frame မ်ား create လုပ္ရန္၉

Generate Members dialog မွာ current project မွာထည့္ထားတဲ့ view

အားလံုးကိုျမင္ရမယ္၉ (ပံု ၅-၁ဿ)

Code မွာသံုးမယ့္ view ေတြကို check လုပ္ပါ၉ Event sub frame လိုအပ္တဲ့ view (ဥပမာ

btnAction) အတြက္ လုိခ်င္တဲ့ event (ဥပမာ Click) ကို check လုပ္ပါ၉ ျပီးရင္

ကို click ျပီး ကိုဆက္ click ပါ၉

Check လုပ္လုိက္တဲ့ view ေတြအတြက္ reference ကုိ code editor ရဲ႕ Globals sub မွာ generate

လုပ္ေပးပါလိမ့္မယ္၉ ဥပမာ

Dim btnAction, btn0 As Button

Dim lblResult As Label

Dim lblComments As Label

248
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Dim lblMathSign As Label

Dim lblNumber1 As Label

Dim lblNumber2 As Label

View တစ္ခုရဲ႕ event ကို check လုပ္ရင္ အဲဒီ event အတြက္ event sub frame ကို

generate လုပ္ေပးပါတယ္၉ ဥပမာ

Sub btnAction_Click

End Sub

ပံု ၅-၁ဿ

249
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Bring To Front & Send To Back: View ၀ခုထပ္ေနတဲ့အခါ view တစ္ခုကို select လုပ္ျပီး

Bring to Front ကုိ click ပါ၉ Selected view ဟာ မ်က္ႏွာစာကို ေရာက္လာပါမယ္၉ Send To Back

ဆိုရင္ ေနာက္ဘက္ ေရာက္သြားပါမယ္၉

ပံု ၅-၁၀

Remove Selected Views: ဖ်က္ခ်င္တဲ့ view/views ကို select လုပ္ျပီး Remove Selected Views

ကုိ click ျပီးဖ်က္ႏုိင္ပါတယ္၉

Duplicate Selected Views: မူလ view/views ကုိ select လုပ္ျပီး Duplicate Selected Views ကုိ

click ရင္ ေနာက္ထပ္ copy တစ္ခုရပါတယ္၉

ပံု ၅-၁၁

Change Grid: Grid size ကုိ change ရန္၉

ပံု ၅-၁၂

250
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Connect to Device/Emulator: Designer ကုိ device/emulator နဲ႕ခ်ိတ္ဆက္ရန္၉ PC မွာ device

ေရာ emulator ပါခ်ိတ္ထားရင္ေတာ့ (ပံု ၅-၁၃) မွာျပထားတဲ့ dialog box ေပၚလာမယ္၉

ခ်ိတ္ဆက္ခ်င္တဲ့ တစ္ခုကိုေရြးေပးပါ၉

ပံု ၅-၁၃

Disconect From Device/Emulator: Designer ကုိ device/emulator နဲ႕ခ်ိတ္ထားရာမွျပန္ျဖဳတ္ရန္၉

Show Abstract Desinger: Abstract Desinger ကို screen မွာမျမင္ရပါကႏွိပ္ရန္၉

Run Script: Script code ကုိ run ရန္၉

Send To UI Cloud: UI Cloud သုိ႕ပို႕ရန္၉

7.2. General Setting

IDE နဲ႕ device/emulator ကုိခ်ိတ္ရန္ Chain icon ကုိ double click လုပ္ပါ၉ Status မွာ

Connected ျဖစ္သြားရင္ခ်ိတ္မိပါျပီ၉ ျပန္ျဖဳတ္ခ်င္ရင္လည္း Chain icon ကို double click ပါ၉

(ပံု ၅-၁၄)

251
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-၁၄

Main tab မွာ Designer state ကိုျပပါတယ္၉ View ေတြကိုထည့္ျခင္း၈ ဖ်က္ျခင္း၈ property

ျပင္ျခင္းကို Main tab မွာလုပ္ရမယ္၉ Designer ရဲ႕ ဘယ္ဘက္ျခမ္းတြင္ layout ထဲမွာရွိတဲ့ view

အားလံုးကိုျပပါတယ္၉ (ပံု ၅-၁၅)

ပံု ၅-၁၅

252
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
အလယ္မွာေတာ့ selected view ရဲ႕ property ကိုျပထားျပီး လိသ
ု လိုျပင္ႏုိင္ပါတယ္၉

(ပံု ၅-၁၆)

ပံု ၅-၁၆

ညာဘက္ကေတာ့ project ထဲကို image file ေတြထည့္ရန္၈ layout variant မ်ား

ထပ္ထည့္ရန္ျဖစ္ပါတယ္၉

ပံု ၅-၁၇

253
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
7.3. Properties List

View တစ္ခုကို select လုပ္ထားရင္ view ရဲ႕ property ေတြကို properties list

မွာျပေနမယ္၉ ဒီေနရာကေန view ရဲ႕ property ေတြကို လိုသလိုျပင္လို႕ရပါတယ္၉ Property ေတြကို

group ေတြခြဲထားပါတယ္၉ (ပံု ၅-၂ှ)

 Main

 Common properties

 View properties (ဒီ properties ကေတာ့ select လုပ္ထားတဲ့ view ေပၚမူတည္ပါတယ္)

ပံု ၅-၂ှ

Main

Name: View name ပါ၉ ၾကိဳက္ရာအမည္ ေပးႏုိ္င္ပါတယ္၉ သို႕ေသာ္ အမည္ေပးတဲ့အခါ view type

ကိုေဖာ္ျပတဲ့ character ၁လံုးကိုေရွ႕ဆံုးမွာထားျပီး ရည္ရြယ္ခ်က္ကိုေဖာ္ျပတဲ့ character

ေတြကိုေနာက္မွာတြဲေပးျခင္းျဖင့္ အလြယ္တကူမွတ္မိႏိုင္ေစပါတယ္၉ ဥပမာ Button view တစ္ခုကို

254
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
အမည္ေပးမယ္ဆုိရင္ btn ဆိုတဲ့ character ၁လံုးကိုေရွ႕မွာထားမယ္၉ Ok button အျဖစ္သံုးမွာဆိုရင္

အမည္ကို btnOk လို႕ေပးပါ၉ Cancel button ဆိုရင္ btnCancel လို႕ေပးပါ၉ Title ကုိေဖာ္ျပမယ့္

Label view ဆိုရင္ lblTitle လို႕အမည္ေပးျခင္းကပိုျပီးသင့္ေလ်ာ္ပါတယ္၉

Type: View ရဲ႕ type ကိုေဖာ္ျပပါတယ္၉ ဥပမာ view ဟာ Button လား၈ Label လားအစရွိသည္ျဖင့္

ေဖာ္ျပေပးပါတယ္၉ View type ကုိေတာ့ျပင္လို႕မရပါ၉

Event Name: View ရဲ႕ event name ျဖစ္ပါတယ္၉ Default အေနနဲ႕ event name ဟာ view name

နဲ႕တူတူဘဲ ျဖစ္ေနမွာပါ၉ ဒါေပမယ့္ event name ကို ကိုယလ


္ ိုသလို ေျပာင္းလို႕ရပါတယ္၉ View ၀ခု

သို႕မဟုတ္ ၀ခုထက္ပိုတဲ့ view ေတြရဲ႕ event name ကို တစ္ခုထဲ ေပးရတဲ့အခါမ်ိဳးလည္း

ရွိတတ္ပါတယ္၉ ဥပမာ Button1, Button2, Button3 ဆိုတဲ့ Button view ၁ခုလံုးရဲ႕ event name

ကုိ Button လို႕ေပးတာမ်ဳိးေပ့ါ၉

Parent: Parent view ရဲ႕ name ကိုေပးရမယ္၉ View ကုိဘယ္ container ေပၚမွာထားမလည္း၉

ဥပမာ Button1 ကုိ Activity ေပၚမွာထားမယ္ဆိုရင္ Button1 ရဲ႕ parent ဟာ Activity ပါ၉ Button1

ကုိ Panel1 ေပၚမွာထားခ်င္ရင္ Button1 ရဲ႕ parent ကုိ Panel1 လို႕ေပးပါ၉

Common properties

HorizontalAnchor: LEFT/RIGHT/BOTH တစ္ခုခုေရြးပါ၉

VerticalAnchor: TOP/BOTTOM/BOTH တစ္ခုခုေရြးပါ၉ (ပံု ၅-၂ဿ)

ပံု ၅-၂ဿ

255
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Left: Parent ရဲ႕ left နဲ႕ view ရဲ႕ left ၾကားအကြာအေှး၉ Unit က dip ပါ၉

Top: Parent ရဲ႕ Top နဲ႕ view ရဲ႕ Top ၾကားအကြာအေှး၉

Width: View ရဲ႕အက်ယ္၉

Height: View ရဲ႕အျမင့္၉

ပံု ၅-၂၀

Enabled: View ကုိ enable ျဖစ္ေစရန္ True ကိုေရြးပါ၉ Disable လုပ္ရန္ False ကိုေရြးပါ၉

Visible: View ကုိ ျမင္ရေအာင္ျပခ်င္ရင္ True ကိုေရြးပါ၉ Hide လုပ္ထားခ်င္ရင္ False ကိုေရြးပါ၉

Tag: View နဲ႕သက္ဆိုင္တဲ့ data တစ္ခုခုထည့္လိုက ထည့္ရန္၉

7.4. Image File

Project ထဲကို image file ေတြထည့္ရန္ AddImages button ကုိ click ပါ၉ ျပန္ဖ်က္လိုပါက

ဖ်က္လိုေသာဖုိင္ကို ေရြးျပီး Remove selected button ကုိ click ပါ၉ (ပံု ၅-၂၁)

256
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-၂၁

7.5. Layout Variant

Layout file တစ္ခုမွာ layout variant အမ်ိဳးမ်ိဳး ရွိႏုိ္င္ပါတယ္၉ Application ကုိ run တဲ့

device ေပၚမူတည္ျပီး သင့္ေတာ္တဲ့ layout variant ကို အလိုလုိ ေရြးခ်ယ္ display

လုပ္ျပမွာျဖစ္ပါတယ္၉

Example 7.11

Application ကုိ portrait mode မွာ run သည္ျဖစ္ေစ၈ landscape mode မွာ run

သည္ျဖစ္ေစ ၾကည့္ေကာင္းတဲ့ ပုံစံျဖစ္ေအာင္ လုပ္ပါမယ္၉ (ပံု ၅-၂၂) မွာျပထားတဲ့အတိုင္း layout

တစ္ခုဖန္တီးပါ၉ ဒါက portrait mode အတြက္ပါ၉

257
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-၂၂

Landscape variant တစ္ခုထပ္ထည့္မယ္၉ (ပံု ၅-၂၃) အတုိင္းလုပ္ပါ၉

ပံု ၅-၂၃

258
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Designer မွာ (ပံု ၅-၂၄) မွာျပထားတဲ့အတုိင္းေတြ႕ရမယ္၉

ပံု ၅-၂၄

အခု layout ကုိ variant အသစ္တစ္ခအ


ု ျဖစ္ထည့္ရန္ (ပံု ၅-၂၅) မွာျပထားတဲ့အတုိင္း

[Tools > Add Current Layout As New Variant] ကိုေရြးပါ၉

ပံု ၅-၂၅

Form ကုိ resize ျပန္လုပ္ရန္ [Tools > Resize Form To Fit Layout] ကိုေရြးပါ၉ ျပီးရင္

(ပံု ၅-၂၆) မွာျပထားတဲ့အတုိင္း view ေတြကို landscape mode အတြက္ အဆင္ေျပေအာင္

ေနရာျပန္ခ်ပါ၉

259
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-၂၆

Designer မွာလည္း variant အသစ္တစ္ခု တုိးလာတာကို (ပံု ၅-၂၇) မွာျပထားတဲ့အတုိင္း

ျမင္ရပါမယ္၉

ပံု ၅-၂၇

Program ကုိ run ျပီး device ကုိ portrait ေရာ landscape မွာပါ ထားၾကည့္ပါ၉ Portrait

မွာထားရင္ 320 x 480 layout variant မွာ design လုပ္ထားတဲ့ layout ကုိျမင္ရျပီး landscape

မွာထားရင္ 480 x 320 layout variant မွာ design လုပ္ထားတဲ့ layout ကုိျမင္ရမွာျဖစ္ပါတယ္၉

260
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Example 7.12

TestLayout.b4a file ကို ဖြင့္ပါ၉ Abstract Designer မွာ (ပံု ၅-၃ှ) မွာျပထားတဲ့အတိုင္း

ျမင္ရမယ္၉

ပံု ၅-၃ှ

ညာဘက္ေဘးမွာ ပိုေနတာေတြကို ေဖ်ာက္ခ်င္ရင္ (ပံု ၅-၃ဿ) မွာျပထားတဲ့အတုိင္း Abstract

Designer window မွာ [Tools > Resize Form to Fit Layout] ကိုေရြးပါ၉ (ပံု ၅-၃၀)

အတိုင္းျဖစ္သြားမယ္၉

261
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-၃ဿ ပံု ၅-၃၀

မူလထဲက 320 x 480, scale = 1, (160dpi) phone အမ်ိဳးအစားအတြက္ layout variant

တစ္ခုရွိျပီးျဖစ္ပါတယ္၉ အျခား device ေတြအတြက္ပါအဆင္ေျပေစရန္ layout variant

တစ္ခထ
ု က္မက ထပ္ထည့္ႏုိ္င္ပါတယ္၉ Layout variant အသစ္ထပ္ထည့္ရန္ New Variant button

ကုိ click ပါ၉ (ပံု ၅-၃၁)

ပံု ၅-၃၁

262
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-၃၂

(ပံု ၅-၃၂) မွာျပထားတဲ့အတုိင္းေရြးျပီး Ok button ကုိ click ပါ၉ (Landscape mode အတြက္

layout variant တစ္ခုထပ္ထည့္ဘုိ႕ပါ)

ပံု ၅-၃၃

(ပံု ၅-၃၃) မွာျပထားတဲ့အတုိင္း layout variant တစ္ခု ထပ္ထည့္လိုက္ပါျပီ၉ ToolBox

ကိုျမင္ရမွာ မဟုတ္ေတာ့ဘူးေနာ္၉

Designer မွာ pnlToolBox ကိုေရြးပါ၉ (ပံု ၅-၃၄)

263
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-၃၄

„Top‟ property ကုိ 10 လို႕ေျပာင္းလိုက္ပါ၉ (ပံု ၅-၃၅)

ပံု ၅-၃၅

ဒါဆိုရင္ btnToolBox ကုိျမင္ရပါျပီ၉ ဒါေပမယ့္ ညာဘက္ေဘးမွာ ေနရာလြတ္

ျဖစ္ေနေသးတယ္၉ လြတ္တဲ့ေနရာမွာ btnToolBox ကို (ပံု ၅-၃၆) အတုိင္းျဖစ္ေအာင္ေရႊ႕လုိက္ပါ၉

264
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-၃၆

ျပီးရင္ program ကုိ 320 x 480 resolution ရွိတဲ့ device/emulator မွာ run ပါ၉ Portrait

mode မွာ (ပံု ၅-၃၇) အတုိင္းျဖစ္မယ္၉

ပံု ၅-၃၇

265
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Landscape mode မွာ (ပံု ၅-၄ှ) အတုိင္းျဖစ္မယ္၉

ပံု ၅-၄ှ

ေနာက္တစ္ခါ ဒီ့ထက္ size ၾကီးတဲ့ device တစ္ခုမွာ run ၾကည့္ပါ၉ (ပံု ၅-၄ဿ) နဲ႕ (ပံု ၅-၄၀)

မွာျမင္ရတဲ့ အတုိင္း device ရဲ႕ size ၾကီးလာတာနဲ႕အမွ် ေနရာလြတ္ေတြရွိလာမယ္၉

ပံု ၅-၄ဿ

266
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-၄၀

ဒါေၾကာင့္ code editor မွာေအာက္ပါအတုိင္း လုိအပ္တဲ့ code ေတြကိုအစားထိုးေရးေပးပါ၉

Activity.Height > Activity.Widht ျဖစ္ရင္ (portrait mode ျဖစ္ရင္) line 25 နဲ႕ 26 ကို

execute လုပ္မယ္၉ မဟုတ္ရင္ (landscape mode ဆိုရင္) ေတာ့ line 27 to 30 ကို execute

လုပ္မယ္၉ ျပီးရင္ program ကုိ run ပါ၉ (ပံု ၅-၄၁) နဲ႕ (ပံု ၅-၄၂) မွာျပထားတဲ့အတုိင္း

ေနရာလြတ္မရွိေတာ့ဘဲ layout ဟာ screen မွာ fit ျဖစ္သြားတာ ေတြ႕ရမယ္၉

267
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-၄၁

ပံု ၅-၄၂

268
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
7.6. Abstract Designer

Abstract Designer ဆိုတာ Designer ရဲ႕ အစိတ္အပို္င္းတစ္ခု ျဖစ္ျပီး layout ေတြကို

design လုပ္ႏုိင္တဲ့ေနရာတစ္ခုျဖစ္ပါတယ္၉ Emulator မွာ design လုပ္တာထက္ Abstract Designer

မွာ design လုပ္ရတာပိုျမန္ပါတယ္၉ View တစ္ခုကို click လုိက္ရင္ သူ႕ရဲ႕ property ေတြကို

Desinger မွာျပပါတယ္၉ Emulator/device မွာ design လုပ္ရင္ run တဲ့အခါ ျမင္ရမယ့္အတုိင္း

အတိအက်ျပပါတယ္၉ (ပံု ၅-၄၃) မွာၾကည့္ပါ၉ Abstract Designer မွာ design လုပ္ရင္ေတာ့

(ပံု ၅-၄၄) မွာ ျပထားတဲ့အတိုင္း အၾကမ္းေလာက္ဘဲ ျပပါတယ္၉

ပံု ၅-၄၄
ပံု ၅-၄၃

269
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
7.6.1. Menu

Layout

(ပံု ၅-၄၅) မွာျပထားတဲ့ layout မ်ားထဲမွ အလိုရွိရာ layout ကုိေရြးခ်ယ္အသံုးျပဳႏုိင္ပါတယ္၉

ပံု ၅-၄၅

270
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Zoom Tools

ပံု ၅-၄၆ ပံု ၅-၄၇

7.6.2. Context Menu

View တစ္ခုကုိ right click လုပ္ရင္ (ပံု ၅-၅ှ) မွာျပထားတဲ့အတိုင္း context menu

ေပၚလာမယ္၉

ပံု ၅-၅ှ

Generate ကိုေရြးရင္ (ပံု ၅-၅ဿ) မွာျပထားတဲ့အတိုင္း ေနာက္ထပ္ေရြးစရာ menu item

ေတြထပ္ေပၚလာမယ္၉

271
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-၅ဿ

„Dim btnTest1 As Button‟ ကုိေရြးရင္ code editor မွာ btnTest1 ကို declare

လုပ္ျပီးျဖစ္မယ္၉

Sub Globals

Dim btnTest1 As Button

End Sub

272
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Down ကုိေရြးရင္ Down event အတြက္ event sub frame ရမယ္၉

Sub btnTest1_Down

End Sub

Up ကုိေရြးရင္ Up event အတြက္ event sub frame ရမယ္၉

Sub btnTest1_Up

End Sub

Click ကုိေရြးရင္ Click event အတြက္ event sub frame ရမယ္၉

Sub btnTest1_Click

End Sub

LongClick ကုိေရြးရင္ LongClick event အတြက္ event sub frame ရမယ္၉

Sub btnTest1_LongClick

End Sub

7.6.3. Select View

View ကုိ select လုပ္ရန္ mouse နဲ႕ click ပါ၉ တစ္ခုထက္ပိုျပီး select လုပ္ခ်င္ရင္ ပထမဆံုး

view ကုိ select လုပ္ျပီးေနာက္ Ctrl key ကုိႏွိပ္ထားျပီး (မလႊတ္ဘ)ဲ ေနာက္ထပ္ view တစ္ခုခ်င္းကို

mouse နဲ႕ click သြားပါ၉ Select လုပ္ျပီးေနာက္ view ေပၚမွာ right click လုပ္ရင္ (ပံု ၅-၅၀)

မွာျပထားတဲ့အတုိင္း context menu တစ္ခုေပၚလာမယ္၉ လိုအပ္တာေရြးသံုးပါ၉

273
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-၅၀

View ေတြရဲ႕ property ကို စုေပါင္းသတ္မွတ္လိုပါက view ေတြကို select လုပ္ျပီး

(ပံု ၅-၅၁) မွာျပထားတဲ့အတုိင္း သတ္မွတ္ႏုိင္ပါတယ္၉ ဥပမာ Button1, Button2, Button3 တုိ႕ရဲ႕

event name ကုိ Button လုိ႕ေပးခ်င္ရင္ Button ၁ခုလံုးကို select လုပ္ျပီးေပးႏုိင္ပါတယ္၉

ပံု ၅-၅၁

7.7. Adding Views by Code

Designer ကုိမသံုးဘဲ code ေရးျပီးလည္း view ေတြကို ထည့္ႏုိင္ပါတယ္၉ View ေတြရဲ႕

position နဲ႕ dimension အတြက္ သံုးႏုိင္တဲ့ unit ၀မ်ိဳးရွိပါတယ္၉

 dip (density independent pixels)

 %x, %y

ဥပမာ

View ရဲ႕ width ကုိ 50%x လို႕သတ္မွတ္ရင္ Activity width ရဲ႕ တစ္ှက္ျဖစ္ပါတယ္၉

View ရဲ႕ height ကုိ 25%y လို႕သတ္မွတ္ရင္ Acitivity height ရဲ႕ 25% ျဖစ္ပါတယ္၉

Activity width နဲ႕ height ဘယ္ေလာက္ရွိလည္းဆုိတာကေတာ့ application ကုိ run ေနတဲ့

device ေပၚမူတည္ပါတယ္၉

274
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Example 7.13

(ပံု ၅-၅၂) မွာျပထားတဲ့အတုိင္း layout တစ္ခုဖန္တီးၾကည့္ပါ၉ lblTitle နဲ႕ pnlTest ကို

Activity ေပၚမွာထားမယ္၉ lblPanelTitle နဲ႕ btnTest ကို pnlTest Panel ေပၚမွာထားပါ၉

lblTitle

lblPanelTitle

btnTest

pnlTest

ပံု ၅-၅၂

View ေတြကို Sub Globals မွာ declare လုပ္ပါ၉

Sub Globals

Dim lblTitle, lblPanelTitle As Label

275
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Dim pnlTest As Panel

Dim btnTest As Button

End Sub

Sub Activity_Create မွာ ေအာက္ပါအတုိင္းေရးပါ၉

Sub Activity_Create(FirstTime As Boolean)

lblTitle.Initialize ("") lblTitle ကုိ initilze လုပ္တယ္၉

lblTitle.Color = Colors.Red Background color ကို အနီေရာင္

သတ္မွတ္တယ္၉

lblTitle.TextSize = 20 Text size ကုိ 20 ထားတယ္၉

lblTitle.TextColor = Colors.Blue Text color ကုိအျပာေရာင္ သတ္မွတ္တယ္၉

Text ကို Label မွာဘယ္လို alignment နဲ႕ေပၚရမယ္ဆိုတာ သတ္မွတ္တယ္၉

lblTitle.Gravity = Gravity.CENTER_HORIZONTAL + Gravity.CENTER_VERTICAL

lblTitle.Text = "Title" Label မွာျမင္ရမယ့္ text ကုိ Title

လို႕သတ္မွတ္တယ္၉

Activity.AddView (lblTitle, 20%x, 10dip, 60%x, 30dip) lblTitle ကုိ Activity

ထဲထည့္တယ္၉

pnlTest.Initialize ("") pnlTest ကုိ initilze လုပ္တယ္၉

pnlTest.Color = Colors.Blue Background color ကို အျပာေရာင္

သတ္မွတ္တယ္၉

btnTest.Initialize ("btnTest") btnTest ကုိ initilze လုပ္တယ္၉ Event name ကို

btnTest လို႕ေပးတယ္၉

276
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
btnTest.Text = "Test" Button မွာျမင္ရမယ့္ text ကုိ Test

လို႕သတ္မွတ္တယ္၉

lblPanelTitle.Initialize ("") lblPanelTitle ကုိ initilze လုပ္တယ္၉

lblPanelTitle.Color = Colors.Red Background colorကို အနီေရာင္သတ္မွတ္တယ္၉

lblPanelTitle.TextSize = 16 Text size ကုိ 16 ထားတယ္၉

lblPanelTitle.TextColor = Colors.Blue Text clor ကုိအျပာထားတယ္၉

Text ကို Lable မွာဘယ္လို alignment နဲ႕ေပၚရမယ္ဆိုတာ သတ္မွတ္တယ္၉

lblPanelTitle.Gravity = Gravity.CENTER_HORIZONTAL +

Gravity.CENTER_VERTICAL

lblPanelTitle မွာျမင္ရမယ့္ text ကုိ Panel test လို႕သတ္မွတ္တယ္၉

lblPanelTitle.Text = "Panel test"

pnlTest ကုိ Activity ထဲထည့္တယ္၉

Activity.AddView (pnlTest,0, lblTitle.Top+lblTitle.Height+10dip,100%x, 50%y)

lblPanelTitle ကုိ pnlTest ထဲထည့္တယ္၉

pnlTest.AddView (lblPanelTitle, 20dip, 10dip, 100dip, 30dip)

btnTest ကုိ pnlTest ထဲထည့္တယ္၉

pnlTest.AddView (btnTest, 50dip, 50dip, 100dip, 60dip)

End Sub

AddView method မွာ parameter 5 ခုယူပါတယ္၉ View, Left, Top, Widht, Height

ျဖစ္ပါတယ္၉ Left, Top က view ရဲ႕ position ျဖစ္ျပီး width, Height ကေတာ့ view ရဲ႕ dimension

ျဖစ္ပါတယ္၉

277
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
7.8. Designer Scripts

ေစ်းကြက္မွာထြက္ေပၚေနတဲ့ Android device ေတြဟာ size အမ်ိဳးမ်ိဳး၈ resolution

အမ်ိဳးမ်ိဳးျဖစ္ပါတယ္၉ Android developer ေတြ ရင္ဆုိင္ရေလ့ရွိတဲ့ ျပသနာတစ္ခုကေတာ့ မိမိတုိ႕

application ရဲ႕ user interface ကို အမ်ိဳးမ်ိဳးေသာ device ေတြမွာအဆင္ေျပေအာင္

ၾကိဳးစားရျခင္းပါဘဲ၉ အထက္မွာေရးခဲ့သလို layout variant အမ်ိဳးမ်ိဳးျပဳလုပ္ႏုိင္ေပမယ့္

သိပ္အဆင္ေျပလွတဲ့ solution ေတာ့မဟုတ္ေသးပါဘူး၉ Screen size နဲ႕ resolution အမ်ိဳးမ်ိဳးမွာ

အဆင္ေျပေအာင္လုပ္ႏုိင္မယ့္ နည္းလမ္း ေနာက္တစ္ခု ကေတာ့ Designer Scripts

ကုိအသံုးျပဳျခင္းပါဘဲ၉

Layout file တုိင္းမွာ script code ပါပါတယ္၉ Script ကို Designer window ရဲ႕ Designer

Scripts tab ကေနၾကည့္ႏုိင္ပါတယ္၉ (ပံု ၅-၅၃)

ပံု ၅-၅၃

278
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Script ၀မ်ိဳးရွိပါတယ္၉ Variant အားလံုးအတြက္သံုးမယ့္ General script နဲ႕ current variant

အတြက္သာသံုးမယ့္ Specific script ရယ္လုိ႕ျဖစ္ပါတယ္၉ F5 ကိုႏွိပ္ျပီး run လိုက္တာနဲ႕ script code

ကုိ execute လုပ္ျပီး ခ်ိတ္ဆက္ထားတဲ့ device/emulator မွာ updated layout ကုိျပမွာပါ၉

Properties:

 Left / Right / Top / Bottom / HorizontalCenter / VerticalCenter

 Width / Height

 TextSize (TextSize ကုိ dip unit မသံုးရပါဘူး)

 Text

 Image

 Visible

Methods

 SetLeftAndRight (Left, Right)

 SetTopAndBottom (Top, Bottom)

Keywords

 Min / Max

 AutoScale

 AutoScaleAll

 AutoScaleRate

 ActivitySize

 If .. Then

279
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Example 7.14

Designer script သံုးျပီး device အသီးသီးအတြက္ အဆင္ေျပမယ့္ layout

ျဖစ္ေအာင္လုပ္ပါမယ္၉ Abstract Designer မွာ view ေတြကို (ပံု ၅-၅၄) မွာျပထားတဲ့အတုိင္း

ေနရာခ်ပါ၉ Position နဲ႕ size အတိအက်ျဖစ္စရာ မလုိပါ၉ Script code နဲ႕လုပ္ေပးမွာမုိ႕ပါ၉

ပံု ၅-၅၄

btnLeft နဲ႕ btnRight ကိုအေပၚပိုင္းမွာ ထားမယ္၉ btnDown ကို bottom မွာထားျပီး သူ႕ရဲ႕

width က Activity အျပည့္ေနရာယူမယ္၉ ListView1 ကလြတ္တဲ့ေနရာ အကုန္ယူမယ္၉

ToggleButton1 ကို Activity ရဲ႕အလယ္တည့္တည့္မွာ ထားမယ္၉ Layout file name ကုိ main

280
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
လုိ႕ေပးျပီး Activity_Create မွာ Activity.LoadLayout(“main”) လုိ႕ေရးပါ၉ Designer window မွာ

Desiger Scripts tab ကုိေရြးျပီးေအာက္ပါ code ေတြေရးပါ၉

btnRight.Right = 100%x btnRight ကုိညာဘက္အစြန္ဆံုးမွာထားပါတယ္၉

btnDown.Bottom = 100%y btnDown ကုိေအာက္ဆံုးမွာထားပါတယ္၉

btnDown.Width = 100%x btnDown ရဲ႕ width ကုိ Activity width

အျပည့္ေနရာယူပါတယ္၉

EditText1.Width = 100%x EditText1 ရဲ႕ width ကုိ Activity width

အျပည့္ေနရာယူပါတယ္၉

EditText1 ရဲ႕ bottom ကုိ btnDown ရဲ႕အေပၚ 5dip အကြာမွာထားပါတယ္၉

EditText1.Bottom = btnDown.Top - 5dip

ListView1.Width = 100%x ListView1 ရဲ႕ width ကုိ Activity width

အျပည့္ေနရာယူပါတယ္၉

ListView1 ဟာ btnLeft ရဲ႕ bottom နဲ႕ EditText1 ရဲ႕ top

ၾကားလြတ္တဲ့ေနရာအားလံုးယူလိုက္ပါတယ္၉

ListView1.SetTopAndBottom (btnLeft.Bottom, EditText1.Top)

ToggleButto1 ကို Activity ရဲ႕ အလ်ားလိုက္ center မွာထားပါတယ္၉

ToggleButton1.HorizontalCenter = 50%x

ToogleButton1 ကို Activity ရဲ႕ ေထာင္လုိက္ center မွာထားပါတယ္၉

ToggleButton1.VerticalCenter = 50%y

281
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ျပီးရင္ F5 ကိုႏွိပ္ျပီး script code ေတြကို run လိုက္ပါ၉ (ပံု ၅-၅၅) မွာျပထာတဲအ
့ တုိင္း

ေနရာခ်ေပးသြားပါလိမမ
့္ ယ္၉

ပံု ၅-၅၅

ျပီးရင္ program ကုိ run ပါ၉ (ပံု ၅-၅၆) မွာျပထားတဲ့အတုိင္း ျမင္ရမယ္၉ အျခား device

အသီးသီးမွာလည္း run ၾကည့္ပါ၉ Device မရွိရင္ resolution အမ်ိဳးမ်ိဳးနဲ႕ျပဳလုပ္ထားတဲ့ emulator

ေတြသံုးျပီး စမ္းႏုိင္ပါတယ္၉

282
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-၅၆

7.9. Anchor

Application ကုိ ဘယ္ device မွာ run သည္ျဖစ္ေစ view ကို Activity ရဲ႕ (Left, Right,

Both) (Top, Bottom, Both) တစ္ခုခုမွာအျမဲရွိေနေစခ်င္ရင္ Anchor ကုိသံုးရမယ္၉ Anchor

property ၀ခုရွိပါတယ္၉

 Horizontal Anchor

 Vertical Anchor

283
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Horizontal Anchor

ပံု ၅-၅၇

ေရြးႏုိင္တဲ့ value ၁မ်ိဳးရွိပါတယ္၉

 LEFT

o View ရဲ႕ left edge ကို parent view (ဥပမာ Activity) ရဲ႕ left edge နဲ႕ anchor

လုပ္ေပးပါတယ္၉ Edge ၀ခုအကြာအေှးကေတာ့ „Left‟ property

မွာေပးထားတဲ့အတုိင္းျဖစ္ပါမယ္၉

ဥပမာ

View

Activity ပံု ၅-၆ှ

View ရဲ႕ left edge

Left = 50

Parent view ရဲ႕ left edge

 RIGHT

o View ရဲ႕ right edge ကို parent view ရဲ႕ right edge နဲ႕ anchor လုပ္ေပးပါတယ္၉

Edge ၀ခုအကြာအေှးကေတာ့ „Right‟ property မွာေပးထားတဲ့အတုိင္းျဖစ္ပါမယ္၉

 BOTH

o Left edge ေရာ right edge ကိုပါ anchor လုပ္ပါတယ္၉

284
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Vertical Anchor

ပံု ၅-၆ဿ

ေရြးႏုိင္တဲ့ value ၁မ်ိဳးရွိပါတယ္၉

 TOP

 BOTTOM

 BOTH

(ေဘးတုိက္အစား အထက္ေအာက္ျဖစ္သြားျခင္းမွအပ Horizontal Anchor နဲ႕သေဘာတရားခ်င္း

တူပါတယ္၉)

7.10. Auto Scale

View တစ္ခုကို device ၾကီးၾကီးေပၚမွာျမင္ရတဲ့အခါ (view size တူေပမယ္)့ သာမန္ device

မွာ ျမင္ရတာထက္ ပိုငယ္သြားတယ္လို႕ထင္ရပါတယ္၉ အထက္မွာေျပာခဲ့သလို %x နဲ႕ %y သံုးျပီး

views size ခ်ိန္တာဟာ သိပ္ေတာ့အေကာင္းၾကီး မဟုတ္ေသးပါဘူး၉ Application ကုိ run မယ့္

device ရဲ႕ size ေပၚမူတည္ျပီး view size နဲ႕ text size ကုိေျပာင္းႏုိင္တဲ့နည္း ရွိပါေသးတယ္၉

အဲဒါကေတာ့ auto scale ျပဳလုပ္ျခင္းျဖစ္ပါတယ္၉

Auto scale ကုိေအာက္ပါ formula နဲ႕တြက္ပါတယ္၉

delta = ((100%x + 100%y) / (320dip + 430dip) - 1)

rate = 0.3

scale = 1 + rate * delta

285
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Auto scale function ၁ခုရွိပါတယ္၉

 AutoScaleRate (rate)

 AutoScale (View)

 AutoScaleAll

Auto scale function ဟာ standard variant (320 x 480, scale = 1.0)

ကိုအေျခခံထားပါတယ္၉

 AutoScaleRate(rate) ဟာ scale တြက္ဘုိ႕အတြက္ လိုအပ္တဲ့ rate value

ကိုသတ္မွတ္ေပးတာပါ၉ Rate value ဟာ 0 နဲ႕ 1 ၾကားျဖစ္ရပါမယ္၉ 0.2 နဲ႕ 0.5 ၾကား value

က အေကာင္းဆံုး ျဖစ္ပါတယ္၉ Default value က 0.3 ျဖစ္ပါတယ္၉ Rate value 1

ဆုိရင္ေတာ့ %x, %y ကုိသံုးတာနဲ႕ အတူတူဘဲ အက်ိဳးသက္ေရာက္မႈရွိပါမယ္၉

ဥပမာ rate value 0.5 အတြက…


AutoScaleRate (0.5)

 AutoScale (View) ကေတာ့ ဘယ္ view ကို auto scale လုပ္မယ္ဆိုတာ သတ္မွတ္တာပါ၉

ဥပမာ btnTest1 ကုိ auto scale လုပ္ခ်င္ရင္…

AutoScale (btnTest1)

ဒါဟာ ေအာက္ပါ code ေတြေရးတာနဲ႕သေဘာခ်င္းတူတူဘဲျဖစ္ပါတယ္၉

btnTest1.Left = btnTest1.Left * scale

btnTest1.Top = btnTest1.Top * scale

btnTest1.Width = btnTest1.Width * scale

btnTest1.Height = btnTest1.Height * scale

btnTest1.TextSize = btnTest1.TextSize * scale

286
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
 AutoScaleAll ကေတာ့ layout မွာပါတဲ့ view အားလံုးကို scale လုပ္တာပါ၉

Auto scale function ကုိ designer script မွာေအာက္ပါပံုစံမ်ိဳး ေရးရပါမယ္၉ Rate value ကုိ

0 ကေန 1 ၾကားအမ်ိဳးမ်ိဳးေပးျပီးစမ္းၾကည့္ပါ၉

'All variants script

AutoScaleRate (0.5)

AutoScaleAll

Example 7.15

Designer script အသံုးျပဳတဲ့ example တစ္ခုလုပ္ပါမယ္၉ Project တစ္ခု create လုပ္ပါ၉

(ပံု ၅-၆၀) မွာျပထားတဲ့အတုိင္း layout တစ္ခု create လုပ္ျပီး layout name ကုိ Main လုိ႕ေပးပါ၉

Layout Name: Main

ပံု ၅-၆၀

287
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Designer Script

Main layout ရဲ႕ designer script code ကို (ပံု ၅-၆၁) မွာျပထားတဲ့အတုိင္းေရးပါ၉

ပံု ၅-၆၁

(ပံု ၅-၆၂) မွာျပထားတဲ့အတုိင္း ေနာက္ထပ္ layout တစ္ခု create လုပ္ျပီး layout name ကုိ

Panel လုိ႕ေပးပါ၉

Layout Name: Panel

ပံု ၅-၆၂

288
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Designer Script

Panel layout ရဲ႕ designer script code ကို (ပံု ၅-၆၃) မွာျပထားတဲ့အတုိင္းေရးပါ၉

ပံု ၅-၆၃

Main Activity မွာ ေအာက္ပါအတုိင္းေရးပါ၉

#Region Module Attributes

#FullScreen: False

#IncludeTitle: True

#ApplicationLabel: AutoScaleExample1

#VersionCode: 1

#VersionName:

#SupportedOrientations: unspecified

#CanInstallToExternalStorage: False

#End Region

Sub Process_Globals

End Sub

289
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub Globals

Dim lblTitle, lblSubTitle As Label

Dim scvTest As ScrollView

Dim pnlToolBox, pnlSetup As Panel

Dim btnTest1, btnTest2, btnTest3 As Button

End Sub

Sub Activity_Create (FirstTime As Boolean)

Activity.LoadLayout ("Main")

scvTest.Panel.LoadLayout ("Panel")

scvTest.Panel.Height = pnlSetup.Height

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

အထက္ပါ application ကုိ 800/1280 10'' screen emulator မွာ rate value

အမ်ိဳးမ်ိဳးေျပာင္းျပီး run ၾကည့္တဲ့အခါ (ပံု ၅-၆၄) မွ (ပံု ၅-၇ဿ) ထိျပထားတဲ့အတုိင္း

ေတြ႕ရမွာျဖစ္ပါတယ္၉ 4800/800 7'' screen emulator မွာ rate value အမ်ိဳးမ်ိဳးေျပာင္းျပီး run

ၾကည့္တဲ့အခါ (ပံု ၅-၇၀) မွ (ပံု ၅-၇၅) ထိျပထားတဲ့အတုိင္း ေတြ႕ရမွာျဖစ္ပါတယ္၉ 320/480 3.5”

screen emulator မွာ rate value အမ်ိဳးမ်ိဳးေျပာင္းျပီး run ၾကည့္တဲ့အခါ (ပံု ၅-၇၆) မွာ

ျပထားတဲ့အတုိင္း rate value ဘယ္ေလာက္ေျပာင္းေျပာင္း ထူးျခားမႈမရွိတာ ေတြ႕ရမွာျဖစ္ပါတယ္၉

290
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

Rate = 0 Rate = 0.1

ပံု ၅-၆၄ ပံု ၅-၆၅

Rate = 0.3 Rate = 0.5

ပံု ၅-၆၆ ပံု ၅-၆၇

291
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

Rate = 0.7 Rate = 1

ပံု ၅-၇ှ ပံု ၅-၇ဿ

Rate = 0 Rate = 0.1

ပံု ၅-၇၀ ပံု ၅-၇၁

292
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

Rate = 0.3 Rate = 0.5

ပံု ၅-၇၂ ပံု ၅-၇၃

Rate = 0.7 Rate = 1

ပံု ၅-၇၄ ပံု ၅-၇၅

293
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-၇၆

Example 7.16

Example 7.15 မွာေရးထားတဲ့ application ကုိ landscape mode မွာ run ရင္

ၾကည့္မေကာင္းပါဘူး၉ (ပံု ၅-၇၇) မွာျမင္ရတဲ့အတုိင္း ေဘးသားေတြပိုေနျပီး ေအာက္ပိုင္းက view

ေတြေပ်ာက္ေနမွာပါ၉

ပံု ၅-၇၇

294
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ဒါေၾကာင့္ landscape orientation အတြက္ layout variant တစ္ခုထပ္လုပ္ပါမယ္၉ New

variant မွာ button ေတြပါတဲ့ ToolBox ကို screen ရဲ႕ညာဘက္ျခမ္းကို (ပံု ၅-ဿှှ)

မွာျပထားတဲ့အတုိင္း ေရႊ႕လိုက္ပါ၉

ပံု ၅-ဿှှ

Designer script မွာလည္းေအာက္ပါအတုိင္းေရးပါ၉

Main Layout File: 320 x 480, scale = 1 (160 dpi)

'All variants script

Rate value ကုိ 0.5 လုိ႕သတ္မွတ္ျပီး view အားလံုးကို autoscale လုပ္ပါ၉

AutoScaleRate (0.5)

AutoScaleAll

'Variant specific script: 320 x 480, scale=1

Label ေတြကို Activity ရဲ႕အလ်ားလိုက္ center မွာထားပါ၉

lblTitle.HorizontalCenter = 50%x

lblSubTitle.HorizontalCenter = 50%x

ToolBox Panel ကို Activity ရဲ႕အလ်ားလိုက္ center မွာထားပါ၉

pnlToolBox.HorizontalCenter = 50%x

295
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ToolBox Panel ရဲ႕ bottom ကို Activity bottom မွာထားပါ၉

pnlToolBox.Bottom = 100%y

ScrollView ကို Activity ရဲ႕ေထာင္လိုက္ center မွာထားပါ၉

scvTest.HorizontalCenter = 50%x

ScrollView ကုိ lblSubTitle နဲ႕ pnlToolBox ၾကားလြတ္တဲ့ေနရာအားလံုးယူျပီးထားပါ၉

ScrollView နဲ႕ pnlToolbox ၾကားမွာေတာ့ 10dip ေလာက္ျခားထားပါ၉

scvTest.SetTopAndBottom (lblSubTitle.Bottom, pnlToolBox.Top - 10dip)

Main Layout File: 480 x 320, scale = 1 (160 dpi)

'All variants script

Rate value ကုိ 0.5 လုိ႕သတ္မွတ္ျပီး view အားလံုးကို autoscale လုပ္ပါ၉

AutoScaleRate (0.5)

AutoScaleAll

'Variant specific script: 480 x 320, scale = 1

Activity left နဲ႕ ToolBox ၾကားကလြတ္တဲ့ေနရာ center မွာ lblTitle နဲ႕ lblSubTitle

ကိုထားပါ၉

lblTitle.HorizontalCenter = pnlToolBox.Left / 2

lblSubTitle.HorizontalCenter = pnlToolBox.Left / 2

ToolBox ကို Activity ရဲ႕ ေထာင္လုိက္ အလယ္တည့္တည့္မွာ ထားပါ၉

pnlToolBox.VerticalCenter = 50%y

296
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ToolBox ကို Activity ညာဘက္အစြန္းမွာ ကပ္ထားပါ၉

pnlToolBox.Right= 100%x

ScrollView height ကုိ (lblSubTitle နဲ႕ Activity ေအာက္ေျခၾကားက ရွိသေလာက္ေနရာ -

10dip) ထားပါ၉

scvTest.SetTopAndBottom (lblSubTitle.Bottom, 100%y - 10dip)

Panel Layout File: 320 x 480, scale = 1 (160 dpi)

'All variants script

Rate value ကုိ 0.5 လုိ႕သတ္မွတ္ျပီး view အားလံုးကို autoscale လုပ္ပါ၉

AutoScaleRate (0.5)

AutoScaleAll

'Variant specific script: 480 x 320, scale=1

ဘာမွမေရးပါ၉

480 x 320, scale = 1 (160 dpi) မွာ

'All variants script

Rate value ကုိ 0.5 လုိ႕သတ္မွတ္ျပီး view အားလံုးကို autoscale လုပ္ပါ၉

AutoScaleRate (0.5)

AutoScaleAll

'Variant specific script: 480 x 320, scale=1

ဘာမွမေရးပါ၉

အထက္ပါအတုိင္း Designer script မွာေရးျပီး run ရင္ (ပံု ၅-ဿှဿ) မွာျပထားတဲအ


့ တုိင္း

297
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
landscape orientation မွာပါၾကည့္ေကာင္းတဲ့ layout ကိုရရွိမွာျဖစ္ပါတယ္၉

ပံု ၅-ဿှဿ

7.11. UI Cloud

Application ကုိ device အသီးသီးမွာ run ၾကည့္ဘုိ႕ဆိုတာ လက္ေတြ႕မွာမလြယ္ပါဘူး၉

ဒီေလာက္မ်ားျပားတဲ့ device ေတြကို ဘယ္သူမွ မပိုင္ဆုိင္ႏုိင္သလို ရွိခဲ့ရင္ေတာင္ လုိက္စမ္းေနလို႕

အဆင္မေျပပါဘူး၉ UI Cloud သံုးလိုက္ရင္ေတာ့ Application ကုိ device အသီးသီးမွာ run ရင္

ျမင္ရမယ့္အေနအထားကို ပံုနဲ႕တကြ ၾကည့္ႏုိင္ပါတယ္၉ Designer script နဲ႕ layout

တစ္ခုဖန္တီးျပီးသြားရင္ အဲဒါကို UI Cloud ကုိ ပို႕ႏုိင္ပါတယ္၉ Tools menu ကေနသြားရမယ္၉

298
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-ဿှ၀

UI Cloud Screen နမူနာမ်ား

ပံု ၅-ဿှ၁

299
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၅-ဿှ၂

300
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
တကယ့္ size ကုိသိခ်င္ရင္ အဲဒီပံုေပၚမွာ click လုပ္ၾကည့္ႏုိင္ပါတယ္၉

ပံု ၅-ဿှ၃

301
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

Chapter 8

Tutorials

8.1. Maths Trainer

ကေလးမ်ားအတြက္ Maths trainer application တစ္ခုေရးၾကည့္ရေအာင္၉ User interface

layout ကုိ emulator မွာလုပ္ျပပါမယ္၉ Desktop ေပၚက Basic4android ကို run ပါ၉

Emulator ကို run ရန္

IDE menu မွာ [Tools >Run AVD Manager] ကို ေရြးပါ၉ (ပံု ၆-ဿ)

ပံု ၆-ဿ

302
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
(ပံု ၆-၀) အတုိင္းေပၚလာရင္ Close button ကို click ပါ၉

ပံု ၆-၀

(ပံု ၆-၁) မွာျပထားတဲ့အတုိင္း [Tools > Manage AVDs…] ကိုေရြးပါ၉

ပံု ၆-၁

(ပံု ၆-၂ နဲ႕ ၆-၃) မွာ ျမွားျပထားတာေတြကို click ပါ၉

303
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၆-၂

ပံု ၆-၃

304
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Emulator ready ျဖစ္ေအာင္ ေစာင့္ပါ၉ အခ်ိန္အနည္းငယ္ ၾကာတတ္ပါတယ္၉ (ပံု ၆-၄)

အတိုင္းေပၚလာရင္ emulator ready ျဖစ္ပါျပီ၉

ပံု ၆-၄

Project ကို save လုပ္ရန္

Project ကို save ရန္ MyFirstProgram ဆိုတဲ့ folder တစ္ခု create လုပ္ပါ၉ Project name

ကုိ MyFirstProgram.b4a လို႕ေပးျပီး save ပါ၉

ပံု ၆-၅

305
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Package Nameေပးရန္

Project တုိင္းကို package name ေပးရမယ္၉ IDE menu မွာ [Project > Package Name]

ကိုေရြးပါ၉ (ပံု ၆-၆)

ပံု ၆-၆

(ပံု ၆-၇) မွာျပထားတဲ့အတုိင္း „Set package name‟ dialog box ေပၚလာမယ္၉

ပံု ၆-၇

Default name က b4a.example ပါ၉ b4a.myfistprogram လို႕ေျပာင္းလိုက္ပါ၉ Package

name ဟာ lower case ဘဲျဖစ္ရမယ္၉

306
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Application Label သတ္မွတ္ရန္

Code editor ရဲ႕ထိပ္မွာ region ႏွစ္ခရ


ု ွိတယ္၉ (ပံု ၆-ဿှ)

ပံု ၆-ဿှ

Region ဆိုတာ collapse/extend လုပ္ႏုိင္တဲ့ code အစိတ္အပိုင္းတစ္ခုပါ၉ + ကို click ရင္

region ကုိ extend လုပ္မယ္၉ - ကို click ရင္ region ကုိ collapse လုပ္မယ္၉ (ပံု ၆-ဿဿ)

ပံု ၆-ဿဿ

Region ေတြကို extend လုပ္ရင္ ေအာက္ပါအတုိင္း Project Attributes နဲ႕ Activity

Attributes ေတြကို ျမင္ရပါမယ္၉

#Region Project Attributes

#ApplicationLabel: B4A Example

#VersionCode: 1

#VersionName:

'SupportedOrientations possible values: unspecified, landscape or portrait.

#SupportedOrientations: unspecified

#CanInstallToExternalStorage: False

#End Region

307
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
#Region Activity Attributes

#FullScreen: False

#IncludeTitle: True

#End Region

ApplicationLable ရဲ႕ default name က B4A Example ပါ၉ အဲဒါကို MyFirstProgram

လုိ႕ေျပာင္းလိုက္ပါ၉ User interface layout ျပဳလုပ္ဘုိ႕ IDE menu မွာ Designer ကုိ ေရြးပါ၉

(ပံု ၆-ဿ၀)

ပံု ၆-ဿ၀

(ပံု ၆-ဿ၁) မွာျပထားတဲ့အတုိင္း Designer window ေပၚလာမယ္၉

ပံု ၆-ဿ၁

308
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
IDE နဲ႕ device/emulator မခ်ိတ္ဆက္ရေသးရင္ (ပံု ၆-ဿ၂) မွာျပထားတဲ့အတုိင္း

Disconnected ျဖစ္ေနမယ္၉

ပံု ၆-ဿ၂

User interface layout ကုိ emulator မွာ လုပမ


္ ွာမုိ႕ IDE ကို emulator နဲ႕ခ်ိတ္ပါ၉

(ပံု ၆-ဿ၃)

ပံု ၆-ဿ၃

IDE နဲ႕ emulator ခ်ိတ္မိသည္ထိေစာင့္ပါ၉ အခ်ိန္အနည္းငယ္ၾကာတတ္ပါတယ္၉ ျပီးသြားရင္

(ပံု ၆-ဿ၄) မွာျပထားတဲ့အတုိင္း Connected ျဖစ္သြားမယ္၉

309
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၆-ဿ၄

Emulator ကုိၾကည့္ပါ၉ (ပံု ၆-ဿ၅) မွာျပထားတဲ့အတုိင္းျမင္ရမယ္၉

ပံု ၆-ဿ၅

User interface layout ကုိ (ပံု ၆-ဿ၆) မွာ ျပထားတဲ့အတုိင္း ျဖစ္ေအာင္လုပ္ရမယ္၉

ပံု ၆-ဿ၆

310
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Activity မွာေအာက္ပါ view ေတြထည့္ရမယ္၉

 Label ၀ခု (ဿ ကေန ၇ ထိ random number ေတြကိုျပဘုိ႕ပါ)

 Label ဿခု (+ သေကၤတကိုျပဘို႕)

 EditText ဿခု (Number ၀ခုရဲ႕ေပါင္းလာဒ္ အေျဖရိုက္ထည့္ဘို႕ပါ)

 Button ဿခု (အေျဖရိုက္ထည့္ျပီးရင္ click ဘို႕ပါ)

 Label ဿခု (အေျဖ မွား/မွန္ေဖာ္ျပဘို႕ပါ)

Label ဆုိတာ text ကိုျပဘို႕သုံးတဲ့ view, EditText ဆိုတာ text ရိုက္ထည့္ႏုိင္တဲ့ view,

Button ဆိုတာ user က action တစ္ခုခု (ဥပမာ click) လုပ္ႏုိင္တဲ့ view ျဖစ္ပါတယ္၉

Number ေတြျပဘို႕ Label ၀ခုထည့္ရမယ္၉ အရင္ဆံုး Label တစ္ခုထည့္ပါ၉ (ပံု ၆-ဿ၇)

ပံု ၆-ဿ၇

311
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Emulator မွာ Label တစ္ခုေပၚလာမယ္၉ (ပံု ၆-၀ှ)

ပံု ၆-၀ှ

Label ရဲ႕ default properties ေတြကိုလည္း Designer မွာျမင္ရမယ္၉ (ပံု ၆-၀ဿ)

ပံု ၆-၀ဿ

312
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Label size ကို လိုခ်င္တဲ့အရြယ္ရေအာင္ mouse နဲ႕ဆြဲျပင္ျပီး စိတ္ၾကိဳက္ေနရာကို

ေရႊ႕လိုက္ပါ၉ (ပံု ၆-၀၀)

ပံု ၆-၀၀

Designer မွာလည္း Left, Top, Width, Height properties ေတြေျပာင္းသြားတာ ေတြ႕မယ္၉

(ပံု ၆-၀၁)

ပံု ၆-၀၁

Designer မွာ Left, Top, Width, Height properties ေတြကို တုိက္ရိုက္ျပင္လိုက

ျပင္ႏုိင္ပါတယ္၉ Label name ကုိ lblNumber1 လို႕ေျပာင္းလုိက္ပါ၉ lbl ဆိုတာ ဒီ view ဟာ label

ျဖစ္ေၾကာင္းအလြယ္တကူသိေအာင္၈ Number1 ဆိုတာကေတာ့ First number ကုိ ျပမွာျဖစ္လို႕ပါ၉

အမည္ေပးတဲ့အခါ ဘယ္လို view အမ်ိဳးအစားလည္း၈ ဘာအတြက္ သံုးမွာ လဲဆိုတာ ေပၚလြင္ေအာင္

ခုလိုမ်ိဳးေပးသင့္ပါတယ္၉ (ပံု ၆-၀၂)

313
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၆-၀၂

အမည္ေျပာင္းျပီးရင္ enter key ကုိေခါက္ပါ၉ Event name property ပါ လိုက္ေျပာင္းသြား

ပါလိမ့္မယ္၉ (ပံု ၆-၀၃)

ပံု ၆-၀၃

Properties ေတြကေတာ့ ေအာက္ပါအတုိင္းျဖစ္ပါတယ္၉

Name: View ရဲ႕ name

Type: View ရဲ႕ type (ဒါကိုေတာ့ေျပာင္းလို႕မရပါဘူး)

Event Name: View ရဲ႕ event name

Parent: View ဟာ ဘယ္ေပၚမွာရွိေနတယ္ဆိုတာ ျပပါတယ္ (ခုဆို Activity ေပၚမွာရွိေနတာေပါ့)

က်န္တဲ့ properties ေတြကို (ပံု ၆-၀၄) အတုိင္းျဖစ္ေအာင္ ေျပာင္းလိုက္ပါ၉

314
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၆-၀၄

ေနာက္ထပ္ အလားတူ Label တစ္ခု လိုေသးတယ္၉ အသစ္ထပ္မထည့္ဘဲ ပထမတစ္ခုကို

copy လုပ္မယ္၉ (ပံု ၆-၀၅)

ပံု ၆-၀၅

315
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
အခုဆိုရင္ emulator မွာ (ပံု ၆-၀၆) အတုိင္းျဖစ္သြားမယ္၉

ပံု ၆-၀၆

Name နဲ႕ Left property ၀ခုကိုဘဲျပင္ပါ၉ (ပံု ၆-၀၇)

ပံု ၆-၀၇

Emulator မွာ (ပံု ၆-၁ှ) အတုိင္းျဖစ္သြားမယ္၉

ပံု ၆-၁ှ

316
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Math sign (+) သေကၤတကိုျပဘို႕အတြက္ ေနာက္ထပ္ Label တစ္ခု ထပ္ထည့္မယ္၉

Emulator ေပၚက lblNumber1 ကုိ click ပါ၉ Designer မွာ [Tools > Duplicate Selected View]

ကိုေရြးပါ၉ (ပံု ၆-၁ဿ) အတုိင္းျဖစ္သြားမယ္၉

ပံု ၆-၁ဿ

တတိယေျမာက္ Label ကုိ ပထမ Label နဲ႕ ဒုတိယ Label ၾကားမွာထားပါ၉ Name ကို

lblMathSign လို႕ေပးျပီး Text property မွာ „+‟ ေရးပါ၉ (ပံု ၆-၁၀) အတုိင္းျဖစ္သြားမယ္၉

ပံု ၆-၁၀

EditText view တစ္ခုထည့္ဦးမယ္၉ Designer menu မွာ [Add View > EditText]

ကိုေရြးပါ၉ (ပံု ၆-၁၁)

317
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၆-၁၁

EditText view ရဲ႕ property ကုိ (ပံု ၆-၁၂) အတုိင္း ျပင္ပါ၉ edt ဆိုတာ EditText မွန္း

သိေအာင္၈ Result ဆိုတာ Result ကုိထည့္ဘုိ႕ေပါ့၉

ပံု ၆-၁၂

318
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Input Type မွာ NUMBERS လို႕ သတ္မွတ္လိုက္ရင္ EditText ထဲကို number ေတြသာ

ရိုက္ထည့္လို႕ရပါမယ္၉ Hint Text ကေတာ့ user ကို ဒီေနရာမွာ ဘာရိုက္ထည့္ေပးရမယ္ဆိုတာ

ေျပာျပမယ့္ text ျဖစ္ပါတယ္၉ ျပီးရင္emulator မွာ (ပံု ၆-၁၃) အတုိင္း ျဖစ္သြားမယ္၉

ပံု ၆-၁၃

Button တစ္ခုထပ္ထည့္ရေအာင္၉ (ပံု ၆-၁၄)

ပံု ၆-၁၄

319
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
(ပံု ၆-၁၅) မွာျပထားတဲ့အတုိင္း Button property ေတြ ျပင္ပါ၉

ပံု ၆-၁၅

Emulator ကုိၾကည့္ပါ၉ (ပံု ၆-၁၆) မွာျပထားတဲ့အတုိင္းျဖစ္သြားမယ္၉

ပံု ၆-၁၆

320
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Comment အတြက္ Label တစ္ခု ထည့္ပါဦး၉ Properties ေတြကို (ပံု ၆-၁၇) အတုိင္း ျပင္ပါ၉

(ပံု ၆-၂ှ) မွာျပထားတဲ့အတုိင္းျဖစ္သြားမယ္၉

ပံု ၆-၁၇ ပံု ၆-၂ှ

အခုလုပ္ခဲ့တဲ့ layout ကုိ file တစ္ခုမွာသိမ္းမယ္၉ (ပံု ၆-၂ဿ နဲ႕ ၆-၂၀)

ပံု ၆-၂ဿ
ပံု ၆-၂၀

321
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
View ေတြကို code ထဲမွာေခၚသံုးဘို႕ဆိုရင္ declare လုပ္ေပးရမယ္၉ Button click event

အတြက္ subroutine frame တစ္ခုလည္းလုပ္ရမယ္၉ (ပံု ၆-၂၁) မွာျပထားတဲ့အတုိင္းလုပ္ပါ၉

ပံု ၆-၂၁

(ပံု ၆-၂၂) မွာျပထားတဲ့အတုိင္း check လုပ္ပါ၉ ျပီးရင္ [Generate members > Close]

ကိုေရြးပါ၉

ပံု ၆-၂၂

322
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
View ေတြကို check လုပ္လိုက္ျခင္းျဖင့္ Globals subroutine မွာ ေအာက္ပါအတုိင္း

generate လုပ္ေပးပါတယ္၉

Dim btnAction As Button

Dim edtResult As EditText

Dim lblComments As Label

Dim lblMathSign As Label

Dim lblNumber1 As Label

Dim lblNumber2 As Label

View ရဲ႕ event ကုိ check လုပ္ျခင္းျဖင့္ event sub frame တစ္ခုကို ေအာက္ပါအတုိင္း

generate လုပ္ေပးပါတယ္၉

Sub btnAction_Click

End Sub

အခုကစျပီး code ေတြေရးေတာ့မွာမုိ႕ code editor ကိုသြားပါ၉ Layout file ကုိ Activity ေပၚ

load လုပ္ဘို႕လိုပါတယ္ (ဒါမွသာ screen ေပၚမွာ ျမင္ရမယ္ေလ) Activity_Create sub မွာ

လုပ္ရမယ္၉

Sub Activity_Create(FirstTime As Boolean)

Activity.LoadLayout("Main")

End Sub

Program ကုိ စတင္တာနဲ႕ new problem တစ္ခုကို generate လုပ္ဘ္ို႕လိုတယ္၉ New

323
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
problem ဆိုတာ 1 ကေန 9 ထိ random number „Number1‟ နဲ႕ „Number2‟ ကို generate လုပ္ျပီး

lblNumber1 နဲ႕ lblNumber2 တုိ႕မွာ အသီးသီးျပဘို႕ကို ေျပာတာပါ၉ Number ၀ခုကို

သိမ္းဘုိ႕အတြက္ variable ၀ခုကို Globals sub မွာ ေအာက္ပါအတုိင္း declare လုပ္ပါ၉

Sub Globals

Dim btnAction As Button

Dim edtResult As EditText

Dim lblComments As Label

Dim lblMathSign As Label

Dim lblNumber1 As Label

Dim lblNumber2 As Label

Dim Number1, Number2 As Int

End Sub

ျပီးရင္ „New‟ sub routine ကို ေအာက္ပါအတုိင္း ေရးပါ၉

Sub New

Number1 = Rnd (1, 10)

Number2 = Rnd (1, 10)

lblNumber1.Text = Number1

lblNumber2.Text = Number2

lblComments.Text = "Enter the result" & CRLF & "and click on OK"

edtResult.Text = ""

End Sub

324
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
„New‟ subroutine ကုိ ေခၚမယ့္ code ကုိ Activity_Create subroutine မွာ

ေအာက္ပါအတုိင္း ေရးပါ၉

Sub Activity_Create (FirstTime As Boolean)

Activity.LoadLayout ("Main")

New

End Sub

Rnd (1, 10) ဆိုတာ 1 ကေန 9 ထိ random number တစ္ခက


ု ို generate လုပ္ခုိင္းတာ ပါ၉

lblComments.Text = "Enter the result" & CRLF & "and click on OK" ဆုိတာကေတာ့

Enter the result

and click OK

ဆုိတာကို lblComments (Label) မွာ ျပတာပါ၉ CRLF ဆိုတာက ေနာက္တစ္ေၾကာင္း ဆင္းခုိင္းတာ

ေပါ့၉

ျပီးရင္ btnAction_Click event အတြက္ code ေရးမယ္၉

Sub btnAction_Click

If btnAction.Text = "O K" Then

If edtResult.Text="" Then

Msgbox ("No result entered","E R R O R")

Else

CheckResult

325
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
End If

Else

New

btnAction.Text = "O K"

End If

End Sub

btnAction.Text = O K ျဖစ္ရင္ edtResult.Text = blank ျဖစ္လား စစ္တယ္၉

If btnAction.Text = "O K" Then

If edtResult.Text="" Then

Blank ျဖစ္ေနရင္ No result entered ဆိုျပီး အေျဖမရိုက္ထည့္ရေသးေၾကာင္း Message box

ျပမယ္၉

Msgbox ("No result entered","E R R O R")

Blank မျဖစ္ရင္ (အေျဖရိုက္ထည့္ထားရင္) ေတာ့ CheckResult subroutine ကုိ call မယ္၉

CheckResult

btnAction.Text က O K မဟုတ္ဘူးဆိုရင္ေတာ့ „New‟ subroutine ကို call မယ္၉ ျပီးရင္

btnAction.Text ကို O K လုိ႕ျပင္ေရးမယ္၉

New

btnAction.Text = "O K"

CheckResult subroutine မွာ ဘယ္လိုေရးမလဲ ၾကည့္ရေအာင္၉

edtResult.Text = Number1 + Number2 ျဖစ္လားစစ္တယ္၉

326
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
If edtResult.Text = Number1 + Number2 Then

ျဖစ္ရင္ lblComments.Text မွာ

G O O D result

Click on NEW

လုိ႕ျပမယ္၉ ျပီးရင္ btnAction.text မွာလည္း N E W လုိ႕ေျပာင္းေပးဘုိ႕ေအာက္ပါအတိုင္းေရးမယ္၉

lblComments.Text = "G O O D result" & CRLF & "Click on NEW"

btnAction.Text = "N E W"

edtResult.Text = Number1 + Number2 မျဖစ္ရင္ lblComments.Text မွာ

W R O N G result

Enter a new result

and click OK

လုိ႕ျပဘုိ႕ေအာက္ပါအတုိင္းေရးရမယ္၉

lblComments.Text = "W R O N G result" & CRLF & "Enter a new result" & CRLF &

"and click OK"

327
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Complete Code

#Region Project Attributes

#ApplicationLabel: MyFirstProgram

#VersionCode: 1

#VersionName:

'SupportedOrientations possible values: unspecified, landscape or portrait.

#SupportedOrientations: unspecified

#CanInstallToExternalStorage: False

#End Region

#RegionActivity Attributes

#FullScreen: False

#IncludeTitle: True

#End Region

Sub Process_Globals

End Sub

Sub Globals

Dim btnAction As Button

Dim edtResult As EditText

Dim lblComments As Label

Dim lblMathSign As Label

Dim lblNumber1 As Label

328
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Dim lblNumber2 As Label

Dim Number1, Number2 As Int

End Sub

Sub Activity_Create(FirstTime AsBoolean)

Activity.LoadLayout("Main")

New

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed AsBoolean)

End Sub

Sub New

Number1 = Rnd (1, 10)

Number2 = Rnd (1, 10)

lblNumber1.Text = Number1

lblNumber2.Text = Number2

lblComments.Text = "Enter the result" & CRLF & "and click on OK"

edtResult.Text = ""

End Sub

329
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub btnAction_Click

IfbtnAction.Text = "O K"Then

IfedtResult.Text=""Then

Msgbox ("No result entered","E R R O R")

Else

CheckResult

EndIf

Else

New

btnAction.Text = "O K"

EndIf

End Sub

Sub CheckResult

If edtResult.Text = Number1 + Number2 Then

lblComments.Text = "G O O D result" & CRLF & "Click on NEW"

btnAction.Text = "N E W"

Else

lblComments.Text = "W R O N G result" & CRLF & "Enter a new result"

& CRLF & "and click OK"

EndIf

End Sub

330
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
8.2. Numeric Keyboard

ဒီ Tutorial ကေတာ့ Maths Trainer မွာ keyboard တစ္ခုထပ္ထည့္မွာျဖစ္ပါတယ္၉

SecondProgram ဆိုတဲ့ folder တစ္ခု လုပ္ပါ၉ MyFirstProgram ထဲက file နဲ႕ folder ေတြ

အားလံုးကို SecondProgram ထဲ ကူးထည့္လိုက္ပါ၉ MyFirstProgram.b4a ကို

SecondProgram.b4a လုိ႕ အမည္ေျပာင္းလိုက္ပါ၉ IDE ကုိဖင


ြ ့္ျပီး SecondProgram.b4a ကို ဖြင့္ပါ၉

Designer ကုိ ဖြင့္ပါ၉ IDE နဲ႕ emulator ကုိ ခ်ိတ္ပါ၉ MyFirstProgram လုပ္ခဲ့တုန္းက number

၀လံုးေပါင္းလာဒ္ကို ရိုက္ထည့္ဘုိ႕ emulator မွာပါတဲ့ keyboard ကုိ သံုးခဲ့ရတယ္ေနာ္၉ ခုေတာ့

ကုိယ္ပိုင္ keyboard တစ္ခု လုပ္ပါမယ္၉ ရိက


ု ္ထည့္မွာက number ေတြဘဲ ရိုက္မွာဆိုေတာ့ 0 to 9 ဘဲ

ပါမွာေပါ့၉ ဒါေၾကာင့္ numeric keyboard လို႕ ေခၚရေအာင္၉ (ပံု ၆-၂၃) မွာျပထားတဲ့

အတုိင္းျဖစ္ေအာင္ လုပ္ၾကရမွာေနာ္၉

ပံု ၆-၂၃

331
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Package name ကုိလည္း ေျပာင္းဘုိ႕လုိေသးတယ္၉ (ပံု ၆-၂၄ နဲ႕ ၆-၂၅) မွာ ျပထားတဲ့

အတုင
ိ ္းလုပ္ပါ၉

ပံု ၆-၂၄

ပံု ၆-၂၅

Package name ေပးတဲ့အခါ lowercase (စာလံုးေသး) ေတြဘဲ သံုးရမယ္ဆိုတာ

မေမ့နဲ႕ေနာ္၉ ApplicationLabel ကို SecondProgram လုိ႕ေျပာင္းပါ၉ „edtResult‟ EditText view ကို

ဖ်က္ျပီး Label တစ္ခုထပ္ထည့္ပါ၉ ဖ်က္ဘုိ႕က ေတာ့ emulator မွာ edtResult ကုိ select လုပ္ပါ၉

ပံု ၆-၂၆

332
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ျပီးရင္ Designer menu မွာ (ပံု ၆-၂၇) မွာျပထားတဲ့အတိုင္း ေရြးလုိက္ပါ၉

ပံု ၆-၂၇

ဒါဆို „edtResult‟ EditText view မရွိေတာ့ဘူး၉ (ပံု ၆-၃ှ)

ပံု ၆-၃ှ

ခု Label တစ္ခု ထပ္ထည့္မယ္ေနာ္၉ ရွိျပီးသား Label ကုိဘဲ ပြားယူမယ္ေလ၉ ဒါေၾကာင့္

lblNumber1 ကုိ select လုပ္ပါ၉ ျပီးရင္ (ပံု ၆-၃ဿ) မွာျပထားတဲ့အတုိင္း Label တစ္ခု ပြားယူလုိက္ပါ၉

333
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၆-၃ဿ

ပံု ၆-၃၀

ပြားယူထားတဲ့ Label ကုိ (ပံု ၆-၃၁) မွာျပထားတဲ့အတုိင္း ေနရာခ်ျပီး size ကုိေျပာင္းပါ၉

ပံု ၆-၃၁

334
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၆-၃၂

ပံု ၆-၃၃

(ပံု ၆-၃၂) မွာျပထားတဲ့အတုိင္း property ေတြကိုေျပာင္းေပးပါ၉ (ပံု ၆-၃၃) မွာျပထားတဲ့

အတုိင္းျဖစ္လာမယ္၉

ျပီးရင္ keyboard အတြက္ key ေတြထားဘုိ႕ Panel view တစ္ခုထည့္ရေအာင္၉ Panel ကုိ

ေနရာခ်ျပီး (ပံု ၆-၃၅) မွာျပထားတဲ့အတုိင္း property ေတြသတ္မွတ္ေပးပါ၉

335
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၆-၃၄

ပံု ၆-၃၅

OK button ကုိ Panel ထဲ ေရႊ႕ရေအာင္၉ ေရႊ႕နည္းကေတာ့ OK button ကို select လုပ္ပါ၉

(view တစ္ခုနဲ႕ ပတ္သက္ျပီး တစ္စံုတစ္ခု လုပ္ခ်င္တိုင္း အဲဒီ view ကုိအရင္ select လုပ္ရမယ္ဆိုတာ

မွတ္ထားေပးပါ၉) (ပံု ၆-၃၆)

ပံု ၆-၃၆

ပံု ၆-၃၇

336
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
(ပံု ၆-၃၇) မွာျပထားတဲ့အတုိင္း Ok button ရဲ႕ Parent ကုိ pnlKeyboard လို႕ေျပာင္းပါ၉

အခုဆိုရင္ OK button ဟာ Panel ထဲေရာက္သြားပါျပီ၉ (ပံု ၆-၄ှ)

ပံု ၆-၄ှ

ပံု ၆-၄ဿ

lblComments ကို lblResult နဲ႕နီးသြားေအာင္ အထက္ကို mouse နဲ႕တြန္းေရႊ႕ေပးပါ၉ ျပီးရင္

Panel ရဲ႕ height ကိုလည္း ခ်ဲ႕ေပးပါ၉ (ပံု ၆-၄ဿ)

ဒီတစ္ခါ Panel ထဲကို Button တစ္ခထ


ု ပ္ထည့္ရေအာင္၉ Panel ကုိ select လုပ္ပါ၉ (ပံု ၆-၄၀)

ပံု ၆-၄၀ ပံု ၆-၄၁

337
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ျပီးရင္ (ပံု ၆-၄၁) မွာျပထားတဲ့ အတုိင္း Button တစ္ခုထပ္ထည့္ပါ၉ (ပံု ၆-၄၂)

မွာျပထားတဲ့အတုိင္း Panel ထဲမွာ Button အသစ္တစ္ခုေရာက္လာပါလိမ့္မယ္၉

ပံု ၆-၄၂

(ပံု ၆-၄၃) မွာျပထားတဲ့အတုိင္း property ေတြေျပာင္းေပးပါ၉

ပံု ၆-၄၃

338
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Panel ထဲမွာ 0 လို႕ေပၚေနတဲ့ Button တစ္ခုေရာက္သြားျပီေနာ္၉ (ပံု ၆-၄၄)

ပံု ၆-၄၄

ေနာက္ထပ္ 1 ကေန 9 ထိ button ေတြထပ္လုပ္ရမယ္ေနာ္၉ ဒါေၾကာင့္ ဒီ Button ကုိ select

လုပ္ပါ၉ ျပီးရင္ ပြားပါ၉ (ပံု ၆-၄၅ နဲ႕ ၆-၄၆)


ပံု ၆-၄၆

ပံု ၆-၄၅

(ပံု ၆-၄၇) မွာျပထားတဲ့အတုိင္း Button တစ္ခုတုိးလာမယ္၉ ပံု ၆-၅ှ

ပံု ၆-၄၇

339
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ျပီးရင္ (ပံု ၆-၅ှ) မွာျပထားတဲ့အတုိင္း parameter ေတြေျပာင္းလိုက္ပါဦး၉ (ပံု ၆-၅ဿ)

အတုိင္းျဖစ္သြားမယ္ေနာ္၉

ပံု ၆-၅၀

ပံု ၆-၅ဿ

ဒီလုိနဲ႕ ေနာက္ထပ္ 2 ကေန 9 ထိရေအာင္ ကိုယ္ဘာသာကိုယ္ဆက္လုပ္ေနာ္၉ ျပီးရင္

(ပံု ၆-၅၀) မွာျပထားတဲ့အတုိင္း ျဖစ္ေနရမယ္၉

(ပံု ၆-၅၀) မွာျပထားတဲ့အတုိင္းျဖစ္ျပီဆိုရင္ Button ေတြထဲက တစ္ခုကို select လုပ္ျပီး

ေနာက္ထပ္ Button တစ္ခု ရေအာင္ပြားပါဦး၉ အသစ္ရလာတဲ့ Button ကို select လုပ္ျပီး သူ႕အတြက္

parameter ေတြကို (ပံု ၆-၅၁) မွာျပထားတဲ့အတိုင္း ေျပာင္းေပးပါ၉

ပံု ၆-၅၁

340
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၆-၅၂

OK button ကို select လုပ္ျပီး သူ႕ရဲ႕ parameter ေတြကိုလည္း (ပံု ၆-၅၂)

မွာျပထားတဲ့အတုိင္း ေျပာင္းေပးပါဦး၉ ျပီးရင္ေတာ့ Layout က (ပံု ၆-၅၃) မွာျပထားတဲ့အတုိင္း

ျဖစ္သြားျပီေနာ္၉

ပံု ၆-၅၃

341
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
အခု code ေတြ စေရးရေအာင္၉

Globals

အရင္ဆံုးလုပ္ရမွာက Sub Globals မွာ

Dim edtResult As EditText အစား

Dim lblResult As Label လို႕ေျပာင္းေရးပါ၉

မွတ္မိေသးလား၉ စစခ်င္းမွာ edtResult EditText view ကုိ ဖ်က္ျပီး lblResult Label

ထည့္ခဲ့တာေလ၉ ဒါေၾကာင့္ ဒီမွာလာေျပာင္းေပးရတာ၉ ထပ္ျပီး code editor မွာ edtResult

လို႕ေရးထားတဲ့ေနရာတုိင္းကို lblResult လို႕လိက


ု ္ျပင္ေရးလိုက္ပါ၉

btnEvent_Click

ကဲ ဒီတစ္ခါေတာ့ Button ေတြကို click လုပ္တဲ့အခါ ေဆာင္ရြက္ရမယ့္ code

ေတြေရးေတာ့မယ္၉ BtnAction (OK button) ကလြဲျပီး က်န္ Button အားလံုး ရဲ႕ event name ကို

btnEvent လုိ႕ေပးခဲ့တာေနာ္၉ မေသခ်ာရင္ Button ေတြရဲ႕ property ကုိ ျပန္ၾကည့္ပါ၉ အဲဒီမွာ Event

Name က btnEvent ဆိုတာ ေတြ႕ပါလိမ့္မယ္၉ ဒါေၾကာင့္ Button ေတြကို click တဲ့အခါ

ျဖစ္ေပၚလာမယ့္ event subroutine name က btnEvent_Click ျဖစ္ပါတယ္၉ Button တစ္ခုခုကို

click တဲ့အခါေဆာင္ရြက္ေပးရမယ့္ code ေတြကို Sub btnEvent_Click နဲ႕ End Sub ၾကားမွာ

ေရးရမယ္၉

Sub btnEvent_Click

Dim Send As Button

Send = Sender

Select Send.Tag

Case "BS"

If lblResult.Text.Length >0 Then

lblResult.Text=lblResult.Text.SubString2(0,lblResult.Text.Length- 1)

342
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
End If

Case Else lblResult.Text = lblResult.Text & Send.Tag

End Select

If lblResult.Text.Length = 0 Then

btn0.Visible = False

Else

btn0.Visible = True

End If

End Sub

အထက္ပါ code ေတြကို တစ္ခုခ်င္းရွင္းျပပါမယ္၉

User click လုပ္လုိက္တဲ့ Button ကုိထည့္ဘုိ႕အတြက္ Button view „Send‟ ကို declare

လုပ္ပါတယ္၉

Dim Send As Button

User က view တစ္ခုကို click လုပ္လုိက္ျပီဆိုရင္ အဲဒီ view ရဲ႕ object reference ဟာ

Sender ထဲကို ေရာက္သြားပါတယ္၉ (အလြယ္မွတ္ခ်င္ရင္ user က Button တစ္ခုကို click လုပ္လုိက္

ရင္ အဲဒီ Button ရဲ႕ reference က Sender ထဲေရာက္သြားတယ္လို႕မွတ္ပါ) Send ထဲမွာ Sender ကုိ

assign လုပ္ပါ၉

Send = Sender

ဒါဆို Send ထဲမွာ click ခံရေသာ Button ေရာက္သြားျပီေနာ္၉

ခုတစ္ခါ click ခံရတဲ့ Button ဟာ „<‟ (Backspace button) လား၈ 0 to 9 (Numeric

button) လားဆိုတာကို ခြဲျခားရဦးမယ္၉ Send.Tag ကုိ Select Case နဲ႕စစ္ရမွာပါ၉

ဘာေၾကာင့္လည္းဆိုေတာ့ Send ထဲမွာ click ခံရေသာ Button ရွိေနမွာဆိုေတာ့ Send.Tag မွာ click

343
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ခံရေသာ Button ရဲ႕ Tag value ရွိေနမွာမုိ႕ပါ၉ Backspace button ကုိ click တာဆိုရင္ Send.Tag

မွာ „BS‟ ရွိေနမွာပါ၉ ဘယ္လိုလုပ္ျပီး ရွိေနတာလည္းသိခ်င္ရင္ (ပံု ၆-၅၂) မွာျပန္ၾကည့္ပါ၉ Designer

property မွာ Backspace button ရဲ႕ Tag value ကုိ „BS‟ လို႕ေပးခဲ့လို႕ပါ၉

Send.Tag ရဲ႕ value ကို Select Case နဲ႕စစ္ပါေတာ့မယ္၉

Select Send.Tag

Case "BS"

Send.Tag = BS လားဆိုတာ စစ္ပါတယ္၉ BS ဆိုရင္ user က Backspace ကိုႏွိပ္တာေပါ့၉

Backspace ကိုႏွိပ္ရင္ lblReult ထဲကေနာက္ဆံုး character တစ္လံုးကိုဖ်က္ေပးရမယ္၉ lblResult မွာ

အနည္းဆံုး character တစ္ခုေတာ့ ရွိရမွာေပါ့၉ မရွိရင္ ဘာကုိ သြားဖ်က္မလဲ၉ ဒါေၾကာင့္ lblResult မွာ

character အနည္းဆံုး တစ္ခုရွိသလား စစ္ရမယ္၉ စစ္နည္းကေတာ့ lblResult.Text ရဲ႕ length ကုိ

ၾကည့္မယ္၉ Length က 0 ထက္ မ်ားရင္ character အနည္းဆံုး ဿခုရွိလို႕ေပါ့ေနာ္၉ Length = 5

ဆိုရင္ lblResult.Text မွာ character ၃လံုး ရွိတယ္ဆိုတာ သိႏုိင္တယ္ေနာ္၉

If lblResult.Text.Length >0 Then

ဟုတ္ျပီ Length က 0 ထက္ ၾကီးတယ္ဆိုရင္ေတာ့ အဲဒီ character ေတြထဲက ေနာက္ဆံုး

character ကုိဖ်က္ေပးရမွာပါ၉

lblResult.Text ထဲက character ေတြဟာ position 0 ကစျပီး ဘယ္ကေန ညာကို

တစ္ခုစီတုိးသြားပါတယ္၉ ဥပမာ lblResult.Text မွာ 12345 ဆိုျပီး ရွိေနတယ္ ဆိုပါေတာ့၉ Positon 0

မွာ 1, position 1 မွာ 2, …, position 4 မွာ 5 ရွိေနမွာပါ၉

Backspace ကုိႏွိပ္တာဆိုရင္ lblResult ထဲက ေနာက္ဆံုး character ကိုဖ်က္ေပးရမယ္၉

ဒါဆိုရင္ position 0 ကစျပီး (မူလ character အေရအတြက္ -1) ကိုဘဲျဖတ္ယူလိုက္ရင္ရျပီေပါ့၉

မူလက lblResult မွာရွိေနမွာက 12345 ဆိုရင္

lblResult.Text.Length က 5 ျဖစ္မယ္ (character ၃ခုရွိလို႕)

SubString2 method က မူလ string ထဲကေန ကိုယ္လိုခ်င္တဲ့ string ကုိျဖတ္ယူတာ၉

SubString2(0, 4) ဆိုရင္ မူလ string ရဲ႕ position 0 ကစျပီး စုစုေပါင္း 4 ခုကိုဘဲျဖတ္ယူမယ္၉

344
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

lblResult.Text = lblResult.Text.SubString2 (0, lblResult.Text.Length - 1)

1234 12345 5

ဖ်က္ျပီးေနာက္ မဖ်က္မီ
4

အထက္ပါ ဥပမာမွာ lblResult.Text.Lenght က 5 ရမယ္၉ lblResult.Text.Lenght - 1

ဆိုေတာ့ 4 ျဖစ္သြားမယ္၉ lblResult.Text.SubString2 (0, 4) ဆုိေတာ့ 12345 ကို position 0

ကစျပီး 4 လံုးယူမယ္ဆိုေတာ့ 1234 ဘဲက်န္တယ္၉ အဲဒီ 1234 ကုိ lblResult ထဲ ထည့္လိုက္ေတာ့

12345 ကေန 1234 ျဖစ္သြားတယ္၉ ေနာက္ဆံုး character „5‟ ကို ဖ်က္ျပီးသားျဖစ္သြားတာပါဘဲ၉

Case "BS"

If lblResult.Text.Length >0 Then

lblResult.Text=lblResult.Text.SubString2 (0,lblResult.Text.Length - 1)

End If

Click ခံရတဲ့ Button ရဲ႕ Tag က BS မဟုတ္ဘူးဆိုရင္ေတာ့ numeric button

ေတြထဲကတစ္ခုခုဘဲေပါ့၉ Numeric button ကို click လုပ္ရင္ ဘာျဖစ္ေစခ်င္တာလဲ၉ အဲဒီ button ရဲ႕

value ကုိ lblResult.Text မွာေဖာ္ျပရမွာေပါ့၉ Calculator သံုးဘူးတယ္မုိ႕လား၉ 1 ကုိ click ရင္ Text

box မွာ 1 လုိ႕လာေပၚမယ္ေလ၉ အဲလိုလုပ္ခ်င္တာပါ၉ lblResult.Text မွာ မူလရွိေနတဲ့ Text အျပင္

Send.Tag ပါထည့္ေပးရမယ္၉ Send.Tag မွာရွိေနမွာက 0 to 9 ထဲက တစ္ခုခုေပါ့၉ ဥပမာ calculator

သံုးတဲ့အခါ 1 ကို ႏွိပ္ရင္ 1 ေပၚလာမယ္၉ အဲဒါက 1 ကို lblResult.Text ထဲထည့္လုိကလ


္ ုိ႕ပါ၉ ေနာက္

တစ္ခါ 2 ကုိႏွိပ္ရင္ 12 ဆိုျပီးျမင္ရမယ္ေနာ္၉ ဒါက lblResult.Text ထဲက မူလရွိေနတဲ့ 1 မွာေနာက္

ထပ္ရိုက္တဲ့ 2 ပါ ထပ္ထည့္လိုက္လို႕ပါ၉ & သေကၤတေလးကေတာ့ lblResult.Text နဲ႕ Send.Tag

ကိုတြဲစပ္ေပးလိုက္တာပါ၉

Case Else lblResult.Text = lblResult.Text & Send.Tag

345
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
lblResult ထဲမွာ character တစ္ခုမွမရွိေသးရင္ „0‟ Button ကုိသံုးစရာအေၾကာင္းမရွိလို႕

invisible လုပ္ထားပါ၉ (Number တစ္ခုကိုေရးတဲ့အခါ 1, 2, 3, …, 9 တစ္ခုခုနဲ႕သာ „စ‟ ေရးေလ့ရွိျပီး

„0‟ နဲ႕ „စ‟ ေရးေလ့မရွိတာေၾကာင့္ပါ) Character တစ္ခုခုရွိေနျပီဆိုရင္ေတာ့ „0‟ Button ကုိ visible

ျပန္လုပ္ပါ၉

If lblResult.Text.Length = 0 Then

btn0.Visible = False

Else

btn0.Visible = True

End If

Sub New

Label ရဲ႕ color ကုိ Designer ကေနသတ္မွတ္လို႕ရသလို code

ေရးျပီးလည္းသတ္မွတ္လို႕ရပါတယ္၉ lblComment ရဲ႕ color ကုိ ေအာက္ပါအတုိင္း

သတ္မွတ္ေပးလုိက္ပါ၉

lblComments.Color = Colors.RGB (255,235,128) (အှါေရာင္)

စစခ်င္းမွာ „0‟ Button ကုိႏွိပ္စရာမလိုတဲ့အတြက္ btn0 ကို invisible လုပ္ထားပါ၉

btn0.Visible = False

Sub CheckResult

ဒီ sub ထဲမွာေတာ့ lblComment ရဲ႕ color ကုိ အေျဖအမွား/အမွန္ေပၚမူတည္ျပီး အေျဖမွန္ရင္

အစိမ္းေရာင္၈ အေျဖမွားရင္ အနီေရာင္ျဖစ္ေအာင္ သတ္မွတ္ေပးလုိက္ပါ၉

lblComments.Color = Colors.RGB (128,255,128) အစိမ္းေရာင္

lblComments.Color = Colors.RGB (255,128,128) အနီေရာင္

346
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
အားလံုးျပီးရင္ run ၾကည့္ပါ၉ (ပံု ၆-၅၄) မွာျပထားတဲ့ပံုမ်ားအတုိင္း ျဖစ္ေနပါမယ္၉

စမ္းၾကည့္ၾကပါ၉

ပံု ၆-၅၄

8.3. Guess My Number

အခုေရးမယ့္ application ကေတာ့ user က number တစ္ခုကို EditText မွာ ရုိက္ထည့္ျပီး

button ကုိ click ရမယ္၉ User ထည့္လုိက္တဲ့ number ဟာ program မွာသတ္မွတ္ထားတဲ့ number

ထက္ငယ္သလား၈ ၾကီးသလား၈ တူတူဘဲလား ဆိုတာစစ္ျပီး toast message ထုတ္ျပမယ့္ application

ျဖစ္ပါတယ္၉

Basic4android ကို run ျပီး D:\GuessMyNumber ေအာက္မွာ Guess.b4a အမည္နဲ႕ save

လုပ္ပါ၉ (နမူနာျပျခင္းျဖစ္ပါတယ္၉ ၾကိဳက္ရာေနရာမွာ ၾကိဳက္ရာအမည္နဲ႕သိမ္းႏုိင္ပါတယ္)

(ပံု ၆-၅၆) မွာျပထားတဲ့ user interface layout တစ္ခု create လုပ္ပါ၉ Label, Button,

EditText တစ္ခုစီပါရမယ္၉ Layout file name ကုိ Layout1 လို႕ေပးပါ၉

347
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၆-၅၅

EditText1 ရဲ႕ Properties ပံု ၆-၅၆

lblNumber ရဲ႕ Properties ပံု ၆-၅၇

348
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

btnGuess ရဲ႕ Properties

ပံု ၆-၆ှ

Layout file ကုိ create လုပ္ျပီးတာနဲ႕ေတာ့ application ကို run ရင္ screen

မွာျမင္ရမွာမဟုတ္ေသးပါဘူး၉ Screen မွာျမင္ဘုိ႕အတြက္ layout file ကုိ Activity

ေပၚမွာတင္ေပးရပါမယ္၉ Code editor မွာေအာက္ပါအတုိင္းေရးပါ၉

Sub Activity_Create(FirstTime As Boolean)

Activity.LoadLayout("Layout1")

End Sub

အခုေန program ကုိ run လုိက္ရင္ screen မွာ layout ကုိ ျမင္ရမွာျဖစ္ပါတယ္၉ ဒါေပမယ့္

Button ကုိ click လုပ္ရင္ ဘာမွျဖစ္လာမွာ မဟုတ္ေသးပါဘူး၉ ဘာေၾကာင့္လည္းဆိုေတာ့ Button

349
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
click event ကို handle လုပ္ေပးမယ့္ code မေရးရေသးလိ႕ု ပါ၉ ဒါေၾကာင့္ Designer window မွာ

[Tools > Generate Members] ကို ေရြးျပီး btnGuess ရဲ႕ click event ကုိ check လုပ္ပါ၉ ျပီးရင္

„Generate members‟ button ကုိ ႏွိပ္ျပီး „Close‟ button ကုိ ဆက္ႏွိပ္ပါ၉ (ပံု ၆-၆ဿ)

ပံု ၆-၆ဿ

Code editor မွာ ေအာက္ပါအတုိင္း btnGuess_Click event sub frame တစ္ခု

ေရးျပီးျဖစ္ေနတာ ေတြ႕ရပါမယ္၉

Sub btnGuess_Click

End Sub

btnGuess ကို click လုပ္ရင္ ျဖစ္ေစခ်င္တာေတြကို အထက္ပါ btnGuess_Click event sub

ထဲမွာေရးေပးရပါမယ္၉ ဒါကိုမေရးခင္ user ရုိက္ထည့္တဲ့ number နဲ႕ တုိက္စစ္ဘုိ႕ number တစ္ခုကို

အရင္သတ္မွတ္ထားရမယ္၉ အဲဒီ number ကိုသိမ္းဘုိ႕ variable တစ္ခုကို declare လုပ္ေပးရမယ္၉

ဒါေၾကာင့္ Globals sub မွာ ေအာက္ပါအတုိင္း declare လုပ္ေပးပါ၉

Sub Globals

Dim MyNumber As Int

End Sub

350
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
MyNumber ထဲမွာ random number တစ္ခုထည့္ပါမယ္၉ Random number ဆိုတာ ကုိယ္

သတ္မွတ္ထားတဲ့ range အတြင္းက တစ္ခုခုျဖစ္မွာပါ၉ Random number ကုိ generate လုပ္ဘုိ႕

Rnd() ကုိ သံုးရပါမယ္၉ () ထဲမွာ parameter ၀ခု ေပးရပါမယ္၉ ပထမ parameter က ကိုယ္လိုခ်င္

တဲ့ number ရဲ႕ minimum value ကိုေပးရမွာ ျဖစ္ပါတယ္၉ ဒီ program မွာ 1 က 99 ထိ number

ေတြထဲက တစ္ခုခုကိုလိုခ်င္တာမုိ႕ ပထမ parameter မွာ 1 လို႕ ေပးပါတယ္၉ ဒုတိယ parameter

ကေတာ့ (လိုခ်င္တဲ့ number အေရအတြက္ + 1) ျဖစ္ပါတယ္၉ လိုခ်င္တဲ့ Number က 1 ကေန 99

ထိ ဆိုေတာ့ စုစုေပါင္း 99 လံုးျဖစ္ပါတယ္၉ ဒါေၾကာင့္ ဒုတိယ parameter မွာ 100 လို႕

ေရးေပးရပါမယ္၉

Rnd (1, 100)

Rnd ကုိ ေခၚလုိက္တဲ့အခါတုိင္း 1 ကေန 99 ထိ number ေတြထဲက တစ္ခုခုကို ရပါမယ္၉

တစ္ခါနဲ႕တစ္ခါ မတူပါဘူး၉ Rnd ကေန ထုတ္ေပးတဲ့ number ကုိ ေအာက္ပါအတုိင္း MyNumber ထဲ

ထည့္ေပးရပါမယ္၉

MyNumber = Rnd (1, 100)

Activity ကုိ create လုပ္တုိင္း random number ကို ရခ်င္တာမို႕ အထက္ပါ code ကုိ

Activity_Create မွာ ေရးပါ၉

Sub Activity_Create (FirstTime As Boolean)

Activity.LoadLayout ("Layout1")

MyNumber = Rnd (1, 100)

End Sub

ခုဆိုရင္ program ထဲမွာ random number တစ္ခထ


ု ုတ္ေပးဘုိ႕လုပ္ျပီးပါျပီ၉ User က

ရိုက္ထည့္မယ့္ number ကုိလက္ခံယူဘုိ႕လိုပါေသးတယ္၉ User က number ကုိ edtNumber မွာ

351
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ရိုက္ထည့္မွာပါ၉ ဒါေၾကာင့္ edtNumber ထဲက number ကုိ ယူရပါမယ္၉ edtNumber ကို code မွာ

ယူသံုးမယ္ဆိုရင္ declare လုပ္ေပးဘို႕လိုပါတယ္၉ ဒါေၾကာင့္ Globals မွာ ေအာက္ပါအတုိင္း declare

လုပ္ပါ၉

Dim edtNumber As EditText

EdtNumber မွာ number တစ္ခု ရုိက္ထည့္ျပီးတာနဲ႕ btnGuess ကုိ click ရမွာပါ၉ ဒါေၾကာင့္

btnGuess Click event sub မွာ လိုအပ္တဲ့ code ေတြ စေရးပါမယ္၉ edtNumber.Text ဟာ

MyNumber ထက္ ၾကီးသလားစစ္ပါ၉ ၾကီးရင္ “My number is smaller” ဆိုတဲ့ toast message ကုိ

ထုတ္ျပပါမယ္၉ Toast message ဆိုတာ screen မွာ ခဏသာေပၚလာျပီး အလိုလို ျပန္ေပ်ာက္သြား

မယ့္ message မ်ိဳး ျဖစ္ပါတယ္၉ ေရးနည္းကေတာ့ ToastMessageShow(“”,False) ပါ၉ “” ထဲမွာ

ကိုယ္ေပးခ်င္တဲ့ message ကုိ ေရးရပါမယ္၉ False ဆိုရင္ message ကုိ ခဏေလးသာျပျပီး True

ဆိုရင္ နည္းနည္းေလး ပိုၾကာၾကာျပမွာျဖစ္ပါတယ္၉

If edtNumber.Text > MyNumber Then

ToastMessageShow("My number is smaller.", True)

အထက္ပါ expression False ျဖစ္ရင္ edtNumber.Text ဟာ MyNumber ထက္

ငယ္သလား ထပ္စစ္ရမွာ ျဖစ္ပါတယ္၉

Else If edtNumber.Text < MyNumber Then

ToastMessageShow ("My number is larger.", True)

အထက္ပါ expression False ျဖစ္ရင္ေတာ့ edtNumber.Text ဟာ MyNumber နဲ႕

တူေနလို႕သာ ျဖစ္ပါတယ္၉ ဒါေၾကာင့္ ေအာက္ပါအတုိင္း message ထုတ္ျပရမွာ ျဖစ္ပါတယ္၉

Else

ToastMessageShow ("Well done.", True)

End If

352
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
EditText1 မွာ ေနာက္တစ္ၾကိမ္ထပ္ရိုက္ခါနီးရင္ အရင္ရိုက္ထားတဲ့ text ကုိ delete

လုပ္ရပါမယ္၉ အဲလိုမလုပ္ရေလေအာင္ edtNumber မွာရိုက္ထားတဲ့ text ကုိ select လုပ္ပါမယ္၉

ဒါမွသာ number တစ္လံုး စရိုက္လိုက္တာနဲ႕ edtNumber မွာ အရင္ရွိေနတဲ့ text ဟာ ပ်က္သြား

မွာပါ၉ (select လုပ္ထားတဲ့ text ေနရာမွာ ေနာက္ထပ္တစ္ခုခု ရိုက္လုိက္တာနဲ႕ select လုပ္ထားတဲ့

text ပ်က္သြားတာေတာ့ သိျပီးျဖစ္မွာပါ)

edtNumber.SelectAll

Complete Code

Sub Globals

Dim MyNumber As Int

Dim edtNumber As EditText

End Sub

Sub Activity_Create (FirstTime As Boolean)

Activity.LoadLayout ("Layout1")

MyNumber = Rnd (1, 100)

End Sub

Sub btnGuess_Click

If edtNumber.Text > MyNumber Then

ToastMessageShow ("My number is smaller.", False)

Else If edtNumber.Text < MyNumber Then

ToastMessageShow ("My number is larger.", False)

Else

353
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ToastMessageShow ("Well done.", True)

End If

edtNumber.SelectAll

End Sub

Program ကုိ run ရင္ (ပံု ၆-၆၀) အတုိင္း ျမင္ရမွာျဖစ္ပါတယ္၉ Number တစ္ခုျပီးတစ္ခု

ထည့္စမ္းၾကည့္ပါ၉

ပံု ၆-၆၀

354
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

Chapter 9

Frequently Asked Questions

မၾကာခဏေမးေလ့ရွိေသာေမးခြန္းမ်ား

9.1. "You should first save the projects." Message

Designer menu ကုိေရြးတဲ့အခါ (ပံု ၉-ဿ) မွာျပထားတဲ့ dialog box ေပၚလာတတ္ပါတယ္။

ပံု ၉-ဿ

အဲဒါကေတာ့ project ကုိ save မလုပ္ရေသးလုိ႕ပါ။ Save မလုပ္ရေသးရင္ „Files‟ folder

မရွိေသးဘူးေလ။ အဲဒီေတာ့ Designer မွာ create လုပ္မယ့္ layout file

ကုိသိမ္းဘုိ႕ေနရာမရွိျဖစ္ေနတာေပါ့။ ဒါေၾကာင့္ Designer ကိုဖြင့္တာနဲ႕ project ကိုအရင္ save

လုပ္ဘို႕ message ေပၚလာတာပါ။ [File > Save] ကိုေရြးျပီး save လုပ္လိုက္ရင္ရပါျပီ။

9.2. "Are you missing a library reference" Message

"Are you missing a library reference?" ဆုိတဲ့ message (ပံု ၉-၀) ေပၚလာတာကေတာ့

project မွာသံုးထားတဲ့ object အတြက္လိုအပ္ေသာ library ကိုမေရြးေပးထားလို႕ပါ။

355
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)

ပံု ၉-၀

Tabs area မွာ „Libs‟ tab ကုိေရြးျပီးလိုအပ္တဲ့ library ေတြကို check လုပ္ေပးလိုက္ပါ။ ဘယ္

object အတြက္ ဘယ္ library ကုိ check ရမယ္ဆိုတာမသိရင္

http://www.basic4ppc.com/android/documentation.html သို႕မဟုတ္

http://www.basic4ppc.com/android/wiki/index.php/Libraries မွာၾကည့္ပါ။ Page

ေအာက္ပိုင္းမွာ object type ေတြအမ်ားၾကီးေတြ႕မယ္။ ကိုယ္သိခ်င္တဲ့ object type ကိုေရြးလိုက္ရင္

သက္ဆုိင္ရာ library ကိုျမင္ရမယ္။

9.3. Library ကိုဘယ္လို load/update လုပ္မလဲ

Official library နဲ႕ additional library ေတြကို

http://www.basic4ppc.com/android/wiki/index.php/Libraries မွာရယူႏုိင္ပါတယ္။

အခ်ိဳ႕ library ေတြကေတာ့ Basic4Android license version ှယ္ယူထားသူ user

မ်ားအတြက္သာျဖစ္ပါတယ္။

Library ကို load လုပ္ခ်င္ရင္ သုိ႕မဟုတ္ version update လုပ္ခ်င္ရင္

ေအာက္ပါအဆင့္ေတြအတုိင္းလုပ္ပါ။

 Library zip file ကို download ဆြဲပါ။

 Unzip လုပ္ပါ။.jar ဖုိင္နဲ႕ .xml file ၀ခုရမယ္။

 ဒီဖုိင၀
္ ခုကို (Standard B4A library ဆိုရင္) C:\Program Files\Anywhere

Software\Basic4android\Libraries folder ထဲမွာထားပါ။ (Additional library ဆုိရင္)

C:\Program Files\Anywhere Software\Basic4android\Additional Library folder

356
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ထဲမွာထားပါ။

 „Libs‟ tab ကေနၾကည့္ပါ။ ထပ္ထည့္လုိက္တဲ့ library ကိုျမင္ရပါမယ္။

9.4. View ကိုဘယ္အခ်ိန္မွာinitialize လုပ္ဘို႕လုိျပီး၊ ဘယ္အခ်ိန္မွာမလုိဘူးလဲ

Designer ကေန „Add View‟ ကိုသံုးျပီးထည့္ထားတဲ့ view ေတြကို initialize

လုပ္စရာမလိုပါဘူး။ Code ေရးျပီးထည့္ထားတဲ့ view ဆိုရင္ေတာ့ initialize လုပ္ေပးရပါမယ္။

9.5. စာေၾကာင္းရွည္ရင္ခြဲေရးျခင္း

Code editor မွာစာေၾကာင္းရွည္ေနလို႕ တစ္လုိင္းထဲနဲ႕မဆန္႕ရင္ ခြဲေရးႏုိင္ပါတယ္။

ပထမစာေၾကာင္းအဆံုးမွာ space တစ္ခုျခားျပီး _ (Underscore) ထည့္ပါ။ ဥပမာ

Answ = Msgbox2 ("Do you want to quit the program", "A T T E N T I O N", "Yes", "",

"No", Null)

ကို ေအာက္ပါအတုိင္း ခြဲေရးရမယ္။

Answ = Msgbox2("Do you want to quit the program?", _

"A T T E N T I O N", "Yes", "", "No", Null)

9.6. Avoid closing an Application/capture Keycodes like Back/Menu

Back key ကုိႏွိပ္လုိက္တဲ့အခါ လက္ရွိ program ကေနတစ္ခါထဲတန္းမထြက္သြားဘဲ user

ကမွားျပီးႏွိပ္မိတာလား၊ တကယ္ႏွိပ္တာလားေသခ်ာေအာင္ထပ္ေမးႏိုင္ပါတယ္။

User က key တစ္ခုကိုႏွိပ္လုိက္ရင္ Activity_KeyPress event ျဖစ္ေပၚမယ္။ Event sub

ထဲမွာ ဘယ္ key ကုိႏွိပ္လုိက္သလည္းဆိုတာ အရင္စစ္ရမယ္။ User ႏွိ္ပ္လုိက္တဲ့ key ရဲ႕ key code

က „KeyCode‟ ဆိုတဲ့ variable ထဲကိုေရာက္ေနမွာပါ။ ဒါေၾကာင့္ KeyCode = Back key

လားဆိုတာေအာက္ပါအတုိင္းစစ္ရမယ္။

If KeyCode = KeyCodes.KEYCODE_BACK

357
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Back key ျဖစ္ေနရင္ "Do you want to quit the program?" ဆိုတဲ့ message box

တစ္ခုျပပါမယ္။ User က „No‟ ကုိေရြးရင္ program ထဲကမထြက္ခ်င္ေသးဘူး။ ဒါေၾကာင့္ True ကုိ

return ျပန္ျပီး program ကိုမပိတ္ဘဲဆက္ရွိေနေစပါတယ္။

Sub Activity_KeyPress (KeyCodeAsInt) As Boolean

Dim Answ As Int

If KeyCode = KeyCodes.KEYCODE_BACK Then

Answ = Msgbox2 ("Do you want to quit the program?", _

"A T T E N T I O N", "Yes", "", "No", Null)

If Answ = DialogResponse.NEGATIVEThen

Return True

End If

End If

Return False

End Sub

Activity_KeyPress event sub မွာ „Home‟ key ကုိႏွိပ္လိုက္သလားဆိုတာကိုေတာ့

စစ္လို႕မရပါဘူး။

9.7. Activity ထဲသို႕ Menu Item ထည့္ျခင္း

Activity ထဲကို Menu item ေတြထည့္တဲ့ Method ၀ခုရွိပါတယ္။

 AddMenuItem

 AddMenuItem2 (Icon ပါထည့္ရန္)

358
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
ဥပမာ

Activity.AddMenuItem ("Load", "mnuLoad")

Activity.AddMenuItem ("Save", "mnuSave")

အထက္ပါအတုိင္းေရးျပီး run တဲ့အခါ menu button ကုိႏွိပ္ရင္ (ပံု ၉-၁) မွာကဲ့သို႕ Menu

item ႏွစ္ခုျမင္ရပါမယ္။

ပံု ၉-၁

Menu item မွာ icon ထည့္ခ်င္ရင္ေအာက္ပါအတုိင္းေရးရပါမယ္။ Icon image မ်ားကို

(ဥပမာ Load.png နဲ႕ Save.png) ကို File manager ကေနထည့္ထားရပါမယ္။

Activity.AddMenuItem2("Load", "mnuLoad", LoadBitmap(File.DirAssets, "Load.png"))

Activity.AddMenuItem2("Save", "mnuSave", LoadBitmap(File.DirAssets, "Save.png"))

359
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
9.8. Designer ကေန View ကုိဘယ္လိုဖ်က္မလဲ

Designer မွာထည့္ျပီးသား view တစ္ခုကိုျပန္ဖ်က္ခ်င္ရင္ view ကုိ select လုပထ


္ ားပါ။

Designer မွာ [Tools > Remove Selected Views] ကိုေရြးပါ။ (ပံု ၉-၂)

ပံု ၉-၂

9.9. "Process has timeout" Message

"Process has timeout" message ခဏခဏေပၚေနရင္ timeout value ကုိေျပာင္းေပးပါ။

IDE မွာ [Tools > IDE Options > Configure Process Timeout] ကိုေရြးပါ။

ပံု ၉-၃

360
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Timeout value ကို မူလေပးထားတာထက္ ပိုျပီး ေပးလိုက္ပါ။ (ပံု ၉-၄)

ပံု ၉-၄

9.10. Project ထဲက layout file သို႕မဟုတ္ အျခားဖုိင္တစ္ခုကိုဘယ္လိုဖ်က္မလဲ

Project ထဲက မလိုတဲ့ layout file (xxx.bal) သုိ႕မဟုတ္ အျခားဖိုင္တစ္ခုခုကိုဖ်က္လိုပါက

Files tab ကေန ဖ်က္ခ်င္တဲ့ဖုိင္ကိုေရြးျပီး Remove button ကိႏ


ု ွိပ္ပါ။

9.11. Screen Orientation သတ္မွတ္ျခင္း

Screen orientation ကိုေအာက္ပါအတုိင္းသတ္မွတ္ႏုိင္ပါတယ္။ ဒီ orientation ဟာ project

မွာပါတဲ့ Activity အားလံုးအတြက္အက်ံဳးှင္ပါတယ္။

 Portrait: ေထာင္လိုက္ဘဲထားႏုိင္

 Landscape: အလ်ားလိုက္ဘဲထားႏုိင္

 Both: ႏွစ္မ်ိဳးလံုးေျပာင္းႏုိင္

Project ထဲက Activity တစ္ခုခ်င္းစီအတြက္ orientation တစ္မ်ိဳးစီသတ္မွတ္ခ်င္ရင္ေတာ့

Phone library သံုးျပီး code ေရးရမယ္။

 0 Landscape

 1 Portrait

 -1 Both

361
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub Process_Globals

Dim Phone1 As Phone

End Sub

Sub Activity_Create(FirstTimeAsBoolean)

Activity.LoadLayout("Layout1")

Phone1.SetScreenOrientation(0) [0, 1, -1 အနက္ အလိုရွိရာထည့္ပါ]

End Sub

9.12. Activity ကုိပိတ္ျခင္း

Actiivty1 ကေန Activity2 ကုိေခၚရင္ Activity2 ေပၚလာမယ္။ Activity2 မွာ Back key

ႏွိပ္ရင္ Activity1 ျပန္ေပၚမယ္။ Activity1 ကုိျပန္မေပၚေစခ်င္ရင္ Activity1 module မွာ

ေအာက္ပါအတုိင္းေရးပါ။

StartActivity (Activity2)

Activity1.Finish

9.13. Screenshot လုပ္ျခင္း

Device/emulator ရဲ႕ screenshot ကိုရယူရန္ေအာက္ပါ code ကိုသံုးႏုိင္ပါတယ္။

Reflection library ကိုအသံုးျပဳရမယ္။

Sub btnScrShot_LongClick

Dim Obj1, Obj2 As Reflector

Dim bmp As Bitmap

Dim c As Canvas

Dim now, i As Long

362
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Dim dt As String

DateTime.DateFormat = "yyMMddHHmmss"

now = DateTime.now

dt = DateTime.Date (now) ' e.g.: "110812150355" is Aug.12, 2011, 3:03:55 p.m.

Obj1.Target = Obj1.GetActivityBA

Obj1.Target = Obj1.GetField ("vg")

bmp.InitializeMutable (Activity.Width, Activity.Height)

c.Initialize2 (bmp)

Dim args (1) As Object

Dim types (1) As String

Obj2.Target = c

Obj2.Target = Obj2.GetField ("canvas")

Args (0) = Obj2.Target

types (0) = "android.graphics.Canvas"

Obj1.RunMethod4 ("draw", args, types)

Dim Out As OutputStream

Out = File.OpenOutput (File.DirRootExternal, dt & ".png", False)

bmp.WriteToStream (Out, 100, "PNG")

Out.Close

End Sub

9.14. Compile လုပ္ျပီးရင္ဖုိင္ေတြဘယ္မွာရွိမွာလဲ

Compile လုပ္ျပီးသြားရင္ *.apk file ဟာ Objects folder ေအာက္မွာရွိပါမယ္။ IDE ကုိ

device/emulator နဲ႕ခ်ိတ္ဆက္ျပီး compile လုပ္ရင္ apk file ကုိ device/emulator မွာ install

လုပ္ေပးပါတယ္။ apk name ဟာ project file ရဲ႕အမည္အတုင


ိ ္းျဖစ္ပါမယ္။ extension ကေတာ့

363
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
.apk ျဖစ္ပါတယ္္။ ဥပမာ project name ကုိ „GPSExample.b4a‟ လို႕ေပးထားရင္ apk name က

GPSExample.apk ျဖစ္ပါမယ္။

9.15. Array ကုိဘယ္လို pass လုပ္မလဲ

Array ကုိအျခား sub တစ္ခုထံေအာက္ပါအတုိင္း pass လုပ္ေပးလို႕ရပါတယ္။

Sub Test1

Dim one (1), two (1, 2), three (1, 2, 3) As String

Test2 (one, two, three)

End Sub

Sub Test2 (a () As String, b (,) As String, c (,,) As String) As String (,)

...

End Sub

Single string တစ္ခုကို return ျပန္လိုေသာ္

Sub Test2 (a () As String, b (,) As String, c (,,) As String) As String

1D String Array တစ္ခုကို return ျပန္္လိုေသာ္

Sub Test2 (a () As String, b (,) As String, c (,,) As String) As String ()

2D String Array တစ္ခုကို return ျပန္္လိုေသာ္

Sub Test2 (a () As String, b (,) As String, c (,,) As String) As String (,)

3D String Array တစ္ခုကို return ျပန္္လိုေသာ္

Sub Test2 (a () As String, b (,) As String, c (,,) As String) As String (,,)

364
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
9.16. Device ရဲ႕ Language နဲ႕ Country ရယူျခင္း

ဖုန္း settings မွာသတ္မွတ္ထားတဲ့ language နဲ႕ country ကုိရဘုိ႕ေအာက္ပါ code

ကုိေရးပါ။ GetDefaultLanguage sub ကုိေခၚျပီး returned value ကုိ Logs area မွာျပပါ။

Sub Activity_Create(FirstTimeAs Boolean)

Log(GetDefaultLanguage)

End Sub

Sub GetDefaultLanguage

Dim r As Reflector

r.Target = r.RunStaticMethod("java.util.Locale", "getDefault", Null, Null)

Return r.RunMethod("getDisplayName")

End Sub

GetDefaultLanguage sub ဟာ device ရဲ႕ language နဲ႕ country ကို return

ျပန္ေပးပါတယ္။ Reflection library ကိုသံုးဘို႕လိုမယ္။ နမူနာ Language နဲ႕ Country

ေတြကေအာက္ပါအတုိင္းျဖစ္ပါတယ္။

 English (United States)

 Deutsch (Österreich)

 français (Suisse)

9.17. ဘာေၾကာင့္ apk file name က result.apk ျဖစ္ရတာလဲ

Project file name ေပးရာမွာ space ပါခဲ့ရင္ result.apk ဖုိင္ရယ္လို႕ျဖစ္ေနပါလိမ့္မယ္။

ဥပမာ project file name ကုိ „GPS Example.b4a‟ လုိ႕ေပးခဲ့ရင္ ရရွိလာတဲ့ apk file name ဟာ

result.apk လို႕ျဖစ္ေနမွာပါ။

365
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
9.18. ဘာေၾကာင့္ apk file name ဟာ xxx_DEBUG.apk ျဖစ္ရတာလဲ

Project ျပီးဆံုးလို႕ apk file ကုိအသံုးျပဳေတာ့မယ္၊ အျခားသူေတြထံ ျဖန္႕ေှေတာ့မယ္

ဆိုရင္ေတာ့ IDE tool bar ရဲ႕ debug mode မွာ Release/Release (obfuscated) ကိုေရြးျပီး run

ရမယ္။ Debug (legacy)/Debug (rapid) နဲ႕ run ရင္ေတာ့ apk file က xxx_DEBUG.apk

ရယ္လုိ႕ျဖစ္ေနမယ္။

ပံု ၉-၅

9.19. Select True/Case Trick

Select – Case အသံုးျပဳပံုတစ္မ်ိဳးပါ။

i = 10

Select True

Case (i < 9)

Log ("False")

Case (i = 10)

Log ("True")

End Select

9.20. Array ထဲမွာ random numberေတြမထပ္ေအာင္ထည့္ျခင္း

Sub Globals
Dim numbers (10) As Int
End Sub

366
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub Activity_Create (FirstTime As Boolean)
For i = 0 To 9
numbers (i) = i + 1
Next
ShuffleArray (numbers)
For i = 0 To 9
Log (numbers (i))
Next
End Sub

Sub ShuffleArray (arr () As Int)


Dim i As Int
For i = arr.Length - 1 To 0 Step -1
Dim j, k As Int
j = Rnd (0, i + 1)
k = arr (j)
arr (j) = arr (i)
arr (i) = k
Next
End Sub

9.21. Detect Screen Orientation

Screen orientation ကုိေအာက္ပါ code သံုးျပီးသိႏုိင္ပါတယ္။ Activity ရဲ႕ width နဲ႕ height

ကိုႏိႈင္းယွဥ္တာပါဘဲ။ Width က height ထက္မ်ားေနရင္ landscape ေပါ့။ မဟုတ္ရင္ portrait ပါ။

Sub Activity_Create (FirstTime As Boolean)

If Activity.Width > Activity.Height Then

367
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Log ("Landscape")

Else

Log ("Portait")

End If

End Sub

9.22. Calling the Internal Calculator

Device တုိင္းလိုလိုမွာ calculator application ပါျပီးျဖစ္ပါတယ္။ အဲဒီ application ကုိ ကိုယ့္

application ကေနလွမ္းေခၚသံုးဘို႕ ေအာက္ပါ code ကုိသံုးပါ။

Sub Calculator

Dim i As Intent

i.Initialize ("", "")

i.SetComponent ("com.android.calculator2/.Calculator")

Try

StartActivity (i)

Catch

ToastMessageShow ("Calculator app not found.", True)

End Try

End Sub

အခ်ိဳ႕ device ေတြမွာ calculator ရဲ႕ Action name ကိုေျပာင္းထားေလ့ရွိပါတယ္။

ဘယ္ေလာက္ဘဲေျပာင္းေျပာင္း package name မွာ calc ဆိုတာေလာက္ေတာ့ပါမွာဘဲေလ။

အဲဒါမ်ိဳးအတြက္ေတာ့ေအာက္ပါ code နဲ႕အဆင္ေျပပါတယ္။ Phone library ေတာ့သံုးရမယ္။

Device မွာ install လုပ္ထားတဲ့ package ေတြကရ


ို ယူျပီး Packages မွာထည့္ပါ။

Packages = Pm.GetInstalledPackages

368
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
calc စာလံုးပါတဲ့ package name ကိုရွာပါ။ ေတြ႕ရင္ သူ႕ရဲ႕ Intent ကုိသံုးျပီး calculator

application ကုိေခၚပါ။

If st.Contains ("calc") =True Then

Inte=Pm.GetApplicationIntent (st)

If Inte.IsInitialized Then

StartActivity (Inte)

Code အျပည့္အစံုကေအာက္ပါအတုိင္းျဖစ္ပါတယ္။

Sub Calculator

Dim Pm As PackageManager

Dim Inte As Intent

Dim Packages As List

Dim st As String

Packages = Pm.GetInstalledPackages

For i = 0 To Packages.size - 1

st = Packages.Get (i)

If st.Contains ("calc") =True Then

Inte = Pm.GetApplicationIntent (st)

If Inte.IsInitialized Then

StartActivity (Inte)

Exit

End If

End If

Next

End Sub

369
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
9.23. Alpha/Red/Green/Blue

အေရာင္တစ္ခုမွာ component ၂မ်ိဳးရွိပါတယ္။

 Alpha

 Red

 Green

 Blue

Alpha/Red/Green/Blue value ေတြကိုေအာက္ပါအတုိင္းရယူႏုိင္ပါတယ္။

Sub Activity_Create (FirstTimeAs Boolean)

Dim argb () As Int

argb = GetARGB (Colors.Transparent)

Log ("A = " & argb (0))

Log ("R = " & argb (1))

Log ("G = " & argb (2))

Log ("B = " & argb (3))

End Sub

Sub GetARGB (Color As Int) As Int ()

Dim res (4) As Int

res (0) = Bit.UnsignedShiftRight (Bit.And (Color, 0xff000000), 24)

res (1) = Bit.UnsignedShiftRight (Bit.And (Color, 0xff0000), 16)

res (2) = Bit.UnsignedShiftRight (Bit.And (Color, 0xff00), 8)

res (3) = Bit.And (Color, 0xff)

Return res

End Sub

370
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
9.24. Devcie အမ်ိဳးအစားရယူျခင္း

Device ရဲ႕ အမ်ိဳးအစားကိုသိရန္ေအာက္ပါ code ကိုသံုးပါ။ Device ရဲ႕ physical size

ကုိၾကည့္ျပီး tablet လား၊ ဖုန္းလားခြဲျခားႏုိင္ပါတယ္။

Sub Activity_Create (FirstTime As Boolean)

If GetDevicePhysicalSize > 6 Then

Log (“7 inches or 10 inches tablet”)

Else

Log (“phone”)

End If

End Sub

Sub GetDevicePhysicalSize As Float

Dim lv As LayoutValues

lv = GetDeviceLayoutValues

Return Sqrt (Power (lv.Height, 2) + Power (lv.Width, 2)) / lv.Scale / 160

End Sub

9.25. Generate a Click Event

သာမန္အားျဖင့္ view တစ္ခုကို click ရင္ click event ျဖစ္ေပၚပါတယ္။ ဥပမာ Activity

ေပၚမွာ လြတ္ေနတဲ့ေနရာတစ္ခုကို click ရင္ Activity Click event ျဖစ္ေပၚပါတယ္။

ေအာက္ပါ code မွာေတာ့ Activity ကုိ click ရင္ Spinner Click event ျဖစ္ေပၚေအာင္

ဘယ္လိုလုပ္ရမယ္ဆိုတာျပထားပါတယ္။

371
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Globals

Spinner object „sp‟ ကို declare လုပ္ပါ။

Sub Globals

Dim sp As Spinner

End Sub

Activity_Create

„sp‟ initialize လုပ္ျပီး item „a‟, „b‟, „c‟, „d‟ တုိ႕ကိုထည့္ထားပါ။ ျပီိးရင္ Spinner object „sp‟

ကို Activity ေပၚတင္ပါ။

Sub Activity_Create (FirstTimeAs Boolean)

sp.Initialize ("sp")

sp.AddAll (Array As String ("a", "b", "c", "d"))

Activity.AddView (sp, 10dip, 10dip, 200dip, 50dip)

End Sub

Activity_Click

Activity_Click event sub မွာ OpenSpinner sub ကုိေခၚျပီး „sp‟ ကုိ pass လုပ္ပါ။

Sub Activity_Click

OpenSpinner (sp)

End Sub

372
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
OpenSpinner

OpenSpinner sub မွာ Reflector object „r‟ ကို declare လုပ္ျပီး spinner objet „s‟ ကုိ „r‟

ရဲ႕ target အျဖစ္သတ္မွတ္ပါ။ ျပီးရင္ RunMethod ကုိေခၚျပီး „perFormClick‟ ကုိ pass

လုပ္ေပးလိုက္ပါ။ အဲဒါဆိုရင္ Spinner Click event ျဖစ္ေပၚပါလိမ့္မယ္။ Program ကုိ run ျပီး

Activity မွာလြတ္တဲ့ေနရာကို click ပါ။ Spinner Click event ျဖစ္ေပၚျပီး Spinner ထဲမွာထည့္ထားတဲ့

item „a‟, „b‟, „c‟, „d‟ တုိ႕ကိုျမင္ရပါမယ္။

Sub OpenSpinner (s As Spinner)

Dim r As Reflector

r.Target = s

r.RunMethod ("performClick")

End Sub

9.26. "Out of memory" Error/Bitmap

တစ္ခါတေလမွာ "Out of memory" message ေပၚလာျပီး program ရပ္သြားတတ္ပါတယ္။

အဲဒါကေတာ့ memory ေပၚမွာ bitmap အၾကီးၾကီးေတြရွိေနတာမ်ိဳး (သို႕မဟုတ)္ bitmap ေတြ

အရမ္းမ်ားေနတာမ်ိဳးေၾကာင့္ ျဖစ္တတ္ပါတယ္။ ေအာက္ပါ code ကုိသံုးျပီး bitmap

အၾကီးၾကီးေတြကိုဖယ္ရွားကာ memory မွာေနရာလြတ္ေအာင္ေဆာင္ရြက္ႏုိင္ပါတယ္။

Dim Obj1 As Reflector [Reflector object တစ္ခုကို declare လုပ္ပါတယ္]

Obj1.Target = bmp [bmp ဆိုတာဖယ္ရွားခ်င္တဲ့ Bitmap ပါ]

Obj1.RunMethod ("recycle") [recycle method ကုိသံုးျပီး Bitmap ကုိဖ်က္ပါတယ္]

တကယ္လုိ႕ bitmap က canvas ေပၚမွာရွိေနရင္ေတာ့

Obj1.Target = canv [Object ရဲ႕ target ကုိ canv လို႕ထားပါတယ္]

Obj1.Target = Obj1.GetField ("bw")

373
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
getObject method ကိုေခၚျပီး result ကို Obj1 ရဲ႕ target အျဖစ္သတ္မွတ္ပါတယ္။

Obj1.Target = Obj1.RunMethod("getObject")

Obj1.RunMethod("recycle") [recycle method ကုိသံုးျပီးဖ်က္ပါတယ္]

9.27. ScrollView မွ Scrollbar ကုိျဖဳတ္ျခင္း

ScrollViewမွာ ပါတဲ့ scrollbar ကုိဖ်က္ရန္ ေအာက္ပါ code ကုိသံုးပါ။ Reflection library

ကိုသုံးရမယ္။

Dim r As Reflector

r.Target = ScrollView1

r.RunMethod2 ("setVerticalScrollBarEnabled", False, "java.lang.boolean")

9.28. Directory ရွိမရွိစစ္ျခင္း

Directory ရွ၊ိ မရွိစစ္ျပီး မရွိပါက directory ျပဳလုပ္ရန္ ေအာက္ပါ code ကုိသုံးပါ။

DirRootExternal မွာ Images ဆိုတဲ့ directory ရွိမရွိ အရင္စစ္ပါတယ္။ မရွိရင္ DirRootExternal မွာ

Images ဆိုတဲ့ directory ကို create လုပ္ေပးပါတယ္။

Dim MyDirctory As String

MyDirctory = File.DirRootExternal & "/Images"

If File.Exists (MyDirctory, "") = False Then

File.MakeDir (File.DirRootExternal, "Images")

End If

374
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
9.29. Full Screenျဖစ္ေစရန္ Code ျဖင့္ျပဳလုပ္ျခင္း

Sub FullScreen (Active As Boolean, ActivityName As String)

Dim obj1 As Reflector

Dim i As Int

i = 1024 'FLAG_FULLSCREEN

obj1.Target = obj1.GetMostCurrent (ActivityName)

obj1.Target = obj1.RunMethod ("getWindow")

If Active Then

obj1.RunMethod2 ("addFlags", i, "java.lang.int")

Else

obj1.RunMethod2 ("clearFlags", i, "java.lang.int")

End If

End Sub

Active = True ဆိုရင္ full screen ျဖစ္မယ္။

Active = False ဆိုရင္ status bar ကိုျပမယ္။

9.30. EditText ရဲ႕ Input Mode ကုိေျပာင္းျခင္း

EditText ဆိုတာ data ေတြရိုက္ထည့္ရမယ့္ view ပါ။ ဘယ္လို data အမ်ိဳးအစား

ရိုက္ထည့္ႏုိင္တယ္ဆိုတာ သတ္မွတ္ေပးႏုင
ိ ္တယ္။

 INPUT_TYPE_NONE (စာရိုက္ခြင့္မျပဳ)

 INPUT_TYPE_NUMBERS (Integer number ေတြသာရိုက္ခြင့္ျပဳ)

 INPUT_TYPE_DECIMAL_NUMBERS (Decimal number ေတြသာရိုက္ခြင့္ျပဳ)

 INPUT_TYPE_TEXT (မည္သည့္ text မဆိုရိုက္ခြင့္ျပဳ)

375
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
 INPUT_TYPE_PHONE (Phone number ေတြသာရိုက္ခင
ြ ့္ျပဳ)

ဥပမာ

EditText1.InputType = EditText1.INPUT_TYPE_TEXT

9.31. ေနာက္ဆံုး modify လုပခ


္ ဲ့တဲ့ အခ်ိန္အရ ဖိုင္မ်ားကိုစီျခင္း

ေနာက္ဆံုး modify လုပ္ခဲ့တဲ့အခ်ိန္အရ ဖုိင္ေတြကိုစီႏိုင္ပါတယ္။

Sub Process_Globals

Type FileAndTime (Name As String, Time As Long)

End Sub

Sub Globals

End Sub

Sub Activity_Create (FirstTime As Boolean)

Dim files As List

files = ListFilesByDate (File.DirRootExternal)

For i = 0 To files.Size - 1

Dim fs As FileAndTime

fs = files.Get(i)

Log (fs.Name & ": " & DateTime.Date (fs.Time))

Next

End Sub

376
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Sub ListFilesByDate (Folder As String) As List

Dim files As List

files = File.ListFiles (Folder)

Dim sortedFiles As List

sortedFiles.Initialize

For i = 0 To files.Size - 1

Dim fs As FileAndTime

fs.Name = files.Get (i)

fs.Time = File.LastModified (Folder, fs.Name)

sortedFiles.Add (fs)

Next

sortedFiles.SortType ("Time", False)

Return sortedFiles

End Sub

9.32. Device ရဲ႕ dpi value ကုိရယူျခင္း

Device ရဲ႕ dpi value ကုိရယူရန္ Reflection library ကိုသံုးဘုိ႕လိုပါတယ္။ dpi ဆုတ
ိ ာ dots

per inch ပါ။ Device ရဲ႕ တစ္လက္မမွာရွိတဲ့ pixel အေရအတြက္ေပါ့။ 160 dpi ဆိုရင္ တစ္လမမွာ

pixel 160 ရွိမွာပါ။

Dim Xdpi, Ydpi As Float

Dim r As Reflector

r.Target = r.GetContext

r.Target = r.RunMethod ("getResources")

r.Target = r.RunMethod ("getDisplayMetrics")

377
ထားထားေစာလြင္ Basic4android ျဖင့္ Android Application ေရးနည္း (ဿ)
Xdpi = r.GetField ("xdpi")

Ydpi = r.GetField ("ydpi")

GetField method ကိုသံုးျပီး ေအာက္ပါ data ေတြကိုရယူႏုိင္ပါတယ္။

 density: Screen density

 densityDpi: Screen density (dpi unit)

 heightPixels: Screen ရဲ႕ေထာင္လုိက္ pixel အေရအတြက။္

 widthPixels: Screen ရဲ႕အလ်ားလုိက္ pixel အေရအတြက။္

 scaledDisplay: Font အတြက္ scaling factor

 xdpi: Screen အလ်ားလိုက္ တစ္လမ မွာရွိတဲ့ pixel အေရအတြက္

 ydpi: Screen ေထာင္လိုက္ တစ္လမ မွာရွိတဲ့ pixel အေရအတြက္

9.33. Java Program Line ကုိရွာျခင္း

တစ္ခါတေလမွာ programကုိ run ရင္ java execution error message

ေပၚလာတတ္ပါတယ္။ Java line number ဘယ္ေလာက္မွာ error ရွိတယ္ဆိုတာမ်ိဳးေပါ့။ အဲဒီ java

line ကုိရွာခ်င္ရင္

 Objects\src\packagenameမွာ activity.java file ကိုရွာပါ။

 Line number ကုိေဖာ္ျပေပးႏုိင္တဲ့ text editor တစ္ခုခု (ဥပမာ Notepad++) မွာ

အဲဒီဖုိင္ကိုဖြင့္ပါ။

 Error message မွာျပတဲ့ line number ကုိရွာပါ။ ျပသနာျဖစ္ေစတဲ့ code ကုိေတြ႕ပါလိမ့္မယ္။

Reference: Basic4android Beginner‟s Guide

378

You might also like