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

الفصل 3 (الجزء 1): Java, XML and the UI designer

One screen, two files: the class and the layout

على طول: الشاشة بتتكوّن من ملف Java وملف XML، وهنربطهم ببعض.

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

الفيديو قيد الإعداد

الفيديو قيد الإعداد. في الأثناء اقرأ المحاضرة المكتوبة وجرّب الأمثلة تحت.

رسوم متحركة

من Java للشاشة

أمثلة كود

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

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

المثال ١: هيكل الكلاس

public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Log.d("MainActivity", "screen created");    }}

    

المثال ٢: زرارين في الـ XML

<androidx.constraintlayout.widget.ConstraintLayout    android:layout_width="match_parent"    android:layout_height="match_parent">  <Button      android:id="@+id/buttonTop"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="Top"      app:layout_constraintTop_toTopOf="parent" />  <Button      android:id="@+id/buttonBottom"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="Bottom"      app:layout_constraintTop_toBottomOf="@id/buttonTop" /></androidx.constraintlayout.widget.ConstraintLayout>

    

المثال ٣: الكود بيلاقي العنصر

setContentView(R.layout.activity_main);Button top = findViewById(R.id.buttonTop);top.setText("Press me");Log.d("MainActivity", "found " + top.getText());

    

اختبار قصير

اختبر فهمك

1. Which file holds the layout?

2. setContentView(R.layout.activity_main) does what?

3. Two views with the same id:

4. In extends AppCompatActivity, extends means:

5. findViewById returns:

تمارين

جرّب بإيدك

التمرين 1

اكتب عنصر TextView في الـ XML بيه id اسمه textHello ونص Welcome.

Write a TextView element in the XML with the id textHello and the text Welcome.

Starter code:

<!-- TODO: TextView with id textHello and text Welcome -->

التمرين 2

اكتب سطر الـ setContentView، وبعده هات الزرار buttonTop وحط فيه نص Press me.

Write the setContentView line, then find buttonTop and set its text to Press me.

Starter code:

// TODO: load the layout// TODO: find buttonTop and set its text

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

شاشة بزرارين تحت بعض

الداتا: two-buttons: our own layout with a title, a top button and a bottom button.

License / source: Our own layout; ConstraintLayout guide by Google (link only). Reference page

الخطوات

  1. حط TextView للعنوان، وزرارين ids بتوعهم buttonTop و buttonBottom.
  2. اربط الزرار الأول بأعلى الأب، والتاني بأسفل الأول.
  3. في onCreate هات العنوان وحط فيه نص Two buttons.
  4. شغّل التطبيق: لازم تشوف العنوان وفوقه زرارين تحت بعض. المتوقع في الـ Logcat تحت.

Starter code:

setContentView(R.layout.activity_main);TextView title = findViewById(R.id.textTitle);Button top = findViewById(R.id.buttonTop);Button bottom = findViewById(R.id.buttonBottom); // TODO: set the title text// TODO: log that the layout has three views

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).