Android नेविगेशन बार के डायमेंशन प्राप्त करने का तरीका
यह उदाहरण दिखाता है कि सिस्टम रिसोर्सेज का उपयोग करके नेविगेशन बार की ऊंचाई और चौड़ाई को कोड के माध्यम से कैसे प्राप्त किया जाता है। यह इमर्सिव UI या कस्टम लेआउट बनाने के लिए उपयोगी है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project के माध्यम से प्रोजेक्ट बनाएं।
चरण 2: लेआउट फ़ाइल (res/layout/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>
चरण 3: 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 height = resources.getDimensionPixelSize(resourceId);
textView.setText("Navigation Bar Height: " + height + "px");
} else {
textView.setText("Navigation bar not found (gesture navigation?)");
}
}
}
चरण 4: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.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>
सुधार और नोट
- मूल कोड में
textView.setText(resourceId)था जो गलत है (यह रिसोर्स ID सेट करता, न कि मान)। इसेgetDimensionPixelSize()से ठीक किया गया है। - नेविगेशन बार की चौड़ाई के लिए
navigation_bar_widthरिसोर्स का उपयोग करें। - जेस्चर नेविगेशन वाले डिवाइस पर नेविगेशन बार नहीं हो सकता, इसलिए null चेक जरूरी है।