Tải bản đầy đủ (.pdf) (133 trang)

Báo cáo Thực hành Lập trình di động bằng Android Studio

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (3.68 MB, 133 trang )

TABLE OF CONTENT
CHAPTER I. INTRODUCTION TO ANDROID STUDIO ............................................... 6
1. Android Studio ............................................................................................................. 6
1.1. Download Android Studio for Window ................................................................ 6
1.2. Install Android Studio ............................................................................................ 7
2. Components in an Android project ............................................................................ 13
2.1. Activities .............................................................................................................. 13
2.2. Service ................................................................................................................. 14
2.3. Content Providers ................................................................................................ 14
2.4. Broadcast Receivers ............................................................................................. 15
2.5. Additional Components ....................................................................................... 15
3. Folders and files in a project ...................................................................................... 15
3.1. Manifests folder ................................................................................................... 16
3.2. Java folder ............................................................................................................ 16
3.3. Resources folder .................................................................................................. 17
3.4. Gradle scripts ....................................................................................................... 17
3.5. The Android Project View ................................................................................... 17
4. Create an Android project .......................................................................................... 18
4.1. Configuration ....................................................................................................... 18
4.2. Run the application .............................................................................................. 22
4.3. My First App ........................................................................................................ 25
CHAPTER II. LAYOUT AND MEASUREMENTS ....................................................... 28
1. Android Layouts......................................................................................................... 28
1.1. What are layouts in an android application?........................................................ 28
1.2. Common layouts .................................................................................................. 29
2. Measurements ............................................................................................................ 31
2.1. Dots per inch ........................................................................................................ 32
2.2. Density independence in Android ....................................................................... 32
3. Design a Simple Calculator ........................................................................................ 33
CHAPTER III. INTERACTIVE COMPONENTS AND EVENT HANDLING ............. 39
1. Basic Components ...................................................................................................... 39


2. Android UI Controls .................................................................................................. 39
2.1. TextView ............................................................................................................. 39
2.2. EditText ............................................................................................................... 40


2.3. Button................................................................................................................... 40
2.4. RadioButton ......................................................................................................... 40
2.5. Checkbox ............................................................................................................. 40
2.6. Spinner ................................................................................................................. 40
2.7. TimePicker ........................................................................................................... 40
2.8. DatePicker ............................................................................................................ 41
2.9. Toast..................................................................................................................... 41
3. Event Handling ........................................................................................................... 41
3.1. Event listeners ...................................................................................................... 41
3.2. Event handlers...................................................................................................... 41
3.3. Event listeners registration .................................................................................. 42
4. Design a simple app to perform Event handling ........................................................ 42
4.1. Design .................................................................................................................. 42
4.2. Event handling ..................................................................................................... 47
5. Design a simple app using Radio Button, Checkbox & Toast ................................... 48
5.1. Design .................................................................................................................. 48
5.2. Event Handling .................................................................................................... 51
6. Program a Calculator Application .............................................................................. 52
CHAPTER IV. DESIGN AND CUSTOMIZE UI ............................................................ 56
1. Listview ...................................................................................................................... 56
2. Custom listview .......................................................................................................... 56
3. Design a simple app using list view ........................................................................... 56
3.1. Design .................................................................................................................. 56
3.2. Event handling ..................................................................................................... 59
4. CTU form ................................................................................................................... 60

4.1. Design .................................................................................................................. 61
4.2. Event handling ..................................................................................................... 68
CHAPTER V. DATABASE MANAGEMENT – SQLITE .............................................. 74
1. SQLite in Android ...................................................................................................... 74
2. Design a simple app using SQLite ............................................................................. 75
2.1. Design .................................................................................................................. 75
2.2. Event handling ..................................................................................................... 82
3. Reproduce application ................................................................................................ 87
3.1. Design .................................................................................................................. 88
2


3.2. Event l intent = Intent(this, NewCustomerActivity::class.java)
this.startActivity(intent)
}

128


// ADD NEW RECORD
findViewById<Button>(R.id.btn_add_rec_atv).setOnClickListener {
val intent = Intent(this, NewRecordActivity::class.java)
this.startActivity(intent)
}
listView.setOnItemClickListener { adaterView, view, position, id ->
val itemPos = adaterView.getItemAtPosition(position)
val tmp = adaterView.getItemIdAtPosition(position)
Log.d("Clicked: ", itemPos.toString() + " " + tmp.toString())
var intent : Intent? = null
if (displayRec == true)

intent = Intent(this, RecordActivity::class.java).apply {
putExtra(REC_ID, itemPos.toString())
}
else
intent = Intent(this, CustomerActivity::class.java).apply {
putExtra(CUS_ID, itemPos.toString())
}
this.startActivity(intent)
}
}
fun viewCustomers(view: View) {
val databaseHandler = DatabaseHandler(this)
val customers: List<CustomerModel> = databaseHandler.viewCustomers()
val cusIds = Array<Int>(customers.size) {-1}
val cusNames = Array<String>(customers.size) {"null"}
var index = 0
for (customer in customers) {
cusIds[index] = customer.cusId
cusNames[index] = customer.cusName
++index
}
val customerAdapter = CustomerAdapter(this, cusIds, cusNames)
listView.adapter = customerAdapter
}
fun viewRecords(view: View) {
val databaseHandler = DatabaseHandler(this)
val records: List<RecordModel> = databaseHandler.viewRecords()
val recIds = Array<Int>(records.size) {-1}
val cusNames = Array<String>(records.size) {"null"}
val days = Array<String>(records.size) {"null"}

var index = 0
for (record in records) {
recIds[index] = record.recId
cusNames[index] = databaseHandler.findCustomerName(record.cusId)
days[index] = record.day
++index
}
val recordAdapter = RecordAdapter(this, recIds, cusNames, days)
listView.adapter = recordAdapter
}
}

129


4.2.11. maninfests/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=" />package="com.example.electricalrecordingapp">
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ElectricalRecordingApp">
android:parentActivityName=".MainActivity">
android:value=".MainActivity"/>
</activity>

android:parentActivityName=".MainActivity">
android:value=".MainActivity"/>
</activity>
android:parentActivityName=".MainActivity">
android:value=".MainActivity"/>
</activity>
android:parentActivityName=".MainActivity">
android:value=".MainActivity" />
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

CHAPTER VI. GOOGLE FIREBASE
1. GOOGLE FIREBASE
Firebase is a mobile application development platform from Google with powerful features
for developing, handling, and enhancing applications.
Firebase is fundamentally a collection of tools developers can rely on, creating applications
and expanding them based on demand.

Firebase aims to solve three main problems for developers:
 Build an app, fast
 Release and monitor an app with confidence
 Engage users
Developers relying on this platform get access to services that they would have to develop
themselves, and it enables them to lay focus on delivering robust application experiences.
130


Some of the Google Firebase platform’s standout features include databases,
authentication, push messages, analytics, file storage, and much more.
Since the services are cloud-hosted, developers can smoothly perform on-demand scaling
without any hassle. Firebase is currently among the top app development platforms relied
upon by developers across the globe [14].

131


REFERENCES
[1] "Android - Application Components," Turtorialspoint, 2021. [Online]. Available:
/>[Accessed 2 2021].
[2] "Project Overview | Android Developers," 2021. [Online].
[Accessed 2 2021].

Available:

[3] Praveenruhil, "Androi Project Folder Structure," 5 3 2021. [Online]. Available:
/>[Accessed
3
2021].

[4] A. Chugh, "Android Studio Project Structure, Compiler, ProGuard," Joural Dev,
2016. [Online]. Available: [Accessed 2 2021].
[5] Treehouse, "How to Make an Android App for Beginners," Youtube, 21 4 2018.
[Online]. Available: [Accessed 2 2020].
[6] "Creat an Android project | Android Developers," [Online]. Available:
[Accessed 2
2021].
[7] "Android Layout and Views – Types and Examples," DataFlair , 2020. [Online].
Available: ining/blogs/android-layout-and-views/. [Accessed 3
2021].
[8] "FrameLayout
|
Android
Devlopers,"
[Online].
Available:
[Accessed 3
2021].
[9] "Dots per inch - Wikipedia," Wikipedia, 23 4 2021. [Online]. Available:
[Accessed 5 2021].
[10] "Top Android UI Controls that you must know about!," Data Flair, [Online].
Available: ining/blogs/android-ui-controls/. [Accessed 4 2021].
[11] "Android Event Handling – Manage the Action of Users Interaction," Data Flair,
[Online].
Available:
ining/blogs/android-event-handling/.
[Accessed 4 2021].
[12] "Android Custom ListView (Adding Images, sub-title)," javaTpoint, [Online].
Available: [Accessed 5 2021].
[13] "Android

SQLite
Tutorial,"
javaTpoint,
[Online].
[Accessed 5 2021].

Available:

[14] G. Batschinski, "What is Firebase? All secrets unlocked," 2020. [Online]. Available:
[Accessed 5 2021].
132


[15] "Android Application Components with Implementation & Examples," Data Flair,
2021. [Online]. Available: ining/blogs/android-applicationcomponents/. [Accessed 2 20121].
[16] "Developer Guides | Android Developers," 2021.
[Accessed 2 2021].

[Online].

Available:

[17] Praveenruhil, "Android UI Layouts," GeeksforGeeks, 11 11 2019. [Online].
Available: [Accessed 3 2020].
[18] "ConstraintLayout Tutorial for Android: Complex Layouts," 2021. [Online].
Available:
[Accessed 3 2021].

133




×