Search code examples
javaandroidandroid-studiobuttoncountdown

how can i add multiple actions for one Button


i try to add play and pause action in one button this is my code i just add play action, can you help me, please?

 fab_play_pause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            timerHasStarted = true;
            countDownTimer.start();


            Resources res = getResources();
            Drawable drawable = res.getDrawable(R.drawable.custom_progressbar_drawable);
            final ProgressBar mProgress = (ProgressBar) rootView.findViewById(R.id.circularProgressbar);
            mProgress.setProgress(0);   // Main Progress
            mProgress.setSecondaryProgress(100); // Secondary Progress
            mProgress.setMax(100); // Maximum Progress
            mProgress.setProgressDrawable(drawable);
            mProgress.setRotation(0);

            ObjectAnimator animation = ObjectAnimator.ofInt(mProgress, "progress", 0, 100);
            animation.setDuration(startTime);
            animation.setInterpolator(new DecelerateInterpolator());
            animation.start();

        }
    });

Solution

  • You can achieve it by this:

    set it default to Play

        fab_play_pause.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                 String text = btnAdd.getText().toString();
    
                   if(text.equalsIgnoreCase("Play")){
                      //your code
                       fab_play_pause.setText("pause")
                   }
                  else if(text.equalsIgnoreCase("pause"))
                   {
                      //your code
                      fab_play_pause.setText("Play")
                   }
                }
            });
    

    For Drawble :

     fab_play_pause.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                    int resId = (Integer) view.getTag();
    
                       if(resId == R.drawable.play){
                          //your code
                          //change background for pause
                       }
                      else if(resId == R.drawable.pause)
                       {
                          //your code
                          //change background for play
                       }
                    }
                });