অ্যান্ড্রয়েডে এক্টিভিটির বর্তমান অরিয়েন্টেশন (Portrait/Landscape) চেক করা
এই উদাহরণটি দেখাবে কিভাবে onConfigurationChanged() মেথড ওভাররাইড করে ডিভাইসের অরিয়েন্টেশন পরিবর্তন হলে পোর্ট্রেট মোড (লম্বা) কিনা তা ডিটেক্ট করতে হয়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project দিয়ে প্রজেক্ট তৈরি করুন।
ধাপ ২: লেয়াউট ফাইল (res/layout/activity_main.xml)
ConstraintLayout ব্যবহার করে একটি
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
ধাপ ৩: জাভা কোড (java/MainActivity.java)
onConfigurationChanged মেথডে Configuration.ORIENTATION_PORTRAIT এবং ORIENTATION_LANDSCAPE চেক করা হয়েছে।
package com.example.myapplication;
import android.content.res.Configuration;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
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);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
conf.setText("It is landscape");
} else {
conf.setText("It is portrait mode");
}
}
}
ধাপ ৪: ম্যানিফেস্টে কনফিগারেশন চেঞ্জ হ্যান্ডলিং যোগ করুন (AndroidManifest.xml)
খুব গুরুত্বপূর্ণ: এক্টিভিটি ট্যাগে android:configChanges="orientation|screenSize" যোগ না করলে অরিয়েন্টেশন চেঞ্জ হলে এক্টিভিটি রিক্রিয়েট হবে এবং onConfigurationChanged কল হবে না।
<?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="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
অ্যাপ্লিকেশনটি রান করুন
ডিভাইস ঘুরিয়ে পোর্ট্রেট/ল্যান্ডস্কেপ মোড চেঞ্জ করুন। টেক্সট ভিউতে বর্তমান মোডের নাম দেখাবে।

নোট
- API 13 (Android 3.2) এর পর থেকে
screenSizeঅবশ্যই যোগ করতে হয়configChanges-এ। - আধুনিক অ্যাপে
ViewModelএবংonSaveInstanceStateব্যবহার করে কনফিগারেশন চেঞ্জ হ্যান্ডেল করা বেটার প্র্যাক্টিস।