Android में बाएं से खुलने वाला SlidingDrawer बनाने का तरीका
नोट: SlidingDrawer API लेवल 17 (Android 4.2) में डिप्रिकेट हो चुका है। आधुनिक ऐप्स में NavigationView (DrawerLayout के साथ) या BottomSheet उपयोग करना चाहिए। यह उदाहरण केवल लिगेसी समझ के लिए है।
चरण 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:padding="4dp"
tools:context=".MainActivity">
<SlidingDrawer
android:id="@+id/slidingDrawer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:handle="@+id/handler"
android:content="@+id/content"
android:rotation="180">
<ImageView
android:id="@+id/handler"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_arrow_upward_black_24dp"
android:rotation="270" />
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:rotation="180">
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/image" />
</LinearLayout>
</SlidingDrawer>
</LinearLayout>
चरण 3: MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
चरण 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>
आधुनिक विकल्प: Navigation Drawer (DrawerLayout)
<!-- res/layout/activity_main.xml -->
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- मुख्य कंटेंट -->
<FrameLayout
android:id="@+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- बाएं नेविगेशन ड्रॉअर -->
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigationView"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
यह मटेरियल डिजाइन गाइडलाइन्स का पालन करता है और सभी नए Android वर्जन पर सही काम करता है।