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

অ্যান্ড্রয়েডে পোর্ট্রেট এবং ল্যান্ডস্কেপের জন্য আলাদা লেআউট নির্দিষ্ট করার পদ্ধতি

রিসোর্স কোয়ালিফায়ার ব্যবহার করে অরিয়েন্টেশন-স্পেসিফিক লেআউট

অ্যান্ড্রয়েডে res/layout-land/ ডিরেক্টরি তৈরি করে ল্যান্ডস্কেপ মোডের জন্য আলাদা লেআউট প্রদান করা যায়। সিস্টেম অটোমেটিক্যালি ডিভাইসের অরিয়েন্টেশন অনুযায়ী সঠিক লেআউট বেছে নেয়।

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

সাধারণ প্রজেক্ট সেটআপ।

ধাপ ২: পোর্ট্রেট লেআউট (ডিফল্ট)

res/layout/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:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f2f6f4"
    android:padding="10dp"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/tvLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/button"
        android:layout_centerInParent="true"
        android:layout_marginTop="16sp"
        android:text="Portrait Mode"
        android:textSize="24sp"
        android:textStyle="bold" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Show Orientation mode" />
</RelativeLayout>

ধাপ ৩: ল্যান্ডস্কেপ লেআউট তৈরি করুন

  1. Project view-এ res ফোল্ডারে রাইট-ক্লিক → New → Android Resource Directory
  2. Resource type: layout
  3. Qualifiers: Orientation → Landscape
  4. এটি res/layout-land/ ফোল্ডার তৈরি করবে
  5. সেখানে 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/tvLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginStart="64dp"
        android:layout_toEndOf="@id/button"
        android:text="Landscape Mode"
        android:textSize="24sp"
        android:textStyle="bold" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginStart="128sp"
        android:text="Show Orientation mode" />
</RelativeLayout>

ধাপ ৪: কোটলিনে অরিয়েন্টেশন ডিটেকশন

src/MainActivity.kt:

import android.content.res.Configuration
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    lateinit var textView: TextView
    lateinit var button: Button
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
        
        textView = findViewById(R.id.tvLayout)
        button = findViewById(R.id.button)
        
        button.setOnClickListener {
            val orientation = resources.configuration.orientation
            if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                Toast.makeText(this@MainActivity, "We are in portrait mode", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this@MainActivity, "We are in Landscape mode", Toast.LENGTH_SHORT).show()
            }
        }
    }
}

অন্যান্য কোয়ালিফায়ার

কোয়ালিফায়ারব্যবহার
layout-sw600dp7-ইঞ্চি ট্যাবলেট+
layout-xlargeবড় স্ক্রিন ডিভাইস
layout-nightডার্ক থিম
layout-hdpiউচ্চ ঘনত্ব স্ক্রিন
অ্যান্ড্রয়েডে পোর্ট্রেট এবং ল্যান্ডস্কেপের জন্য আলাদা লেআউট নির্দিষ্ট করার পদ্ধতি অ্যান্ড্রয়েডে পোর্ট্রেট এবং ল্যান্ডস্কেপের জন্য আলাদা লেআউট নির্দিষ্ট করার পদ্ধতি
  1. অ্যান্ড্রয়েডে ডাইনামিক্যালি বাটন যোগ করা

  2. Android-এ বিভিন্ন অ্যাক্টিভিটির মধ্যে স্যুইচ করার পদ্ধতি

  3. অ্যান্ড্রয়েডে কাস্টম নোটিফিকেশন লেআউট ও টেক্সট কালার তৈরি করা

  4. পিসি এবং অ্যান্ড্রয়েডে গুগল সেফসার্চ অন করার পদ্ধতি