Creating Play/Pause dynamic button in Android

I've recently had to create a dynamic play/pause button for my Android app. After searching online for some time I've realized that there are not too many examples that present the exact solution that I need.

Therefore I've created a simple example below:

1. Define a class level "global" boolean variable and set it to false:

boolean pause = false;

2. defined the following button anywhere within the same class (activity) in any method that you want and set text to "play"

 this.buttonPause = (Button) mActivity.findViewById(R.id.pause_game);
 buttonPause.setText("Play");

3. Create an onClick Listener in the same method

 
        buttonPause.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (pause == false){
                buttonPause.setText("Pause");
                pause = true;

                {

                 else {

             buttonPause.setText("Play");
             pause = false;
             }
    }
 
        });
 
 
This is a very simple approach but gets the job done just fine.