নেভিগেশন বারের ডাইমেনশন প্রোগ্রামের মাধ্যমে বের করা
এই উদাহরণটি দেখাবে কীভাবে সিস্টেম রিসোর্স থেকে নেভিগেশন বারের উচ্চতা এবং প্রস্থ প্রোগ্রামের মাধ্যমে পাওয়া যায়। এটি ইমারসিভ মোড বা কাস্টম UI লেআউট তৈরি করার সময় খুব কাজে দেয়।
পদক্ষেপ ১: প্রজেক্ট তৈরি করুন
Android Studio-তে একটি নতুন প্রজেক্ট শুরু করুন।
পদক্ষেপ ২: লেআউট XML (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Navigation Bar Height and Width"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
পদক্ষেপ ৩: MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textView);
Resources resources = getApplicationContext().getResources();
// নেভিগেশন বারের উচ্চতা রিসোর্স আইডি পান
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
// পিক্সেল মান পান
int navBarHeight = resources.getDimensionPixelSize(resourceId);
textView.setText("Navigation Bar Height: " + navBarHeight + "px");
} else {
textView.setText("Navigation bar not found (gesture navigation active?)");
}
}
}
গুরুত্বপূর্ণ সংশোধন
মূল উদাহরণে textView.setText(resourceId) ব্যবহার করা হয়েছে যা রিসোর্স আইডি (ইন্টিজার) দেখাবে, নয় পিক্সেল মান। সঠিক পদ্ধতি হলো getDimensionPixelSize(resourceId) ব্যবহার করা।
প্রস্থ বের করার পদ্ধতি
নেভিগেশন বারের প্রস্থ সাধারণত স্ক্রিনের প্রস্থের সমান (ভার্টিক্যাল অরিয়েন্টেশনে)। ল্যান্ডস্কেপ মোডে এটি ভিন্ন হতে পারে:
int widthResourceId = resources.getIdentifier("navigation_bar_width", "dimen", "android");
if (widthResourceId > 0) {
int navBarWidth = resources.getDimensionPixelSize(widthResourceId);
}
সতর্কতা
- জেসচার নেভিগেশন সক্রিয় থাকলে নেভিগেশন বার নাও থাকতে পারে
- ডিভাইস এবং অ্যান্ড্রোইড ভার্সন অনুযায়ী মান ভিন্ন হতে পারে
- WindowInsets API (API 20+) ব্যবহার করুন আরও নির্ভরযোগ্য ফলাফলের জন্য