วิธีปิดโหมด Landscape ใน Android
Android รองรับสองการจับตำแหน่งคือ Portrait และ Landscape เราสามารถปิดการจับตำแหน่งในแอปพลิเคชัน Android ได้ ตัวอย่างนี้สาธิตวิธีปิดโหมด Landscape ใน Android
ขั้นตอนที่ 1 - สร้างโปรเจกต์ใหม่ใน Android Studio
ไปที่ File → New Project และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโปรเจกต์ใหม่
ขั้นตอนที่ 2 - เพิ่มโค้ดใน res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#dde4dd">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
ในโค้ดด้านบน มี LinearLayout และ EditText รองรับทั้ง Landscape และ Portrait ดังภาพด้านล่าง

ภาพด้านบนแสดงโหมด Landscape

ภาพด้านบนแสดงโหมด Portrait
เพื่อปิดโหมด Landscape ต้องเพิ่มแท็ก screenOrientation ใน manifest.xml ดังนี้:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.andy.myapplication">
<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/AppTheme">
<activity android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ในโค้ดด้านบน เรากำหนด screenOrientation เป็น portrait หมายถึง MainActivity จะรองรับเฉพาะโหมด Portrait เท่านั้น หากต้องการพัฒนาแอปในโหมด Portrait ให้เพิ่ม screenOrientation ภายในแท็ก application
ลองรันแอปพลิเคชันของคุณ สมมติว่าคุณเชื่อมต่ออุปกรณ์ Android จริงกับคอมพิวเตอร์แล้ว เพื่อรันแอปจาก Android Studio ให้เปิดไฟล์กิจกรรมของโปรเจกต์หนึ่งและคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก และตรวจสอบอุปกรณ์ซึ่งจะแสดงหน้าจอเริ่มต้น

ในผลลัพธ์ด้านบน แสดงเฉพาะโหมด Portrait ตอนนี้พลิกอุปกรณ์ จะไม่เปลี่ยนมุมมองตามการจับตำแหน่ง