كل المقررات

الرئيسية / مقرر تطبيقات الموبايل / الفصل 2 الجزء 2

الفصل 2 (الجزء 2): App components and Android Studio

The building blocks, the project folders and the manifest

على طول: مكونات التطبيق، وبعدها الأدوات والمجلدات اللي هتشتغل فيها.

فيديو المحاضرة

فيديو المحاضرة

بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.

رسوم متحركة

مكونات تطبيق Android

أمثلة كود

أمثلة قصيرة وبسيطة

كل مثال وراه زرار «شغّل خطوة بخطوة»: بيعلّم على السطر اللي بيتنفّذ ويطبع الناتج زي ما بيتنفّذ بالظبط. ده شرح تنفيذ الكود سطر بسطر، مش مترجم حقيقي جوه الصفحة.

المثال ١: المانيفست بيعلن الـ Activity

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.greetingcard">  <uses-permission android:name="android.permission.INTERNET" />  <application android:label="Greeting Card">    <activity android:name=".MainActivity" android:exported="true">      <intent-filter>        <action android:name="android.intent.action.MAIN" />        <category android:name="android.intent.category.LAUNCHER" />      </intent-filter>    </activity>  </application></manifest>

    

المثال ٢: لقاء الـ View بالكود

setContentView(R.layout.activity_main);TextView hello = findViewById(R.id.textHello);Button greet = findViewById(R.id.buttonGreet);hello.setText("Welcome");greet.setOnClickListener(v -> Log.d("MainActivity", "button clicked"));Log.d("MainActivity", "onCreate finished");

    

المثال ٣: Intent لخدمة في الخلفية

Intent music = new Intent(this, MusicService.class);music.putExtra("track", "lesson-01.mp3");Log.d("MainActivity", "asking the service to play");startService(music);

    

اختبار قصير

اختبر فهمك

1. An Activity represents:

2. Which one runs with no screen at all?

3. The file that lists the app's components is:

4. findViewById(R.id.textHello) returns:

5. Which folder holds the XML layouts?

تمارين

جرّب بإيدك

التمرين 1

اكتب جزء المانيفست اللي يعلن MainActivity كشاشة تفتح أول ما التطبيق يشتغل.

Write the manifest part that declares MainActivity as the screen that opens first.

Starter code:

<!-- TODO: activity element with MAIN and LAUNCHER -->

التمرين 2

في onCreate، هات TextView اسمه textHello وحط فيه نص Welcome، وبعدها اطبع سطر في الـ Log.

In onCreate, find the TextView called textHello, set its text to Welcome, then write a line to the log.

Starter code:

setContentView(R.layout.activity_main);// TODO: find textHello and set its text// TODO: Log.d("MainActivity", "ready")

مشروع صغير في آخر المحاضرة

شاشة ترحيب بزرار

الداتا: greeting-card: our own layout with one TextView and one Button, no real user data.

License / source: Our own layout and code; Android layout reference by Google (link only). Reference page

الخطوات

  1. حط TextView بالـ id textHello وزرار بالـ id buttonGreet في الـ layout.
  2. في onCreate هات العنصرين بـ findViewById وحط نص Welcome في الـ TextView.
  3. لما الزرار يتداس، اطبع سطر في الـ Log واظهر Toast للمستخدم.
  4. شغّل التطبيق وبص على Logcat. المتوقع تحت.

Starter code:

setContentView(R.layout.activity_main);TextView hello = findViewById(R.id.textHello);Button greet = findViewById(R.id.buttonGreet); hello.setText("Welcome");greet.setOnClickListener(v -> {    // TODO: Toast and Log when the button is pressed});Log.d("MainActivity", "onCreate finished");

مشروع نهاية الفصل

ملف المشروع العملي للفصل 2

ملف PDF فيه مشروع صغير بأسئلة عملية تطبّق بيها كل اللي اتعلمته في الفصل، مع حل نموذجي في الآخر.

افتح ملف المشروع (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: app fundamentals and the Android platform (developer.android.com).