ภาพรวม
คู่มือสร้างแอป Android แรก "Hello World" แบบขั้นตอนต่อขั้นตอน สำหรับผู้เริ่มต้น
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
- เปิด Android Studio
- เลือก File → New → New Project
- เลือก Template: Empty Activity (หรือ Empty Views Activity)
- คลิก Next
- ตั้งค่า:
- Name: HelloWorld (หรือชื่อที่ต้องการ)
- Package name: com.example.helloworld (อัตโนมัติ)
- Save location: เลือกโฟลเดอร์
- Language: Java หรือ Kotlin
- Minimum SDK: API 21 (Android 5.0) หรือสูงกว่า
- คลิก Finish - รอ Gradle Sync เสร็จ
ขั้นตอนที่ 2: Layout (res/layout/activity_main.xml)
Android Studio สร้างให้อัตโนมัติ แต่โค้ดประมาณนี้:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
ขั้นตอนที่ 3: MainActivity (Java)
package com.example.helloworld;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
MainActivity (Kotlin)
package com.example.helloworld
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
ขั้นตอนที่ 4: Manifest (app/src/main/AndroidManifest.xml)
สร้างอัตโนมัติ:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.helloworld">
<application
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.HelloWorld">
<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>
ขั้นตอนที่ 5: รันแอป
- เชื่อมต่ออุปกรณ์ Android จริง (เปิด USB Debugging) หรือสร้าง Emulator (Device Manager → Create Device)
- ใน Toolbar คลิกไอคอน ▶ Run (หรือ Shift+F10)
- เลือกอุปกรณ์ → OK
- รอ Build และ Install
- แอปจะเปิดบนอุปกรณ์ แสดง "Hello World!"
โครงสร้างโปรเจกต์สำคัญ
app/src/main/java/.../MainActivity- โค้ดหลักapp/src/main/res/layout/activity_main.xml- UI Layoutapp/src/main/res/values/strings.xml- String Resourcesapp/src/main/AndroidManifest.xml- การตั้งค่าแอปapp/build.gradle.kts(หรือ .gradle) - Dependencies & Build Config
เคล็ดลับสำหรับมือใหม่
- ใช้ Kotlin - ภาษารسميةของ Android ปัจจุบัน
- เรียนรู้ View Binding แทน
findViewById - ศึกษา Activity Lifecycle (onCreate, onStart, onResume, onPause, onStop, onDestroy)
- อ่าน Android Basics with Compose คอร์สฟรีจาก Google