Simple and powerful library to emulate iOS's "3D Touch" preview functionality on Android.

Related tags

UI/UX android java
Overview

Android 3D Touch - PeekView

preview

iOS uses 3D Touch as a way to "peek" into full content, such as emails, pictures, web searches, etc. While they have dedicated hardware for this functionality, it is still possible to get similar functionality out of Android, with a long click, rather than the dedicated hardware.

This library aims to create a simple and powerful API to enable 3D Touch style "Peeking" on Android.

Features

features

  1. Simple API
  2. Quick and easy to implement in any app
  3. Long clicking a view will overlay the PeekView until you stop touching the screen again
  4. The PeekView is smart. It will be displayed relative to the touch location, not on some random place on the screen. It will also move out of the way of your finger so that it isn't covered up when displayed.
  5. Customize the PeekView's window size (fullscreen or percent of screen size)
  6. Change the background dim amount
  7. Haptic feedback is optional
  8. Peek into ANY type of views. Example even uses a WebView
  9. Blur the background behind the PeekView for an extra "WOW" factor over the dim percentage

Installation

There are two ways to use this library:

As a Gradle dependency

This is the preferred way. Simply add:

dependencies {
    compile 'com.klinkerapps:peekview:1.2.3'
}

to your project dependencies and run ./gradlew build or ./gradlew assemble.

As a library project

Download the source code and import it as a library project in Eclipse. The project is available in the folder library. For more information on how to do this, read here.

Example Usage

This library is extremely easy to use. There are two steps:

  • Any activities you want to implement the PeekView on, you must use PeekViewActivity as the superclass.

PeekViewActivity is a descendant of AppCompatActivity, so for almost every case, this will just be plug and play, a simple switch. PeekView requires this activity to montitor the touch events and determine when the user lifts their finger to remove the PeekView.

  • Use the Peek.into(... builder to start creating your PeekView on a UI element. There are examples of this in the MainActivity, but here is a very simple one that will display an image full screen.
Peek.into(R.layout.image_peek, new SimpleOnPeek() {
    @Override
    public void onInflated(View rootView) {
        // rootView is the layout inflated from R.layout.image_peek
        ((ImageView) rootView.findViewById(R.id.image))
                .setImageDrawable(getResources().getDrawable(R.drawable.klinker_apps));
    }
}).applyTo(this, findViewById(R.id.image_peek));

More advanced usage

Here are a few of the more advanced things that you can do with PeekView

PeekViewOptions

This is how you customize your PeekView. After creating a PeekViewOptions object, simply add it with Peek.with(options)

Here is a list of all the possible options, along with the implementation:

PeekViewOptions options = new PeekViewOptions();
options.setBackgroundDim(1f);           // range: 0  - 1  (default is .6)
options.setHapticFeedback(false);       // default is true

// it may be a good idea to set set these through resources so that you can use different options based on screen size and orientation
options.setWidthPercent(.4f);           // range: .1 - .9 (default is .6)
options.setHeightPercent(.4f);          // range: .1 - .9 (default is .5)

// you can also set the size of the PeekView using absolute values, instead of percentages. 
// Setting these will override the corresponding percentage value.
// You should use this instead of setting the size of the view from the layout resources, as those get overridden.
options.setAbsoluteWidth(200);          // 200 DP
options.setAbsoluteHeight(200);         // 200 DP

// default is false. If you change this to true, it will ignore the width and height percentages you set.
options.setFullScreenPeek(true); 
// default is true. Unless you are going to animate things yourself, i recommend leaving this as true.
options.setFadeAnimation(false);

// PeekView has the ability to blur the background behind it, instead of just using a simple dark dim.
// If you set a blurred view, then it will invalidate whatever you set as your background dim.
// If you do this, please look at the installation steps for the blur effect, or the app will crash.
options.setBlurBackground(true);                            // default is true
options.setBlurOverlayColor(Color.parse("#99000000"));      // #99000000 default

Peek.into(...).with(options).applyTo(...);

Different lifecycle events

PeekView has a number of lifecycle events that you can choose to implement. In the above example, I just showed the use of SimpleOnPeek, which provides a callback so that you can initialize your layout after it has been inflated. Similar to what you would do after Activity.setContentView(...).

If you are doing any kind of custom animations, or network calls though, you may need something more powerful. SimpleOnPeek is just a wrapper of the OnPeek interface, which also contains callbacks for when the PeekView is displayed to the user on the screen (helpful if you wanted to do your own animations instead of use the default fade animation), as well as a callback for when the PeekView is dismissed from the screen (a good place to stop any networking activities).

Implementing these callbacks is straightforward and almost the same as you have done with the SimpleOnPeek:

Peek.into(..., new OnPeek() {
    @Override
    public void onInflated(View rootView) {
        // the normal inflation callback
    }

    @Override
    public void shown() {
        // the view is shown to the user
        // Could be a nice place for custom animations on the inflated view
    }

    @Override
    public void dismissed() {
        // the view is dismissed from the screen
        // It is destroyed an never reused, so any cleanup would go here
    }
}).applyTo(...);

Clearing the Peek for a View

Sometimes it may be necessary to not allow "peeking" in a View where it was previously allowed. Some situations that come to mind would be in a RecyclerView or ListView where content could be different. Because of the way these lists work, when the view gets recycled, it will retain the "peeking" ability of the original View, unless you clear it. Clearing a "peek" is really easy:

Peek.clear(View);

Currently, all this method does is set the TouchListener to null for the provided View. In the future though, there may be more advanced changes that will be made to this method, so, it is probably safer to use the Peek#clear method instead of just nulling the TouchListener yourself.

Contributing

Please fork this repository and contribute back using pull requests. Features can be requested using issues. All code, comments, and critiques are greatly appreciated.

Changelog

The full changelog for the library can be found here.

License

Copyright 2016 Luke Klinker

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
You might also like...
An Android library containing a simple TableView and an advanced SortableTableView providing a lot of customisation possibilities to fit all needs.
An Android library containing a simple TableView and an advanced SortableTableView providing a lot of customisation possibilities to fit all needs.

SortableTableView for Android An Android library providing a TableView and a SortableTableView. Minimum SDK-Version: 11 | Compile SDK-Version: 25 | La

A simple library to let you sign (or draw lines) smoothly with your finger into a view and save it.
A simple library to let you sign (or draw lines) smoothly with your finger into a view and save it.

FingerSignView Introduction FingerSignView is a simple library that lets you finger, or draw lines, smoothly with your finger into a View and save it

A new canvas drawing library for Android. Aims to be the Fabric.js for Android. Supports text, images, and hand/stylus drawing input. The library has a website and API docs, check it out

FabricView - A new canvas drawing library for Android. The library was born as part of a project in SD Hacks (www.sdhacks.io) on October 3rd. It is cu

A simple library to add Emoji support to your Android Application
A simple library to add Emoji support to your Android Application

Emoji A library to add Emoji support to your Android app. Emojis can be picked in a PopupWindow. In order to edit and display text with Emojis this li

This library offers a simple method to add a small badge icon to your ActionBar-MenuItem
This library offers a simple method to add a small badge icon to your ActionBar-MenuItem

Android-ActionItemBadge ActionItemBadge is a library which offers a simple and easy to use method to add a badge to your action item! Screenshots Incl

Sentinel is a simple one screen UI which provides a standardised entry point for tools used in development and QA alongside device, application and permissions data.
Sentinel is a simple one screen UI which provides a standardised entry point for tools used in development and QA alongside device, application and permissions data.

Sentinel Sentinel is a simple one screen UI that provides standardised entry point for tools used in development and QA alongside device, application

Android StackBlur is a library that can perform a blurry effect on a Bitmap based on a gradient or radius, and return the result. The library is based on the code of Mario Klingemann.
Android StackBlur is a library that can perform a blurry effect on a Bitmap based on a gradient or radius, and return the result. The library is based on the code of Mario Klingemann.

Android StackBlur Android StackBlur is a library that can perform a blurry effect on a Bitmap based on a gradient or radius, and return the result. Th

Snake View is a simple and animated linear chart for Android.
Snake View is a simple and animated linear chart for Android.

Snake View Snake library is a simple and animation line chart for Android. Latest Version How to use Configuring your project dependencies Add the lib

A simple, customizable and easy to use swipeable view stack for Android.
A simple, customizable and easy to use swipeable view stack for Android.

SwipeStack A simple, customizable and easy to use swipeable view stack for Android. QuickStart Include the Gradle dependency dependencies { compil

Owner
Luke Klinker
I make cool things @klinker-apps.
Luke Klinker
Android View for displaying and selecting values in a circle-shaped View, with animations and touch gestures.

CircleDisplay Android View for displaying and selecting (by touch) values / percentages in a circle-shaped View, with animations. Features Core featur

Philipp Jahoda 287 Nov 18, 2022
Stepper Touch for Android based on MaterialUp submission

Stepper-Touch For more updates on this and other open-source projects, follow me on twitter ?? here Stepper Touch for Android based on a Material Up s

Dion Segijn 667 Jan 1, 2023
A view to show bling bling stars when you touch it.

AndroidGlitterView A view to show bling bling stars when you touch it. Demo Dependency compile('com.liangfeizc:glitterview:1.0.0@aar') Attributes name

Fei Liang 177 Nov 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 highly customizable, powerful and easy-to-use alerting library for Android.

Flashbar A highly customizable, powerful and easy-to-use alerting library for Android. Specs This library allows you to show messages or alerts in you

Aritra Roy 1.7k Dec 7, 2022
A powerful library for creating notifications in android platform.

Download Download the latest AAR or grab via Maven: <dependency> <groupId>com.github.halysongoncalves</groupId> <artifactId>pugnotification</artif

Halyson Lima Gonçalves 867 Nov 19, 2022
Dynamic Speedometer and Gauge for Android. amazing, powerful, and multi shape :zap:

SpeedView Dynamic Speedometer, Gauge for Android. amazing, powerful, and multi shape ⚡ , you can change (colors, bar width, shape, text, font ...every

Anas Altair 1.2k Jan 3, 2023
WizardTower - What will eventually be a Roguelike about being a powerful wizard, with a tower.

Wizard Tower Roguelike by sgibber2018 Description: This is a Roguelike I've been wanting to make for a long time. The premise is really simple: you ar

Sam Gibson 0 Jan 5, 2022
Simple and lightweight UI library for user new experience, combining floating bottom navigation and bottom sheet behaviour. Simple and beautiful.

Simple and lightweight UI library for user new experience, combining floating bottom navigation and bottom sheet behaviour. Simple and beautiful.

Rizki Maulana 118 Dec 14, 2022
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