एंड्रॉइड में एक्टिविटी ओरिएंटेशन (पोर्ट्रेट/लैंडस्केप) जांचना
यह ट्यूटोरियल दिखाता है कि onConfigurationChanged() का उपयोग करके डिवाइस ओरिएंटेशन परिवर्तन का पता कैसे लगाएं और वर्तमान मोड (पोर्ट्रेट/लैंडस्केप) प्रदर्शित करें।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: लेआउट (res/layout/activity_main.xml)
<?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:id="@+id/conf"
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/MainActivity.java)
package com.example.myapplication;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
TextView conf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
conf = findViewById(R.id.conf);
// प्रारंभिक ओरिएंटेशन सेट करें
updateOrientationText();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
updateOrientationText();
}
private void updateOrientationText() {
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
conf.setText("यह लैंडस्केप मोड है");
} else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
conf.setText("यह पोर्ट्रेट मोड है");
} else {
conf.setText("अन्य ओरिएंटेशन: " + orientation);
}
}
}
चरण 4: AndroidManifest.xml में कॉन्फिग चेंज हैंडलिंग
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.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:configChanges="orientation|screenSize|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
महत्वपूर्ण नोट्स
android:configChangesमें screenSize जोड़ना एंड्रॉइड 3.2+ (API 13+) के लिए आवश्यक है- इसके बिना, ओरिएंटेशन चेंज पर एक्टिविटी रीस्टार्ट होगी
getResources().getConfiguration().orientationकभी भी वर्तमान ओरिएंटेशन दे सकता है- कस्टम व्यू के लिए
onSizeChanged()भी उपयोगी है
वैकल्पिक: ViewModel/Lifecycle के साथ
// लाइफसाइकल-अवेयर ओरिएंटेशन ऑब्जर्वर
getLifecycle().addObserver(new DefaultLifecycleObserver() {
@Override
public void onResume(@NonNull LifecycleOwner owner) {
updateOrientationText();
}
});
ऐप टेस्ट करें
डिवाइस घुमाएं - टेक्स्ट "पोर्ट्रेट मोड" ↔ "लैंडस्केप मोड" बदलेगा बिना एक्टिविटी रीस्टार्ट के।