Search code examples
androiddialogandroid-seekbar

Android Seekbar as Dialog


I'm creating button to change the volume and i want that onClick, a seekbar appears as a sort of dialog(but not the one that covers the screen and does not allow interaction with other elements on the screen) below the button and it should disappear after a few seconds.I do not want to use the alertDialog that the android provides as that covers the whole activity.How do I go about this? Also is there any library to achieve this?


Solution

  • Put your seekBar below your button in your xml layout with alpha="0", and try:

        SeekBar mSeekbar;
        int delay = 1000;
        Button mButton;
        Handler mHandler;
        Runnable mRunnable = new Runnable() {
            @Override
            public void run() {
                Animation anim = new AlphaAnimation(1, 0);
                anim.setDuration(500);
                mSeekbar.startAnimation(anim);
            }
        };
    
    
        //inside onCreate (or wherever you decide)
        mHandler = new Handler(); //(android.os)
        mButton = (Button) findViewById(R.id.button1);
    
        mSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    
            }
    
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                mHandler.removeCallbacks(mRunnable);
            }
    
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mHandler.postDelayed(mRunnable, delay);
            }
        });
    
    
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mSeekbar.getAlpha() == 0) {
                    Animation anim = new AlphaAnimation(0, 1);
                    anim.setDuration(500);
                    anim.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
    
                        }
    
                        @Override
                        public void onAnimationEnd(Animation animation) {
                            mHandler.postDelayed(mRunnable, delay);
                        }
    
                        @Override
                        public void onAnimationRepeat(Animation animation) {
    
                        }
                    });
                    mSeekbar.startAnimation(anim);
                }
            }
        });