Home » Android » Android: Enable / Disable a button in Runtime?

Android: Enable / Disable a button in Runtime?

Posted by: admin June 15, 2020 Leave a comment

Questions:

Ok, so I have two buttons. The first one is to “Load Text” and the second is to “Speak Out”.

Now, I don’t want the Speak button to be active while there is no text loaded.

I’ve managed to set value into the EditText by Load Text button’s onClickListener method. Inside the same method I have called,

btnSpeak.setEnabled(true);

I have initialized this as,

btnSpeak = (Button) findViewById(R.id.button1);

The entire coding is,

@Override
public void onInit(int status) {

    if (status == TextToSpeech.SUCCESS) {

        int result = tts.setLanguage(Locale.US);

        if (result == TextToSpeech.LANG_MISSING_DATA
        || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS", "This Language is not supported");
        } else {
            btnSpeak.setEnabled(true);
            //for checking
            if(btnSpeak.isEnabled())
            {
                Toast.makeText(SimpleAndroidOCRActivity.this, "Button should work!", Toast.LENGTH_SHORT).show();
            }
            else
            {
                Toast.makeText(SimpleAndroidOCRActivity.this, "Button should not work!", Toast.LENGTH_SHORT).show();
            }
        }

    } else {
        Log.e("TTS", "Initilization Failed!");
    }

}

This is to check the status and assign language to the TTS for further use. I get the toast as “Button should work” but it doesn’t get enabled. Why is it so? What’s the work around?

I have in .xml file as,

<Button
    android:id="@+id/button1"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_width="200dp"
    android:enabled="false"
    android:text="@string/tts_text" />

Should I have it enabled in here and then disable and enable in runtime??

How to&Answers:
@Override
public void onInit(int status) {

    if (status == TextToSpeech.SUCCESS) {

        int result = tts.setLanguage(Locale.US);

        if (result == TextToSpeech.LANG_MISSING_DATA
        || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS", "This Language is not supported");
        } else {
            btnSpeak.setEnabled(true);
            //for checking
            if(btnSpeak.isEnabled())
            {
                btnSpeak.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        YourVoicemethod();

                        Toast.makeText(SimpleAndroidOCRActivity.this, "Button should work!", Toast.LENGTH_SHORT).show();
                    }


                });
            }
            else
            {
                Toast.makeText(SimpleAndroidOCRActivity.this, "Button should not work!", Toast.LENGTH_SHORT).show();
            }
        }

    } else {
        Log.e("TTS", "Initilization Failed!");
    }

}

Answer:

You have to use setClickable

btnSpeak.setClickable(true);
btnSpeak.setEnabled(true);

Also, use isEnabled() to check the state

//for checking
if(btnSpeak.isEnabled())
{
  Toast.makeText(SimpleAndroidOCRActivity.this, "Button should work!", Toast.LENGTH_SHORT).show();
}
else
{
  Toast.makeText(SimpleAndroidOCRActivity.this, "Button should not work!", Toast.LENGTH_SHORT).show();
}

Answer:

Check your button is pressed or not and write your code.

Sample code.

if(button2.isPressed()){
    button2.setEnabled(false);
    button1.setEnabled(true);
}
else if(button1.isPressed()){
    button1.setEnabled(false);
    button2.setEnabled(true);
}

I hope this will help you.

Answer:

May be is too late to answer and you already found a solution, but you can use invalidate to force the button to redraw itself according to its new state:

btnSpeak.setEnabled(true);
btnSpeak.invalidate();

Answer:

you can achieve by disable and enabling the visibility of button

here is an example

View button = findViewById(R.id.buttonid);
button.setVisibility(View.GONE);

Answer:

Use one listener for both buttons

button1.setOnClickListener(listener);
button2.setOnClickListener(listener);

listener = new OnClickListener({
    @Override
    public void onClick(View v) {
        switch(button_id){
            case id1:
            button2.setEnabled(false);
            button1.setEnabled(true);
            break;
            case id2:
            button1.setEnabled(false);
            button2.setEnabled(true);
            break;
        }
    }
});