Make a Popup appear long pressing on a view and handle drag-release events on its elements

Overview

LongPressPopup


You can try the demo app on google play store.
https://play.google.com/store/apps/details?id=rm.com.longpresspopupsample

Or see the full video demo on YouTube.
https://youtu.be/oSETieldmyw

A library that let you implement a behaviour similar to the Instagram's
long-press to show detail, with the option to put every kind of views inside it,
(even web views, lists, pagers and so on) show tooltips on drag over
and handle the release of the finger over Views

[Changelog] (CHANGELOG.md)

Download

Gradle:

compile 'com.rm:longpresspopup:1.0.1'

Min SDK version: 10 (Android 2.3.3)

Usage

Basic

Here's a basic example

public class ActivityMain extends AppCompatActivity {
        
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            Button btn = (Button) findViewById(R.id.btn_popup);
            
            // Create a foo TextView
            TextView textView = new TextView(this);
            textView.setText("Hello, Foo!");
            
            LongPressPopup popup = new LongPressPopupBuilder(this)// A Context object for the builder constructor
                    .setTarget(btn)// The View which will open the popup if long pressed
                    .setPopupView(textView)// The View to show when long pressed 
                    .build();// This will give you a LongPressPopup object
                    
            // You can also chain it to the .build() mehod call above without declaring the "popup" variable before 
            popup.register();// From this moment, the touch events are registered and, if long pressed, will show the given view inside the popup, call unregister() to stop
        }
}

Advanced

Here's a complete example with all the options

public class ActivityMain extends AppCompatActivity implements PopupInflaterListener,
            PopupStateListener, PopupOnHoverListener, View.OnClickListener {

    private static final String TAG = ActivityMain.class.getSimpleName();
     
    private TextView mTxtPopup;   
        
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button btn = (Button) findViewById(R.id.btn_popup);
        
        LongPressPopup popup = new LongPressPopupBuilder(this)
                        .setTarget(btn)
                        //.setPopupView(textView)// Not using this time
                        .setPopupView(R.layout.popup_layout, this)
                        .setLongPressDuration(750)
                        .setDismissOnLongPressStop(false)
                        .setDismissOnTouchOutside(false)
                        .setDismissOnBackPressed(false)
                        .setCancelTouchOnDragOutsideView(true)
                        .setLongPressReleaseListener(this)
                        .setOnHoverListener(this)
                        .setPopupListener(this)
                        .setTag("PopupFoo")
                        .setAnimationType(LongPressPopup.ANIMATION_TYPE_FROM_CENTER)
                        .build();
                
        // You can also chain it to the .build() mehod call above without declaring the "popup" variable before 
        popup.register();
    }
    
    
    // Popup inflater listener
    @Override
    public void onViewInflated(@Nullable String popupTag, View root) {
        mTxtPopup = (TextView) root.findViewById(R.id.txt_popup);
    }
    
    
    // Touch released on a View listener
    @Override
    public void onClick(View view) {
        if (mTxtPopup != null && view.getId() == mTxtPopup.getId()) {
            Toast.makeText(ActivityMain.this, "TextView Clicked!", Toast.LENGTH_SHORT).show();
        }
    }
    
    
    // PopupStateListener
    @Override
    public void onPopupShow(@Nullable String popupTag) {
        if(mTxtPopup != null) {
            mTxtPopup.setText("FooBar!");
        }
    }
    
    @Override
    public void onPopupDismiss(@Nullable String popupTag) {
        Toast.makeText(this, "Popup dismissed!", Toast.LENGTH_SHORT).show();
    }
    
    
    // Hover state listener
    @Override
    public void onHoverChanged(View view, boolean isHovered) {
        Log.e(TAG, "Hover change: " + isHovered + " on View " + view.getClass().getSimpleName());
    }
}



And here are the functions you can use to customize the Popup and it's behaviour from the LongPressPopupBuilder class:

  • public LongPressPopupBuilder setTarget(View target) (null by default)
    Select which view will show the popup view if long pressed

  • public LongPressPopupBuilder setPopupView(View popupView) (null by default)
    Select the view that will be shown inside the popup

  • public LongPressPopupBuilder setPopupView(@LayoutRes int popupViewRes, PopupInflaterListener inflaterListener) (0, null by default)
    Select the view that will be shown inside the popup, and give the popup that will be
    called when the view is inflated (not necessarily when shown, so not load images and so on in this callback,
    just take the views like in the OnCreate method of an Activity)

  • public LongPressPopupBuilder setLongPressDuration(@IntRange(from = 1) int duration) (500 by default)
    Pretty self explanatory right? **Captain here, the long press time needed to show the popup

  • public LongPressPopupBuilder setDismissOnLongPressStop(boolean dismissOnPressStop) (true by default)
    Set if the popup will be dismissed when the user releases the finger (if released on a View inside
    the popup, the View's or the given OnClickListener will be called)

  • public LongPressPopupBuilder setDismissOnTouchOutside(boolean dismissOnTouchOutside) (true by default)
    If setDismissOnLongPressStop(boolean dismissOnPressStop) is set to false, you can choose to make
    the popup dismiss or not if the user touch outside it with this boolean

  • public LongPressPopupBuilder setDismissOnBackPressed(boolean dismissOnBackPressed) (true by default)
    If setDismissOnLongPressStop(boolean dismissOnPressStop) is set to false, you can choose to make
    the popup dismiss or not if the user press the back button

  • public LongPressPopupBuilder setCancelTouchOnDragOutsideView(boolean cancelOnDragOutside) (true by default)
    Set if the long press timer will stop or not if the user drag the finger outside the target View
    (If the target View is inside a scrolling parent, when scrolling vertically the long press timer
    will be automatically stopped

  • public LongPressPopupBuilder setLongPressReleaseListener(View.OnClickListener listener) (null by default)
    This is a standard OnClickListener, which will be called if the user release the finger on a view inside
    the popup, you can use this method or set a standard OnClickListener on the View you want, it will be called
    automatically for you

  • public LongPressPopupBuilder setOnHoverListener(PopupOnHoverListener listener) (null by default)
    This listener will be called every time the user keeps dragging it's finger inside or outside the popup
    views, with a View reference and a boolean with the hover state

  • public LongPressPopupBuilder setPopupListener(PopupStateListener popupListener) (null by default)
    This listener will be called when the popup is shown or dismissed, use this listener to load images or compile text views and so on

  • public LongPressPopupBuilder setTag(String tag) (null by default)
    This method sets a tag on the LongPressPopup, the given tag will be returned in all the listeners. You can also set it in the build(String tag)
    method

  • public LongPressPopupBuilder setAnimationType(@LongPressPopup.AnimationType int animationType) (none by default)
    This method set the opening and closing animation for the popup, can be none or from-to Bottom, Top, Right, Left, Center



Also, the LongPressPopup class gives some utility methods, like

  • public void register()
    Which means that the popup is listening for touch events on the given view to show itself

  • public void unregister()
    Which makes to popup stop listening for touch events and dismiss itself if open

  • public void showNow()
    Which shows immediately the popup
  • public void dismissNow()
    Which dismiss immediately the popup if open

Known Bugs

  • This library do not work correctly with ListViews (Use RecyclerViews!)



License

Copyright 2017 Riccardo Moro.

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.
Comments
  • Problem with padding

    Problem with padding

    Hi.. I have a linearlayout for the popup, with an imageview and two textviews below the imageview. The first time i open a popup, the image seems to have a small top padding. If i open the same popup again, the padding is gone. Anyone has an idea, what happens there?

    opened by mschakulat 6
  • setLongPressDuration?

    setLongPressDuration?

    Hi, popup keeps on showing even if I increase duration. Below is my code:

     final LongPressPopup popup = new LongPressPopupBuilder(holder.itemView.getContext())
                    .setTarget(holder.itemView.findViewById(R.id.row_product_item_image))
                    .setPopupView(v)
                    .setLongPressDuration(1250)
                    .setDismissOnLongPressStop(true)
                    .build();
            v.findViewById(R.id.btnPopUpCancel).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popup.dismissNow();
                }
            });
            popup.register();
    

    I also have setDismissOnLongPressStop, but it doesn't dismiss unless I tap on the cancel button I added to v (Popup view), and the popup still shows when the new activity is on the screen. LongPressDuration just delays it and doesn't reset if user stops tapping target view

    opened by OmarBizreh 3
  • Show image on longpress.

    Show image on longpress.

    When I click on the image it sometimes appears with a zoom there and I have to open the popup again so that it appears correctly, besides a white left side margin.

    Follows the first image after the first click with a strange zoom.

    screenshot_2017-03-08-16-45-53

    Follow the second image again after the click already presented normally. Both are the same pictures.

    screenshot_2017-03-08-16-46-01

    To the right side can be seen a white margin what can be this difference?

    His lib is incredible, and from the outset his enormous attention and help.

    opened by Jsaiao 1
  • Change the dispatch click system

    Change the dispatch click system

    Would you consider changing the way the library dispatches the click to the leaf nodes only by instead checking if non-leaf nodes have a click listener attached and giving priority to those? This means that if i have a ViewGroup with a click listener and some items inside, everywhere i lift my finger in this view group, the click will be dispatched to the view group instead of the children.

    Alternatively we can also inject this behavior and make it custmizable.

    Happy to open a PR for this and contribute just let me know what you think.

    opened by dvdciri 1
  • SwipeRefreshLayout breaks the ability to drag finger to the view

    SwipeRefreshLayout breaks the ability to drag finger to the view

    I really like this library :) But I discovered an issue: I am using an swipeRefreshLayout around my recyclerview and when I am long pressing an item in this recyclerview, the popup shows up, but when I want to drag my finger down to the buttons of the view, the swipeRefreshLayout gets triggered and the popup closes. How can I disable the swipeRefreshLayout while the popup is open?

    opened by Simolation 0
  • setDismissOnLongPressStop don't work on my adapter

    setDismissOnLongPressStop don't work on my adapter

    Thanks to release this useful library, unfortunately in my adapter setDismissOnLongPressStop don't work, could you check this code please?

        public class UserChannelsViewHolder extends RecyclerView.ViewHolder
                implements PopupInflaterListener, View.OnClickListener, PopupStateListener, PopupOnHoverListener {
            ...
            private LongPressPopup mPopup;
            private ImageView      mPopupImg;
            private TextView       mPopupTitle;
    
            public UserChannelsViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
    
                title.setTextSize(AndroidUtilities.dp(15));
                description.setTextSize(AndroidUtilities.dp(11));
    
                iconCafeWebUserCount.setTextSize(AndroidUtilities.dp(11));
                iconVisitCafeWeb.setTextSize(AndroidUtilities.dp(11));
    
                title.setTypeface(FontManager.getInstance(context).loadFont("fonts/vazir_bold.ttf"));
                description.setTypeface(FontManager.getInstance(context).loadFont("fonts/vazir.ttf"));
    
                mPopup = new LongPressPopupBuilder(itemView.getContext())
                        .setTarget(imageAvatar)
                        .setPopupView(R.layout.popup_layout, this)
                        .setAnimationType(LongPressPopup.ANIMATION_TYPE_FROM_CENTER)
                        .setPopupListener(this)
                        .setOnHoverListener(this)
                        .setLongPressDuration(750)
                        .setDismissOnLongPressStop(true)
                        .setDismissOnTouchOutside(true)
                        .setDismissOnBackPressed(true)
                        .setCancelTouchOnDragOutsideView(true)
                        .setLongPressReleaseListener(this)
                        .build();
                mPopup.register();
            }
    
            @Override
            public void onClick(View v) {
    
            }
    
            @Override
            public void onHoverChanged(View view, boolean isHovered) {
                if (isHovered) {
                    if (view.getId() == mPopupTitle.getId()) {
                    } else if (view.getId() == mPopupImg.getId()) {
                    }
                }
            }
    
            @Override
            public void onPopupShow(@Nullable String popupTag) {
                if (mPopupImg != null) {
                    Picasso.with(context)
                            .load("http://cdn.time.ir/App_Themes/Default-fa-IR/Images/summerHeader.jpg")
                            .networkPolicy(NetworkPolicy.OFFLINE)
                            .into(mPopupImg);
                }
    
                if (mPopupTitle != null) {
                    mPopupTitle.setText(list.get(getAdapterPosition()).getCafeName());
                }
            }
    
            @Override
            public void onPopupDismiss(@Nullable String popupTag) {
    
            }
    
            @Override
            public void onViewInflated(@Nullable String popupTag, View root) {
                mPopupImg = (ImageView) root.findViewById(R.id.popup_img);
                mPopupTitle = (TextView) root.findViewById(R.id.popup_title);
    
                mPopupTitle.setOnClickListener(this);
            }
        }
    }
    
    
    opened by pishguy 15
Owner
Riccardo Moro
Just an Android developer and enthusiast
Riccardo Moro
[Deprecated] This project can make it easy to theme and custom Android's dialog. Also provides Holo and Material themes for old devices.

Deprecated Please use android.support.v7.app.AlertDialog of support-v7. AlertDialogPro Why AlertDialogPro? Theming Android's AlertDialog is not an eas

Feng Dai 468 Nov 10, 2022
Make your native android Dialog Fancy and Gify. A library that takes the standard Android Dialog to the next level with a variety of styling options and Gif's. Style your dialog from code.

FancyGifDialog-Android Prerequisites Add this in your root build.gradle file (not your module build.gradle file): allprojects { repositories { ...

Shashank Singhal 522 Jan 2, 2023
Extremely useful library to validate EditText inputs whether by using just the validator for your custom view or using library's extremely resizable & customisable dialog

Extremely useful library for validating EditText inputs whether by using just the validator (OtpinVerification) for your custom view or using library's extremely resizable & customisable dialog (OtpinDialogCreator)

Ehma Ugbogo 17 Oct 25, 2022
Android has a built in microphone through which you can capture audio and store it , or play it in your phone. There are many ways to do that but with this dialog you can do all thats with only one dialog.

# Media Recorder Dialog ![](https://img.shields.io/badge/Platform-Android-brightgreen.svg) ![](https://img.shields.io/badge/Android-CustomView-blue.sv

Abdullah Alhazmy 73 Nov 29, 2022
SweetAlert for Android, a beautiful and clever alert dialog

Sweet Alert Dialog SweetAlert for Android, a beautiful and clever alert dialog 中文版 Inspired by JavaScript SweetAlert Demo Download ScreenShot Setup Th

书呆子 7.3k Dec 30, 2022
🗣 An overlay that gets your user’s voice permission and input as text in a customizable UI

Overview Voice overlay helps you turn your user's voice into text, providing a polished UX while handling for you the necessary permission. Demo You c

Algolia 228 Nov 25, 2022
😍 A beautiful, fluid, and extensible dialogs API for Kotlin & Android.

Material Dialogs View Releases and Changelogs Modules The core module is the fundamental module that you need in order to use this library. The others

Aidan Follestad 19.5k Dec 29, 2022
AlertDialog for Android, a beautiful and material alert dialog to use in your android app.

AlertDialog for Android, a beautiful and material alert dialog to use in your android app. Older verion of this library has been removed

Akshay Masram 124 Dec 28, 2022
Sleek dialogs and bottom-sheets for quick use in your app

⭐ ‎‎‎‏‏‎ ‎Offers a range of beautiful sheets (dialogs & bottom sheets) for quick use in your project. Includes many ways to customize sheets.

Maximilian Keppeler 835 Dec 25, 2022
An easy-to-use Android library that will help you to take screenshots of specif views of your app and save them to external storage (Including API 29 Q+ with Scope Storage)

???? English | ???? Português (pt-br) ???? English: An easy to use Library that will help you to take screenshots ?? of the views in your app Step 1.

Thyago Neves Silvestre 2 Dec 25, 2021
An beautiful and easy to use dialog library for Android

An beautiful and easy to use dialog library for Android

ShouHeng 22 Nov 8, 2022
In this You can Calculate Your Age and that Age will be Appear on Birthday Card with Message.

CalculateAge & Create Birthday Card This is the Extension Version of BirthdayCard In this You can Calculate Your Age and that Age will be Appear on Bi

GDSC Indus University 1 Oct 8, 2021
Example Jetpack Compose Android App, that uses the newest mechanisms, like StateFlow, SharedFlow, etc. to manage states and handle events. ViewModel, UI and Screenshot tests included :)

AndroidMVIExample Example Jetpack Compose Android App, that uses the newest mechanisms, like StateFlow, SharedFlow, etc. to manage states and handle e

Patryk Kosieradzki 55 Nov 18, 2022
Screen Capture Utils - A plugin to handle screen capture events on android and ios

Screen Capture Utils A plugin to handle screen capture events on android and ios ?? Initialize SDK late ScreenCaptureUtils screenCaptureUtils;

Chiziaruhoma Ogbonda 41 Apr 12, 2022
Custom view to expand long text with view more,less action , you can customize min lines , action color

ExpandableTextView Custom expadable text view Examples : <mostafa.projects.expandabletextview.ExpandableTextView android:layout_wi

Mostafa Gad 8 Jan 25, 2022
Under the Hood is a flexible and powerful Android debug view library. It uses a modular template system that can be easily extended to your needs, although coming with many useful elements built-in.

Under the Hood - Android App Debug View Library Under the Hood is a flexible and powerful Android debug view library. It uses a modular template syste

Patrick Favre-Bulle 217 Nov 25, 2022
Provides 9-patch based drop shadow for view elements. Works on API level 9 or later.

Material Shadow 9-Patch This library provides 9-patch based drop shadow for view elements. Works on API level 14 or later. Target platforms API level

Haruki Hasegawa 481 Dec 19, 2022
Provides 9-patch based drop shadow for view elements. Works on API level 9 or later.

Material Shadow 9-Patch This library provides 9-patch based drop shadow for view elements. Works on API level 14 or later. Target platforms API level

Haruki Hasegawa 481 Dec 19, 2022
A TextView that automatically fit its font and line count based on its available size and content

AutoFitTextView A TextView that automatically fit its font and line count based on its available size and content This code is heavily based on this S

null 899 Jan 2, 2023