Disable other buttons when Record button active

Cule :

I have 4 items in the menu and 1 button Rec / Stop. I want, when the Rec button is active and records, the other 4 items in the menu items are disabled. Please help me.

this is activity_main.xml

<ToggleButton
    android:id="@+id/recStop"
    android:layout_width="65dp"
    android:layout_height="65dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="100dp"
    android:background="@drawable/tbutton"
    android:text=""
    android:textOff=""
    android:textOn="" />

this is tbutton.xml

<item android:drawable="@drawable/rec"
android:state_checked="false" />

<item android:drawable="@drawable/stop"
android:state_checked="true" />

this is MainActivity.java

private ToggleButton toggleButton;

toggleButton = (ToggleButton) findViewById(R.id.recStop);

// Button Rec / Stop
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView,
                                 boolean isChecked) {

        if (isChecked) {
            speech.setRecognitionListener(VoiceRecognitionActivity.this);
            progressBar.setVisibility(View.VISIBLE);
            progressBar.setIndeterminate(true);
            speech.startListening(recognizerIntent);
        } else {
            progressBar.setIndeterminate(false);
            progressBar.setVisibility(View.INVISIBLE);
            speech.stopListening();
            speech.destroy();

        }

    }
});

this is MainActivity.java

// Menu items
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
    case R.id.copy:
        break;
}
switch (item.getItemId()) {
    case R.id.share:
        break;
}
switch (item.getItemId()) {
    case R.id.clear:
        break;
}
switch (item.getItemId()) {
    case R.id.about:
        break;
}
return super.onOptionsItemSelected(item);
}
karan :

Inside each condition of your switch...case, check the status of togglebutton and if its checked avoid further actions in it. Also don't use multiple switch, you must define multiple cases inside it see the code below.

switch (item.getItemId()) {
    case R.id.copy:
        if(toggleButton.isChecked()) {
             //display warning message
        } else {
             // your regular code here
        }
        break;

    case R.id.share:
        if(toggleButton.isChecked()) {
             //display warning message
        } else {
             // your regular code here
        }
        break;

    case R.id.clear:
        if(toggleButton.isChecked()) {
             //display warning message
        } else {
             // your regular code here
        }

        break;

    case R.id.about:
        if(toggleButton.isChecked()) {
             //display warning message
        } else {
             // your regular code here
        }
        break;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=158432&siteId=1