• Stars
    star
    695
  • Rank 65,093 (Top 2 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created almost 11 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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.

More Repositories

1

mosby

A Model-View-Presenter / Model-View-Intent library for modern Android apps
Java
5,489
star
2

AdapterDelegates

"Favor composition over inheritance" for RecyclerView Adapters
Java
2,934
star
3

fragmentargs

Annotation Processor for setting arguments in android fragments
Java
1,077
star
4

annotationprocessing101

Java
431
star
5

ParcelablePlease

Annotation Processor for generating Parcelable code
Java
259
star
6

AnnotatedAdapter

Write less code with AnnotatedAdapter, an annotation processor for generating RecyclerView and AbsListView adapters
Java
195
star
7

sqlbrite-dao

DAO for SQLBrite
Java
182
star
8

debugoverlay

A tiny window overlay to log app internal on top of your android app
Java
151
star
9

mosby-conductor

Plugin for conductor to integrate Mosby
Java
131
star
10

CoordinatorsAndroid

Sample that shows how to apply the Coordinator Pattern on Android
Kotlin
121
star
11

Model-View-Intent-Android

A demo that shows how to apply Model-View-Intent on Android
Kotlin
86
star
12

AdapterCommands

Drop in solution to animate RecyclerView's dataset changes by using command pattern
Java
74
star
13

Instantiator

Tired of manually setup test data of Kotlin data classes or POJOs? Instantiator creates Instances of any class for you so that you can focus on writing tests instead of spending time and effort to setup test data
Kotlin
60
star
14

Vaadin-MVP-Lite

This is a Vaadin Addon that provides a Model-View-Presenter Framework
Java
51
star
15

CircleProgressView

Fork from SmoothProgressbar for internal development of another UI library ...
Java
38
star
16

mvi-timing

Just a simple demo app for blog post about MVI and Timing
Java
29
star
17

Todo-Testing-By-Design

Kotlin
13
star
18

rxworkshop

Kotlin
12
star
19

conductor-shared-element-transition

Kotlin
9
star
20

Vaadin-MVP-Lite-MailExample

This is an example project that shows how to use the VaadinMVP framework
JavaScript
9
star
21

appkit

Develop android application in a modern way. Write less code: appkit uses many popular Annotation Processing libraries like Butterknife, IcePick, Dependency Injection with Dagger and achieve a very clean software architecture with Model-View-Presenter (MVP)
Java
9
star
22

SecureBitcoinWallet

Java
8
star
23

SealedSubclassInstantiator

Instantiates instances of subclasses of a sealed class (Kotlin)
Kotlin
8
star
24

website-old

Personal blog, please read README
CSS
7
star
25

MosbyDagger

Example that shows best practices for Dagger2 + Mosby
4
star
26

AndroidCollections

Some usefu collection implementation like List, Map, Set (and combinations of all) etc. that can be useful in Android Projects
Java
4
star
27

OkHttpCertificate

Kotlin
3
star
28

hdlib

Java
2
star
29

fragmentargs-samples

Java
2
star
30

AndroidDesignPatterns

A Collection of Software Design Patterns for Android Applications
Java
2
star
31

ColorProgressBar

A circular (loading spinner) ProgressBar for Android that let you customize the colors of the loading spinner
Java
2
star
32

MosbyViewPagerDemo

Demo of viewpager + mosby
Java
1
star
33

RobolectircTest

Java
1
star
34

DexUtil

How many mehtods are used in your androids .dex file?
Java
1
star
35

annotationprocessing

Some utilities for writing annotation processors
Java
1
star
36

AndroidCollectionsTest

The eclipcse Android Test Project to run unit tests from eclipse
1
star
37

ason

Java
1
star
38

Vaadin-LoadingPanel

A simple LoadingPanel component, where you can switch beetween showing a loading animation and the "normal" content
Java
1
star
39

HippoHappa

Java
1
star
40

stdlib-android-test

Units test for the stdlib-android library
Java
1
star