Perform action on click of sliding drawer Handler
-
26-10-2019 - |
Question
In my application I want to fill the sliding draw only when data is not null. So , when the user presses on the sliding drawer handler it should give a dialog "Data is empty" and sliding drawer should not open.
I have tried Handler.setOnTouchListener() but what happens is anywhere a touch event is called that dialog box appear.
Any suggestions?
Solution
You should take a look at the method setOnDrawerOpenListener
.
Create your own OnDrawerOpenListener
and use it to check if the data is null - if it is, close the drawer again:
private class OnDrawerOpened implements OnDrawerOpenListener {
@Override
public void onDrawerOpened() {
if( data == null )
mySlidingDrawer.close();
}
}
You would need a reference to your SlidingDrawer
and you would need OnDrawerOpened
to be an inner class, so you can access the reference to your SlidingDrawer
.
Alternative
You could also create your own subclass by extending SlidingDrawer
and then override the animateOpen()
and open()
methods (you might need to override animateToggle()
and toggle()
too):
public class MySlidingDrawer extends SlidingDrawer {
public MySlidingDrawer( Context context, AttributeSet attrs ) {
super( context, attrs );
}
@Override
public void animateOpen() {
if( data != null )
super.animateOpen();
else
//Show dialog
}
@Override
public void open() {
if( data != null )
super.open();
else
//Show dialog
}
}
If you do this, you would need to reference your own implementation of the SlidingDrawer
in your XML:
<com.myPackage.MySlidingDrawer>
...
</com.myPackage.MySlidingDrawer>
Depeding on how and when you get the data to check for null, you might want to either pass it to MySlidingDrawer
in its constructor or add a method through which you can set the data.