কম্পিউটার টিউটোরিয়াল

অ্যান্ড্রয়েডে ল্যান্ডস্কেপ মোড (Landscape Mode) চেক করার পদ্ধতি

অ্যান্ড্রয়েডে ল্যান্ডস্কেপ মোড (Landscape Orientation) ডিটেক্ট করার পদ্ধতি

এই উদাহরণটি পূর্ববর্তীটির (পোর্ট্রেট চেক) সমান, শুধুমাত্র ল্যান্ডস্কেপ মোডের জন্য স্পেসিফিক লজিক দেখানো হয়েছে। এক্টিভিটি রিক্রিয়েট না করে অরিয়েন্টেশন চেঞ্জ হ্যান্ডেল করার টেকনিকটি একই।

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে File → New Project দিয়ে প্রজেক্ট তৈরি করুন।

ধাপ ২: লেয়াউট ফাইল (res/layout/activity_main.xml)

<?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)

এখানে শুধু ল্যান্ডস্কেপ চেক করা হয়েছে, পোর্ট্রেটের জন্য আলাদা হ্যান্ডলিং নেই।

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");
        }
    }
}

ধাপ ৪: ম্যানিফেস্টে কনফিগারেশন চেঞ্জ (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="keyboardHidden|orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

অ্যাপ্লিকেশনটি রান করুন

ডিভাইস ল্যান্ডস্কেপ মোডে ঘুরালে টেক্সট "It is landscape" দেখাবে। পোর্ট্রেটে ফিরলে টেক্সট পরিবর্তন হবে না (কারণ কোডে else ব্লক নেই)।

অ্যান্ড্রয়েডে ল্যান্ডস্কেপ মোড (Landscape Mode) চেক করার পদ্ধতি

সর্বোত্তম অভ্যাস

দুই মোডই হ্যান্ডেল করার জন্য if-else ব্যবহার করুন। কেবল ল্যান্ডস্কেপ ডিটেক্ট করলেই হলে উপরের কোড যথেষ্ট।

  1. অ্যান্ড্রয়েড অ্যাপে JSONArray পার্স করার পদ্ধতি

  2. অ্যান্ড্রয়েডে EditText খালি কিনা চেক করার পদ্ধতি

  3. Android-এ ট্রান্সপেরেন্ট (পারদর্শী) অ্যাক্টিভিটি তৈরি করার পদ্ধতি

  4. অ্যান্ড্রয়েডে স্প্লিট স্ক্রিন মোড কীভাবে ব্যবহার করবেন