Swipe-To-Dismiss-Dialog

We use some cool dialog with close button in our Android application but why if you can dismiss that dialog by Swipe and that features provide by Swipe Dismiss dialog which is an Android library which can turn any View into a draggable dialog with swipe to dismiss feature.

Demo Example of Swipe Dismiss dialog

Swipe Dismiss Dialog Success
Swipe Dismiss Dialog with Success
Swipe Dismiss Dialog with input
Swipe Dismiss Dialog with input
Swipe Dismiss Dialog with keyboard
Swipe Dismiss Dialog with keyboard

Download

Add this line to the build.gradle of your module:

compile 'com.github.andreilisun:swipedismissdialog:0.1'

Download Sourecode

How to use

Basic

Set View

  • View dialog = LayoutInflater.from(this).inflate(R.layout.dialog_add_user, null);
    new SwipeDismissDialog.Builder(this)
                    .setView(dialog)
                    .build()
                    .show();

From layout resource

  • new SwipeDismissDialog.Builder(this)
                    .setLayoutResId(R.layout.dialog_success_booking)
                    .build()
                    .show();
  • Callbacks

    Listen for the canceled by tap outside/back pressed events:

  • .setOnCancelListener(new OnCancelListener() {
        @Override
        public void onCancel(View view) {
            Toast.makeText(MainActivity.this, "Canceled", Toast.LENGTH_SHORT).show();
        }
    })

Listen for the swipe dismiss event:

.setOnSwipeDismissListener(new OnSwipeDismissListener() {
                    @Override
                    public void onSwipeDismiss(View view, SwipeDismissDirection direction) {
                        Toast.makeText(MainActivity.this, "Swipe dismissed to: " + direction, Toast.LENGTH_SHORT).show();
                    }
                })

Customization

You can set some parameters:

//Set fling gesture velocity. Range [0.0 - 1.0] 
//Default is 0.1f
.setFlingVelocity(0.07f)  
//Set horizontal oscillation. Range[0.0-35.0]
//Default is 35f
//This is the max rotation angle value
.setHorizontalOscillation(35f)
//Set overlay background color. Default is #80444444
.setOverlayColor(Color.parseColor("#80FF0000"))

Ex code

package com.github.andreilisun.sample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.github.andreilisun.swipedismissdialog.SwipeDismissDialog;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Button addUserButton = (Button) findViewById(R.id.btn_add_user);
        addUserButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAddUserDialog();
            }
        });

        Button showAlertButton = (Button) findViewById(R.id.btn_show_alert);
        showAlertButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAlertDialog();
            }
        });
    }

    private void showAddUserDialog() {
        View dialog = LayoutInflater.from(this).inflate(R.layout.dialog_add_user, null);
        final SwipeDismissDialog swipeDismissDialog = new SwipeDismissDialog.Builder(this)
                .setView(dialog)
                .build()
                .show();
        final EditText usernameEditText = (EditText) dialog.findViewById(R.id.et_username);
        Button addButton = (Button) dialog.findViewById(R.id.btn_add);
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addUserSuccess(usernameEditText.getText().toString());
                swipeDismissDialog.dismiss();
            }
        });
    }

    private void addUserSuccess(String username) {
        Toast.makeText(this, username + " has been added", Toast.LENGTH_SHORT).show();
    }

    private void showAlertDialog() {
        new SwipeDismissDialog.Builder(this)
                .setLayoutResId(R.layout.dialog_success_booking)
                .build()
                .show();
    }
}

 

By Ponglang Petrung

Administrator and PJ at Kamibit Thailand, Android Developer at CodeGears Co., Ltd. and Android Developer, and iOS Application Developer at Appdever

Share your thoughts

Leave a Reply

Loading Facebook Comments ...
Loading Disqus Comments ...