অরিয়েন্টেশন চেঞ্জ হ্যান্ডলিং
এই উদাহরণে দেখানো হয়েছে কিভাবে Android-এ স্ক্রিন অরিয়েন্টেশন চেঞ্জ (পোর্ট্রেট থেকে ল্যান্ডস্কেপ বা বিপরীত) হ্যান্ডল করা যায়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project এ যান এবং প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট ফাইল
res/layout/activity_main.xml ফাইলে নিচের কোডটি যোগ করুন:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/actionEvent"
android:textSize="40sp"
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
ধাপ ৩: MainActivity.java ফাইল
src/MainActivity.java ফাইলে নিচের কোডটি যোগ করুন। onConfigurationChanged() ওভাররাইড করা হয়েছে:
package com.example.myapplication;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView actionEvent;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionEvent = findViewById(R.id.actionEvent);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
actionEvent.setText("ORIENTATION LANDSCAPE");
} else {
actionEvent.setText("ORIENTATION PORTRAIT");
}
}
}
ধাপ ৪: Manifest.xml ফাইল (অপরিহার্য)
AndroidManifest.xml ফাইলে আক্টিভিটির জন্য android:configChanges অ্যাট্রিবিউট যোগ করুন। এটি সিস্টেমকে বলে যে এই কনফিগারেশন চেঞ্জগুলো আপনি নিজে হ্যান্ডল করবেন, তাই আক্টিভিটি রিক্রিয়েট হবে না:
<?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" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
অ্যাপ্লিকেশন চালানোর ফলাফল
অ্যাপটি রান করলে প্রথমে পোর্ট্রেট মোডে নিচের স্ক্রিনটি দেখাবে:

স্ক্রিন ঘুরিয়ে ল্যান্ডস্কেপ করলে টেক্সট পরিবর্তন হবে:

মहত্বपूर्ण: android:configChanges ব্যবহার না করলে অরিয়েন্টেশন চেঞ্জে আক্টিভিটি রিস্টার্ট হয় (onCreate আবার কল হয়)। এই পদ্ধতিতে আপনি নিজে হ্যান্ডল করছেন।