Search code examples
androidlistenertoastslidingdrawer

Make a toast if a slidingDrawer is opening?


Hi I would like to make a toast whenever a slidingDrawer is opening. I would like to know how i can listen if the slidingDrawer is opening and then make the toast if it is. I have looked everywhere but couldnt find a solution to the problem. Hope some of you can help :)

i found this on developer.android but could not figure out how to use it

    public void setOnDrawerCloseListener (SlidingDrawer.OnDrawerCloseListener onDrawerCloseListener)

Solution

  • In your Activity where you have your SlidingDrawer, you simply attach the listener like this:

    EDIT:

    Added more code:

    The layout.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <SlidingDrawer xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/slidingDrawer1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:content="@+id/content"
        android:handle="@+id/handle" >
    
        <Button
            android:id="@+id/handle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Handle" />
    
        <LinearLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </LinearLayout>
    
    </SlidingDrawer>
    

    The Activity onCreate method where you set the content view of the Activity:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
    
        SlidingDrawer mySlidingDrawer = (SlidingDrawer) findViewById(R.id.slidingDrawer1);
        mySlidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
            @Override
            public void onDrawerOpened() {
                Toast.makeText(getApplicationContext(), "SlidingDrawer opened!", Toast.LENGTH_SHORT).show();
            }
        });
    }
    

    Bear in mind, that the SlidingDrawer is actually deprecated in API 17 - it's discouraged to use it.