Skip to content

Most visited

Recently visited

navigation

SwipeDismissFrameLayout

public class SwipeDismissFrameLayout
extends FrameLayout

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ android.support.wear.widget.SwipeDismissFrameLayout


A layout enabling left-to-right swipe-to-dismiss, intended for use within an activity.

At least one listener must be added to act on a dismissal action. A listener will typically remove a containing view or fragment from the current activity.

To suppress a swipe-dismiss gesture, at least one contained view must be scrollable, indicating that it would like to consume any horizontal touch gestures in that direction. In this case this view will only allow swipe-to-dismiss on the very edge of the left-hand-side of the screen. If you wish to entirely disable the swipe-to-dismiss gesture, setSwipeable(boolean) can be used for more direct control over the feature.

Summary

Nested classes

class SwipeDismissFrameLayout.Callback

Implement this callback to act on particular stages of the dismissal. 

Inherited XML attributes

From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View

Constants

float DEFAULT_DISMISS_DRAG_WIDTH_RATIO

Inherited constants

From class android.view.ViewGroup
From class android.view.View

Inherited fields

From class android.view.View

Public constructors

SwipeDismissFrameLayout(Context context)

Simple constructor to use when creating a view from code.

SwipeDismissFrameLayout(Context context, AttributeSet attrs)

Constructor that is called when inflating a view from XML.

SwipeDismissFrameLayout(Context context, AttributeSet attrs, int defStyle)

Perform inflation from XML and apply a class-specific base style from a theme attribute.

SwipeDismissFrameLayout(Context context, AttributeSet attrs, int defStyle, int defStyleRes)

Perform inflation from XML and apply a class-specific base style from a theme attribute.

Public methods

void addCallback(SwipeDismissFrameLayout.Callback callback)

Adds a callback for dismissal.

boolean canScrollHorizontally(int direction)

Check if this view can be scrolled horizontally in a certain direction.

float getDismissMinDragWidthRatio()

Returns the current ratio of te screen at which the swipe gesture is treated as swipe-to-dismiss.

boolean isSwipeable()

Returns true if the layout reacts to swipe gestures.

boolean onInterceptTouchEvent(MotionEvent ev)

Implement this method to intercept all touch screen motion events.

boolean onTouchEvent(MotionEvent ev)

Implement this method to handle touch screen motion events.

void removeCallback(SwipeDismissFrameLayout.Callback callback)

Removes a callback that was added with addCallback(Callback).

void requestDisallowInterceptTouchEvent(boolean disallowIntercept)

Called when a child does not want this parent and its ancestors to intercept touch events with onInterceptTouchEvent(MotionEvent).

void setDismissMinDragWidthRatio(float ratio)

Sets the minimum ratio of the screen after which the swipe gesture is treated as swipe-to- dismiss.

void setSwipeable(boolean swipeable)

Sets the layout to swipeable or not.

Protected methods

boolean canScroll(View v, boolean checkV, float dx, float x, float y)

Tests scrollability within child views of v in the direction of dx.

Inherited methods

From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.view.ViewParent
From interface android.view.ViewManager
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

Constants

DEFAULT_DISMISS_DRAG_WIDTH_RATIO

float DEFAULT_DISMISS_DRAG_WIDTH_RATIO

Constant Value: 0.33

Public constructors

SwipeDismissFrameLayout

SwipeDismissFrameLayout (Context context)

Simple constructor to use when creating a view from code.

Parameters
context Context: The Context the view is running in, through which it can access the current theme, resources, etc.

SwipeDismissFrameLayout

SwipeDismissFrameLayout (Context context, 
                AttributeSet attrs)

Constructor that is called when inflating a view from XML. This is called when a view is being constructed from an XML file, supplying attributes that were specified in the XML file. This version uses a default style of 0, so the only attribute values applied are those in the Context's Theme and the given AttributeSet.

The method onFinishInflate() will be called after all children have been added.

Parameters
context Context: The Context the view is running in, through which it can access the current theme, resources, etc.

attrs AttributeSet: The attributes of the XML tag that is inflating the view.

SwipeDismissFrameLayout

SwipeDismissFrameLayout (Context context, 
                AttributeSet attrs, 
                int defStyle)

Perform inflation from XML and apply a class-specific base style from a theme attribute. This constructor allows subclasses to use their own base style when they are inflating.

Parameters
context Context: The Context the view is running in, through which it can access the current theme, resources, etc.

attrs AttributeSet: The attributes of the XML tag that is inflating the view.

defStyle int: An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults.

SwipeDismissFrameLayout

SwipeDismissFrameLayout (Context context, 
                AttributeSet attrs, 
                int defStyle, 
                int defStyleRes)

Perform inflation from XML and apply a class-specific base style from a theme attribute. This constructor allows subclasses to use their own base style when they are inflating.

Parameters
context Context: The Context the view is running in, through which it can access the current theme, resources, etc.

attrs AttributeSet: The attributes of the XML tag that is inflating the view.

defStyle int: An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults.

defStyleRes int: This corresponds to the fourth argument of View(Context, AttributeSet, int, int). It allows a style resource to be specified when creating the view.

Public methods

addCallback

void addCallback (SwipeDismissFrameLayout.Callback callback)

Adds a callback for dismissal.

Parameters
callback SwipeDismissFrameLayout.Callback

canScrollHorizontally

boolean canScrollHorizontally (int direction)

Check if this view can be scrolled horizontally in a certain direction.

Parameters
direction int: Negative to check scrolling left, positive to check scrolling right.

Returns
boolean true if this view can be scrolled in the specified direction, false otherwise.

getDismissMinDragWidthRatio

float getDismissMinDragWidthRatio ()

Returns the current ratio of te screen at which the swipe gesture is treated as swipe-to-dismiss.

Returns
float the current ratio of te screen at which the swipe gesture is treated as swipe-to-dismiss

isSwipeable

boolean isSwipeable ()

Returns true if the layout reacts to swipe gestures.

Returns
boolean

onInterceptTouchEvent

boolean onInterceptTouchEvent (MotionEvent ev)

Implement this method to intercept all touch screen motion events. This allows you to watch events as they are dispatched to your children, and take ownership of the current gesture at any point.

Using this function takes some care, as it has a fairly complicated interaction with View.onTouchEvent(MotionEvent), and using it requires implementing that method as well as this one in the correct way. Events will be received in the following order:

  1. You will receive the down event here.
  2. The down event will be handled either by a child of this view group, or given to your own onTouchEvent() method to handle; this means you should implement onTouchEvent() to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like normal.
  3. For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent().
  4. If you return true from here, you will not receive any following events: the target view will receive the same event but with the action ACTION_CANCEL, and all further events will be delivered to your onTouchEvent() method and no longer appear here.

Parameters
ev MotionEvent: The motion event being dispatched down the hierarchy.

Returns
boolean Return true to steal motion events from the children and have them dispatched to this ViewGroup through onTouchEvent(). The current target will receive an ACTION_CANCEL event, and no further messages will be delivered here.

onTouchEvent

boolean onTouchEvent (MotionEvent ev)

Implement this method to handle touch screen motion events.

If this method is used to detect click actions, it is recommended that the actions be performed by implementing and calling performClick(). This will ensure consistent system behavior, including:

  • obeying click sound preferences
  • dispatching OnClickListener calls
  • handling ACTION_CLICK when accessibility features are enabled

Parameters
ev MotionEvent: The motion event.

Returns
boolean True if the event was handled, false otherwise.

removeCallback

void removeCallback (SwipeDismissFrameLayout.Callback callback)

Removes a callback that was added with addCallback(Callback).

Parameters
callback SwipeDismissFrameLayout.Callback

requestDisallowInterceptTouchEvent

void requestDisallowInterceptTouchEvent (boolean disallowIntercept)

Called when a child does not want this parent and its ancestors to intercept touch events with onInterceptTouchEvent(MotionEvent).

This parent should pass this call onto its parents. This parent must obey this request for the duration of the touch (that is, only clear the flag after this parent has received an up or a cancel.

Parameters
disallowIntercept boolean: True if the child does not want the parent to intercept touch events.

setDismissMinDragWidthRatio

void setDismissMinDragWidthRatio (float ratio)

Sets the minimum ratio of the screen after which the swipe gesture is treated as swipe-to- dismiss.

Parameters
ratio float: the ratio of the screen at which the swipe gesture is treated as swipe-to-dismiss. should be provided as a fraction of the screen

setSwipeable

void setSwipeable (boolean swipeable)

Sets the layout to swipeable or not. This effectively turns the functionality of this layout on or off.

Parameters
swipeable boolean: whether the layout should react to the swipe gesture

Protected methods

canScroll

boolean canScroll (View v, 
                boolean checkV, 
                float dx, 
                float x, 
                float y)

Tests scrollability within child views of v in the direction of dx.

Parameters
v View: view to test for horizontal scrollability

checkV boolean: whether the view v passed should itself be checked for scrollability (true), or just its children (false)

dx float: delta scrolled in pixels. Only the sign of this is used

x float: x coordinate of the active touch point

y float: y coordinate of the active touch point

Returns
boolean true if child views of v can be scrolled by delta of dx

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.