एंड्रॉइड में स्टेटस बार की ऊंचाई जानने का तरीका
एंड्रॉइड ऐप विकास में कई बार हमें स्टेटस बार की सटीक ऊंचाई जानने की आवश्यकता होती है, खासकर जब हम फुल-स्क्रीन लेआउट या कस्टम UI बना रहे हों। यह उदाहरण दर्शाता है कि प्रोग्रामेटिक रूप से स्टेटस बार की ऊंचाई कैसे प्राप्त की जाए।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें और आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट फ़ाइल में कोड जोड़ें
res/layout/activity_main.xml में निम्नलिखित XML कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
xmlns:app="https://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
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" />
</androidx.constraintlayout.widget.ConstraintLayout>
चरण 3: MainActivity में लॉजिक जोड़ें
src/MainActivity.java में निम्नलिखित कोड जोड़ें:
package com.app.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.res.Resources;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getStatusBarHeight();
}
private int getStatusBarHeight() {
int height = 0;
Resources myResources = getResources();
int idStatusBarHeight = myResources.getIdentifier("status_bar_height", "dimen", "android");
if (idStatusBarHeight > 0) {
height = getResources().getDimensionPixelSize(idStatusBarHeight);
Toast.makeText(this, "स्टेटस बार की ऊंचाई = " + height + " px", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "रिसोर्स नहीं मिला", Toast.LENGTH_LONG).show();
}
return height;
}
}
चरण 4: AndroidManifest.xml कॉन्फ़िगर करें
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.app.sample">
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ऐप चलाएं और परिणाम देखें
अपने एंड्रॉइड डिवाइस को कंप्यूटर से कनेक्ट करें, Android Studio में Run आइकन पर क्लिक करें और अपना डिवाइस चुनें। ऐप लॉन्च होने पर एक टोस्ट संदेश में स्टेटस बार की ऊंचाई पिक्सेल में प्रदर्शित होगी।
नोट: यह विधि सिस्टम रिसोर्स status_bar_height का उपयोग करती है जो सभी एंड्रॉइड संस्करणों पर उपलब्ध होता है। परिणाम डिवाइस के स्क्रीन घनत्व (density) के अनुसार पिक्सेल में होता है।