Mon. Dec 23rd, 2024

Sliding drawer is is used to add smooth simple sliding drawer navigational menus on android applications. Sliding drawer works same as web designing sliding menus. It controlled by simple handle button automatically generated by SlidingDrawer tag. Sliding drawer always works inside layout and best work on linear layout. Sliding drawer contains handle buttons and after it another layout tag and all of hidden menus implements on there

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <SlidingDrawer
        android:id="@+id/slidingDrawer1"
        android:layout_width="wrap_content"
        android:layout_height="350dp"
        android:content="@+id/content"
        android:handle="@+id/handle"
        android:orientation="vertical"
        android:rotation="180"  >
        <Button
            android:id="@+id/handle"
            android:layout_width="wrap_content"
            android:layout_height="50sp"
            android:background="@mipmap/ic_launcher"
            />
        <LinearLayout
            android:id="@+id/content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:rotation="180" >
            <!-- PUT HERE ANY WIDGETS OR BUTTONS, IMAGES, TEXTVIEW, EDITTEXT BOX, SEARCH BOX -->
            <!-- TO OPEN INTO SLIDING DRAWER. -->
            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="@drawable/sample_image" />
        </LinearLayout>
    </SlidingDrawer>
</LinearLayout>

MainActivity.kt

package com.androidian.slidingdrawer

import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.SlidingDrawer
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    var slidingdrawer: SlidingDrawer? = null
    var SlidingButton: Button? = null
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        slidingdrawer = findViewById<View>(R.id.slidingDrawer1) as SlidingDrawer
        SlidingButton = findViewById<View>(R.id.handle) as Button
        slidingdrawer!!.setOnDrawerOpenListener {
            Toast.makeText(
                this@MainActivity,
                "Sliding drawer open",
                Toast.LENGTH_LONG
            ).show()
        }
        slidingdrawer!!.setOnDrawerCloseListener {
            Toast.makeText(
                this@MainActivity,
                "Sliding drawer close",
                Toast.LENGTH_LONG
            ).show()
        }
    }
}

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published. Required fields are marked *