فيديو المحاضرة
الفيديو قيد الإعداد
الفيديو قيد الإعداد. في الأثناء اقرأ المحاضرة المكتوبة وجرّب الأمثلة تحت.
رسوم متحركة
أمثلة كود
أمثلة قصيرة وبسيطة
كل مثال وراه زرار «شغّل خطوة بخطوة»: بيعلّم على السطر اللي بيتنفّذ ويطبع الناتج زي ما بيتنفّذ بالظبط. ده شرح تنفيذ الكود سطر بسطر، مش مترجم حقيقي جوه الصفحة.
المثال ١: هيكل الكلاس
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 -->
<TextView
android:id="@+id/textHello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome" />
Expected output
system: id textHello added to R
التمرين 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
setContentView(R.layout.activity_main);
Button top = findViewById(R.id.buttonTop);
top.setText("Press me");
Expected output
D/MainActivity: button ready
مشروع صغير في آخر المحاضرة
شاشة بزرارين تحت بعض
الداتا: 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
الخطوات
- حط TextView للعنوان، وزرارين ids بتوعهم buttonTop و buttonBottom.
- اربط الزرار الأول بأعلى الأب، والتاني بأسفل الأول.
- في onCreate هات العنوان وحط فيه نص Two buttons.
- شغّل التطبيق: لازم تشوف العنوان وفوقه زرارين تحت بعض. المتوقع في الـ 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
setContentView(R.layout.activity_main);
TextView title = findViewById(R.id.textTitle);
Button top = findViewById(R.id.buttonTop);
Button bottom = findViewById(R.id.buttonBottom);
title.setText("Two buttons");
Log.d("MainActivity", "layout ready: 3 views");
Expected output
D/MainActivity: layout ready: 3 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).