SwipeBack for Android Activities to do pretty the same as the android "back-button" will do, but in a really intuitive way by using a swipe gesture

Related tags

UI/UX SwipeBack
Overview

SwipeBack

SwipeBack is for Android Activities to do pretty the same as the android "back-button" will do, but in a really intuitive way by using a swipe gesture

Not Actively Maintained

Warning: this project is not actively maintained. It works, but don't expect any future improvements or major bug fixes but I'm willing to merge any Pull Request.

Demo

kicker app

Kicker app

See demo video

Dependency

SwipeBack is available on maven central

compile 'com.hannesdorfmann:swipeback:1.0.4'

How to use it

It's not supported yet to build it from xml. You simply have to set it up in you Activities onCreate() method. Instead of Activity.setContentView() call SwipeBack.setContentView().

public class SwipeBackActivity extends FragmentActivity{

	@Override
	public void onCreate(Bundle saved){
		super.onCreate(saved);

		// Init the swipe back
		SwipeBack.attach(this, Position.LEFT)
		    .setContentView(R.layout.activity_simple)
		    .setSwipeBackView(R.layout.swipeback_default);

	}


	@Override
	public void onBackPressed(){
		super.onBackPressed();
		overridePendingTransition(R.anim.swipeback_stack_to_front,
				R.anim.swipeback_stack_right_out);
	}
}

The code above will use the default setup. R.layout.swipeback_default, the default swipe back layout is already provided by this library as well as DefaultSwipeBackTransformer, R.anim.swipeback_stack_to_front, R.anim.swipeback_stack_to_back, R.anim.swipeback_stack_right_in and R.anim.swipeback_stack_right_out.

Customization

The most important thing is the SwipeBackTransformer. This interface provides an API that will be called from the SwipeBack class. Here is where you implement frame by frame animation while the swipe back view will become open (by users swipe gesture). Additionally you can customize the SwipeBack position, the drag mode (drag content or drag window) and if it should be drawn as overlay or not (Type.BEHIND or Type.OVERLAY).

    /**
	 * Attaches the SwipeBack to the Activity.
	 *
	 * @param activity
	 *            The activity the swipe back will be attached to.
	 * @param type
	 *            The {@link SwipeBack.Type} of the drawer.
	 * @param position
	 *            Where to position the swipe back.
	 * @param dragMode
	 *            The drag mode of the drawer. Can be either
	 *            {@link SwipeBack#DRAG_CONTENT} or
	 *            {@link SwipeBack#DRAG_WINDOW}.
	 * @return The created SwipeBack instance.
	 */
	public static SwipeBack attach(Activity activity, Type type, Position position, int dragMode, SwipeBackTransformer transformer)

You can also draw a divider between the normal content view and the swipe back view and a overlay that will fade out while opening the swipe back view.

SwipeBack.attach(this, Position.LEFT)
		.setDrawOverlay(true)
		.setDivider(drawable)
		.setDividerEnabled(true) // Must be called to enable, setDivider() is not enough
		.setSwipeBackTransformer(new SlideSwipeBackTransformer())
		.setContentView(R.layout.activity_simple)
		.setSwipeBackView(R.layout.swipeback_default);

ViewPager

To distinguish a ViewPager swipe gesture from a SwipeBack swipe gesture you have to setup a OnInterceptMoveEventListener:

public class ViewPagerActivity extends FragmentActivity {

	private ViewPager mViewPager;
	private int mPagerPosition;
	private int mPagerOffsetPixels;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SwipeBack.attach(this, Position.LEFT)
		.setContentView(R.layout.activity_view_pager)
		.setSwipeBackView(R.layout.swipeback_default)
		.setDividerAsSolidColor(Color.WHITE)
		.setDividerSize(2)
		.setOnInterceptMoveEventListener(
				new OnInterceptMoveEventListener() {
					@Override
					public boolean isViewDraggable(View v, int dx,
							int x, int y) {
						if (v == mViewPager) {
							return !(mPagerPosition == 0 && mPagerOffsetPixels == 0)
									|| dx < 0;
						}

						return false;
					}
				});


		mViewPager = (ViewPager) findViewById(R.id.viewPager);
		mViewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager()));

		mViewPager.setOnPageChangeListener(new SimpleOnPageChangeListener(){
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
				mPagerPosition = position;
				mPagerOffsetPixels = positionOffsetPixels;
			}

		});
	}


}

Why?

The Samsung Galaxy Nexus was one of the first device without hardware buttons for "back", "home" and "app switching (multitasking)" but used the androids navigation bar on screen (introduced in Android 4.0). The navigation bar was at least in my opinion a big step forward, especially on screen rotation from landscape to portrait and vice versa. But I asked myself, do we really need a navigation bar? I mean the navigation bar takes ca. 10 % of the whole screen. Even at the home screen the "back" and "home" button is useless (because they do nothing). So I thought to myself: Why do we not use swipe gestures instead of a navigation bar? But maybe this idea is to futuristic and not suitable for all kind of android user. A few years later apple introduced the swipe back gesture in iOS 7. Why doesn't Android Apps use swipe gesture as alternative to the back button. Pinterest and Tumblr do so, but at least they use a single Activity and a ViewPager. The problem with this approach is:

  1. You will lost a little bit the ability to jump to any screen by using intents. Take Pinterest as an example: If you get a push notification from Pinterest and you click on it you will see a loading dialog on screen. Internal the navigation stack is generated by adding Fragments to the ViewPager.

  2. ActionBar: The ActionBar is as default not part of a fragment, but it's part of the activity. So you can not (by using a ViewPager) use the default ActionBar to swipe back to the previous Fragment, because the ActionBar will remain sticky. So you have to implement you own ActionBar and attach that to the fragments view.

My approach can be used for activities. It does pretty the same as the android menu drawers do. It adds an aditional layout and slides the content or the window to the side.

Thanks

  • Simon Vig Therkildsen: The most code of handling swipe gestures has been taken from his android-menudrawer library.
Comments
  • Upgrade VerticalTextView to alternative version

    Upgrade VerticalTextView to alternative version

    Use alternative version provided on http://stackoverflow.com/a/7855852 (where original VerticalTextView appears to come from too)

    Original version had problems, such as clipping text incorrectly, leaving for example just "CK" instead of "BACK"

    opened by michalbednarski 9
  • ev.getX(ev.findPointerIndex(mActivePointerId)); crashes due to bad pointer index

    ev.getX(ev.findPointerIndex(mActivePointerId)); crashes due to bad pointer index

    java.lang.IllegalArgumentException: pointerIndex out of range
           at android.view.MotionEvent.nativeGetAxisValue(MotionEvent.java)
           at android.view.MotionEvent.getX(MotionEvent.java:2014)
           at com.hannesdorfmann.swipeback.SlidingSwipeBack.onInterceptTouchEvent(SlidingSwipeBack.java:601)
           at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1960)
           at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
           at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
           at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2369)
           at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1719)
           at android.app.Activity.dispatchTouchEvent(Activity.java:2742)
           at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2330)
           at android.view.View.dispatchPointerEvent(View.java:8666)
           at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4123)
           at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3989)
           at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
           at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
           at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
           at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3680)
           at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
           at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3737)
           at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
           at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
           at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
           at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
           at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
           at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5807)
           at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5781)
           at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5752)
           at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5897)
           at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
           at android.os.MessageQueue.nativePollOnce(MessageQueue.java)
           at android.os.MessageQueue.next(MessageQueue.java:143)
           at android.os.Looper.loop(Looper.java:122)
           at android.app.ActivityThread.main(ActivityThread.java:5254)
           at java.lang.reflect.Method.invoke(Method.java)
           at java.lang.reflect.Method.invoke(Method.java:372)
           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
    
    opened by mrhether 6
  • Limit area in activity for swipe back

    Limit area in activity for swipe back

    Love your library. Is there a way to limit the swipe back area on the activity? I am running into an issue when trying to scroll around on a webView, if I scroll right the swipe back gets activated. Would like to limit it to a smaller area if you know what I mean.

    Thanks

    opened by Mfrenchy77 4
  • How to make swipe back transparent?

    How to make swipe back transparent?

    I like your library but, I want to make transparent swipeback layout from left or other side. How to make transparent background when swipe back to previous activity like Pinterest? Tks

    opened by code4lifevn 4
  • How to use it in fragment?

    How to use it in fragment?

    Great library, I think this library is better than https://github.com/Issacw0ng/SwipeBackLayout because it has an indicator and lots of custom options, but how can I use this library in fragment?For example, in onCreateView, how can I get the swipeback view?

    question 
    opened by kyze8439690 4
  • NullPointerException in VerticalTextView.onDraw

    NullPointerException in VerticalTextView.onDraw

    Previous versions worked good but in last one I am getting big amount of crashes with this log:

    Fatal Exception: java.lang.NullPointerException
           at com.hannesdorfmann.swipeback.view.VerticalTextView.onDraw(VerticalTextView.java:54)
           at android.view.View.draw(View.java:15278)
           at android.view.View.getDisplayList(View.java:14172)
           at android.view.View.getDisplayList(View.java:14214)
           at android.view.View.draw(View.java:14992)
           at android.view.ViewGroup.drawChild(ViewGroup.java:3322)
           at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3158)
           at android.view.View.draw(View.java:15281)
           at android.view.View.getDisplayList(View.java:14172)
           at android.view.View.getDisplayList(View.java:14214)
           at android.view.View.draw(View.java:14992)
           at android.view.ViewGroup.drawChild(ViewGroup.java:3322)
           at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3158)
           at com.hannesdorfmann.swipeback.BuildLayerFrameLayout.dispatchDraw(BuildLayerFrameLayout.java:83)
           at android.view.View.draw(View.java:15281)
           at android.widget.FrameLayout.draw(FrameLayout.java:472)
           at android.view.View.getDisplayList(View.java:14172)
           at android.view.View.getHardwareLayerDisplayList(View.java:14199)
           at android.view.View.getHardwareLayer(View.java:13925)
           at android.view.View.buildLayer(View.java:13853)
           at com.hannesdorfmann.swipeback.BuildLayerFrameLayout$2.run(BuildLayerFrameLayout.java:95)
           at android.os.Handler.handleCallback(Handler.java:733)
           at android.os.Handler.dispatchMessage(Handler.java:95)
           at android.os.Looper.loop(Looper.java:146)
           at android.app.ActivityThread.main(ActivityThread.java:5694)
           at java.lang.reflect.Method.invokeNative(Method.java)
           at java.lang.reflect.Method.invoke(Method.java:515)
           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
           at dalvik.system.NativeStart.main(NativeStart.java)
    
    opened by VladimirWrites 2
  • Fixed crash during rotation.

    Fixed crash during rotation.

    In Android L, using the latest support library, the bundled saved state was crashing due to a class cast exception. Looking through the code, I couldn't detect that the saved state was actually doing anything, noteworty, and figure it must be leftover from the drawer library this is based on.

    Thanks for the library!

    opened by iainconnor 2
  • How could I add this into an Activity containing a content Fragment?

    How could I add this into an Activity containing a content Fragment?

    public class ItemDescriptionActivity extends ActionBarActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                        .add(android.R.id.content, ItemDescriptionFragment.newInstance())
                        .commit();
            }
    }
    

    Your sample code shows how to add swipe back gesture to an Activity, however, it misses the part explaining Activity that contains Fragment.

    opened by dannysunyu 2
  • java.lang.IllegalArgumentException: Invalid Region.Op - only INTERSECT and DIFFERENCE are allowed in android P

    java.lang.IllegalArgumentException: Invalid Region.Op - only INTERSECT and DIFFERENCE are allowed in android P

    java.lang.IllegalArgumentException: Invalid Region.Op - only INTERSECT and DIFFERENCE are allowed at android.graphics.Canvas.checkValidClipOp(Canvas.java:779) at android.graphics.Canvas.clipRect(Canvas.java:918) at com.hannesdorfmann.swipeback.view.VerticalTextView.draw(VerticalTextView.java:45) at android.view.View.updateDisplayListIfDirty(View.java:19082) at android.view.View.draw(View.java:19935) at android.view.ViewGroup.drawChild(ViewGroup.java:4333) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4112) at android.view.View.updateDisplayListIfDirty(View.java:19073) at android.view.View.draw(View.java:19935) at android.view.ViewGroup.drawChild(ViewGroup.java:4333) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4112) at com.hannesdorfmann.swipeback.BuildLayerFrameLayout.dispatchDraw(BuildLayerFrameLayout.java:83) at android.view.View.draw(View.java:20210) at android.view.View.updateDisplayListIfDirty(View.java:19082) at android.view.View.draw(View.java:19935) at android.view.ViewGroup.drawChild(ViewGroup.java:4333) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4112) at com.hannesdorfmann.swipeback.SwipeBack.dispatchDraw(SwipeBack.java:796) at android.view.View.draw(View.java:20210) at android.view.View.updateDisplayListIfDirty(View.java:19082) at android.view.View.draw(View.java:19935) at android.view.ViewGroup.drawChild(ViewGroup.java:4333) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4112) at android.view.View.draw(View.java:20210) at com.android.internal.policy.DecorView.draw(DecorView.java:780) at android.view.View.updateDisplayListIfDirty(View.java:19082) at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:686) at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:692) at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:801) at android.view.ViewRootImpl.draw(ViewRootImpl.java:3311) at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:3115) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2484) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949) at android.view.Choreographer.doCallbacks(Choreographer.java:761) at android.view.Choreographer.doFrame(Choreographer.java:696) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

    opened by konstantin1883 1
  • Does this lib have onSwipeBackListener?

    Does this lib have onSwipeBackListener?

    Hi @sockeqwe, Can i use this lib for fragment? I want to override the listener when user swipe back (not finish my activity). Is your lib support this issue? :)

    opened by ngocht143 1
  • How change text caption?

    How change text caption?

    I cannot find method to change text when i drag fragment

    ex

     SwipeBack.attach(this, Position.LEFT)
                    .setContentView(R.layout.fragment_blank)
                    .setSwipeBackView(R.layout.swipeback_default)
                    .setText("BACK TO MAIN");
    
    opened by Radzhab 1
Releases(1.0.4)
Owner
Hannes Dorfmann
Hannes Dorfmann
A nicer-looking, more intuitive and highly customizable alternative for radio buttons and dropdowns for Android.

SwipeSelector Undergoing for some API changes for a 2.0 major version, see example usage in the sample module! What and why? Bored of dull looking rad

Iiro Krankka 1.1k Dec 30, 2022
Sliding cards with pretty gallery effects.

SlidingCard Sliding cards with pretty gallery effects. Download Include the following dependency in your build.gradle file. Gradle: repositories {

mxn 681 Sep 7, 2022
Android Library to implement simple touch/tap/swipe gestures

SimpleFingerGestures An android library to implement simple 1 or 2 finger gestures easily Example Library The library is inside the libSFG folder Samp

Arnav Gupta 315 Dec 21, 2022
Android library which allows you to swipe down from an activity to close it.

Android Sliding Activity Library Easily create activities that can slide vertically on the screen and fit well into the Material Design age. Features

Jake Klinker 1.3k Nov 25, 2022
Android swipe-to-dismiss mini-library and sample code

Android Swipe-to-Dismiss Sample Code Sample code that shows how to make ListView or other views support the swipe-to-dismiss Android UI pattern. See t

Roman Nurik 1.3k Dec 29, 2022
A swipe button for Android with a circular progress bar for async operations

ProSwipeButton A swipe button for Android with a circular progress bar for async operations Gradle dependencies { ... compile 'in.shadowfax:pr

Shadowfax Technologies 340 Nov 13, 2022
Android jetpack compose swipe library

Swiper for Android Jetpack Compose Android Jetpack Compose swipe library. Downlo

null 32 Dec 10, 2022
A player/ recorder visualizer with the swipe to seek functionality.

iiVisu A player/ recorder visualizer with the swipe to seek functionality. Demo Setup Step 1. Add the JitPack repository to your build file Add it in

Iman Irandoost 126 Nov 25, 2022
A simple implementation of swipe card like StreetView

A simple implementation of swipe card like StreetView!! DONATIONS This project needs you! If you would like to support this project's further developm

Michele Lacorte 831 Jan 4, 2023
Card with swipe options in Jetpack Compose

SwipeableActionCard Card with swipe options in Jetpack Compose Tutorial: Click Here Import SwipeableActionCard library Add this in project level build

Harsh Mahajan 1 Nov 23, 2021
This is a sample Android Studio project that shows the necessary code to create a note list widget, And it's an implementation of a lesson on the Pluralsight platform, but with some code improvements

NoteKeeper-Custom-Widgets This is a sample Android Studio project that shows the necessary code to create a note list widget, And it's an implementati

Ibrahim Mushtaha 3 Oct 29, 2022
TourGuide is an Android library that aims to provide an easy way to add pointers with animations over a desired Android View

TourGuide TourGuide is an Android library. It lets you add pointer, overlay and tooltip easily, guiding users on how to use your app. Refer to the exa

Tan Jun Rong 2.6k Jan 5, 2023
Bubbles for Android is an Android library to provide chat heads capabilities on your apps. With a fast way to integrate with your development.

Bubbles for Android Bubbles for Android is an Android library to provide chat heads capabilities on your apps. With a fast way to integrate with your

Txus Ballesteros 1.5k Jan 2, 2023
This library provides a simple way to add a draggable sliding up panel (popularized by Google Music and Google Maps) to your Android application. Brought to you by Umano.

Note: we are not actively responding to issues right now. If you find a bug, please submit a PR. Android Sliding Up Panel This library provides a simp

Umano: News Read To You 9.4k Dec 31, 2022
[] A simple way to "badge" any given Android view at runtime without having to cater for it in layout

Android ViewBadger A simple way to "badge" any given Android view at runtime without having to cater for it in layout. Note: If your aim is to replica

Jeff Gilfelt 3k Nov 28, 2022
NumberPickerView - Custom Android View to provide a user friendly way of picking numbers. 🧪

?? Custom view for Android which provides a modern design and gestures for picking numbers in a user friendly way.

Mirkamal 6 Feb 16, 2022
A set of Android-UI components to make it easier to request permission in a user friendly way.

Permission UI A set of Android-UI components to make it easier to request permission in a user friendly way. Access background location A jetpack comp

Stefan Wärting 54 Nov 3, 2022
An elegant way to show your menu or messages.

Android View Hover In my opinion, jumping to a new activity to show your menu is a kind of wasting time and life. So, I think, we need a hover view, t

代码家 3.2k Jan 2, 2023
Just a Wheel——A easy way to setEmptyView to ListView、GridView or RecyclerView etc..

中文说明在这里 TEmptyView Just a Wheel—— A easier way to setEmptyView. Without having to write xml file every time. It supports AdapterView(ListView,GridView

Barry 454 Jan 9, 2023