فيديو المحاضرة
الفيديو قيد الإعداد
الفيديو قيد الإعداد. في الأثناء اقرأ المحاضرة المكتوبة وجرّب الأمثلة تحت.
رسوم متحركة
أمثلة كود
أمثلة قصيرة وبسيطة
كل مثال وراه زرار «شغّل خطوة بخطوة»: بيعلّم على السطر اللي بيتنفّذ ويطبع الناتج زي ما بيتنفّذ بالظبط. ده شرح تنفيذ الكود سطر بسطر، مش مترجم حقيقي جوه الصفحة.
المثال ١: دالة بترجّع قيمة
int add(int a, int b) { return a + b;}int total = add(3, 4);Log.d("MainActivity", "total = " + total);
المثال ٢: مستويات الـ Log
Log.d("MainActivity", "debug: onCreate started");Log.i("MainActivity", "info: layout loaded");Log.w("MainActivity", "warning: no name yet");Log.e("MainActivity", "error: file not found");
المثال ٣: الزرار بينادي دالة
Button top = findViewById(R.id.buttonTop);top.setOnClickListener(v -> topClick()); void topClick() { Log.d("MainActivity", "top clicked"); Toast.makeText(this, "Top", Toast.LENGTH_SHORT).show();}
اختبار قصير
اختبر فهمك
1. void in a method signature means:
2. Which line shows a small pop-up to the user?
3. Logcat is used to:
4. A NullPointerException on a view usually means:
5. setOnClickListener is used to:
تمارين
جرّب بإيدك
التمرين 1
اكتب دالة multiply بترجّع حاصل ضرب رقمين، واطبع ناتج 6 في 7.
Write a multiply method returning the product of two numbers, and log 6 times 7.
Starter code:
// TODO: multiply method and one Log line
int multiply(int a, int b) {
return a * b;
}
Log.d("MainActivity", "result = " + multiply(6, 7));
Expected output
D/MainActivity: result = 42
التمرين 2
اكتب دالة bottomClick بتطبع سطر في الـ Log وتظهر Toast، واربطها بالزرار buttonBottom.
Write a bottomClick method that logs a line and shows a Toast, and wire it to buttonBottom.
Starter code:
Button bottom = findViewById(R.id.buttonBottom);// TODO: set the click listener// TODO: write bottomClick
Button bottom = findViewById(R.id.buttonBottom);
bottom.setOnClickListener(v -> bottomClick());
void bottomClick() {
Log.d("MainActivity", "bottom clicked");
Toast.makeText(this, "Bottom", Toast.LENGTH_SHORT).show();
}
Expected output
D/MainActivity: bottom clicked Toast: Bottom
مشروع صغير في آخر المحاضرة
آلة حاسبة صغيرة في الـ Log
الداتا: calc: our own two numbers (6 and 7), no user data.
License / source: Our own code; Log reference by Google (link only). Reference page
الخطوات
- اكتب دالتين: add و multiply، كل واحدة بتاخد رقمين وبترجّع الناتج.
- في onCreate نادي الدالتين على 6 و 7.
- اطبع الناتجين في الـ Log بـ tag اسمه Calc.
- شغّل التطبيق وبص على Logcat. المتوقع تحت.
Starter code:
int add(int a, int b) { // TODO: return the sum return 0;}int multiply(int a, int b) { // TODO: return the product return 0;}// TODO: log add(6, 7) and multiply(6, 7) with tag "Calc"
int add(int a, int b) {
return a + b;
}
int multiply(int a, int b) {
return a * b;
}
Log.d("Calc", "add = " + add(6, 7));
Log.d("Calc", "multiply = " + multiply(6, 7));
Expected output
D/Calc: add = 13 D/Calc: multiply = 42
مشروع نهاية الفصل
ملف المشروع العملي للفصل 3
ملف PDF فيه مشروع صغير بأسئلة عملية تطبّق بيها كل اللي اتعلمته في الفصل، مع حل نموذجي في الآخر.
Credits: the topic order follows the course notes "Mobile Application Development 1" by Dr. Muhammad Kashif Saeed (Applied College, Muhayil, King Khalid University), shared by the course owner. The explanations, diagrams, data and code in this lecture are our own; we copy nothing from the notes. Extra reference: Android developer documentation: layouts, views and the activity life cycle (developer.android.com).