यह उदाहरण दर्शाता है कि Kotlin में इमर्सिव मोड का उपयोग करके Android एक्टिविटी में नेविगेशन बार को स्थायी रूप से कैसे छिपाया जाए।
चरण 1 - नया प्रोजेक्ट बनाएं
Android Studio: File → New Project।
चरण 2 - activity_main.xml लेआउट
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:background="#008080"
android:padding="5dp"
android:text="TutorialsPoint"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello World!"
android:textColor="@android:color/holo_blue_dark"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
चरण 3 - MainActivity.kt में इमर्सिव मोड लागू करें
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
@Suppress("DEPRECATION")
class MainActivity : AppCompatActivity() {
private var currentApiVersion = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
currentApiVersion = Build.VERSION.SDK_INT
hideNavigationBar()
if (currentApiVersion >= Build.VERSION_CODES.KITKAT) {
val decorView = window.decorView
decorView.setOnSystemUiVisibilityChangeListener { visibility ->
if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {
hideNavigationBar()
}
}
}
}
private fun hideNavigationBar() {
val flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_FULLSCREEN or
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
if (currentApiVersion >= Build.VERSION_CODES.KITKAT) {
window.decorView.systemUiVisibility = flags
}
}
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus && currentApiVersion >= Build.VERSION_CODES.KITKAT) {
hideNavigationBar()
}
}
}
चरण 4 - AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.hidenavbar">
<application ...>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ऐप चलाएं
ऐप चलने पर नेविगेशन बार और स्टेटस बार दोनों छिप जाएंगे। स्क्रीन के किनारे से स्वाइप करने पर वे अस्थायी रूप से दिखाई देंगे फिर फिर से छिप जाएंगे (IMMERSIVE_STICKY व्यवहार)।
नोट: Android 11 (API 30+) में WindowInsetsController API का उपयोग करने की सलाह दी जाती है जो अधिक आधुनिक और सरल है।