0

I am writing a recorder and it has buttons to start and to stop recording. Initially, recording is not occurring, so record button should be available, while stop button should be disabled, since it is nothing to stop.

How/where to implement this?

Additional problem is that my buttons are ImageButton-s, so probably I need an asset to represent disabled state. Unfortunately, I found no any way to define disabled state in selector?

Dims
  • 47,675
  • 117
  • 331
  • 600

3 Answers3

2

Without any sample code, I can't give any examples specific to your project. However, setting android:enabled="false" in your layout file for the Stop image button will inflate the layout with the Stop image button disabled by default.

As for representing disabled states, you can use drawable selectors. So let's say you have a file at res/drawable/stop_state.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false"  android:drawable="@drawable/stop_disabled" />
    <item android:drawable="@drawable/stop_enabled" />
</selector>

In the layout containing your ImageButton, you would set the android:drawable property to this drawable e.g.

android:drawable="@drawable/stop_state"

Likewise, make a drawable selector for your record button, and manage the enabled states within your activity using .setEnabled(boolean) based on if your app is currently recording.

Community
  • 1
  • 1
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
1

You can use

button.setEnabled(true) to make a button available to press.

and

you can use button.setEnabled(false) to make the button unavailable to press.

You can apply it like this

Intitally, in the on create method you can do this:-

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_in_app_billing);

        startButton = (Button)findViewById(R.id.buyButton);
        stopButton = (Button)findViewById(R.id.clickButton);
        stopButton.setEnabled(false);
    }

and then on the button press method

public void buttonClicked (View view)
    {
        stopButton.setEnabled(false);
        startButton.setEnabled(true);
    }
Abhriya Roy
  • 1,338
  • 17
  • 23
1

I've had a similar scenario, where I used a single button to handle play and pause, the scenario is identical, as far as I understood, you need to handle start and stop for recording. Here is my approach:

I kept a boolean to keep the state of the button, then inside the click listener of the button, I made the changes accordingly. Here's how:

        private boolean isTurnedOn = false;
        playButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view)
        {


            if(!isTurnedOn)
            {
                //playButton.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.pause));
                //isTurnedOn = true;
                //mPlayer.start();
                //mPlayer.seekTo(length);


            }
            else
            {
                //playButton.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.play2));
                //isTurnedOn = false;
                //mPlayer.pause();
                //length = mPlayer.getCurrentPosition();
            }

        }
    });

As you can see, you can handle both the requirements, including setting the image for the button as well as what happens on click inside the if else body.

Hope it helps.

OBX
  • 6,044
  • 7
  • 33
  • 77