Sun. Dec 22nd, 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.java

package com.androidian.slidingdrawer;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.widget.Button;
import android.widget.SlidingDrawer;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    SlidingDrawer slidingdrawer;
    Button SlidingButton;
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        slidingdrawer = (SlidingDrawer)findViewById(R.id.slidingDrawer1);
        SlidingButton = (Button)findViewById(R.id.handle);

        slidingdrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {

            @Override
            public void onDrawerOpened() {

                Toast.makeText(MainActivity.this, "Sliding drawer open", Toast.LENGTH_LONG).show();
            }
        });

        slidingdrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {

            public void onDrawerClosed() {

                Toast.makeText(MainActivity.this, "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 *