Android: Enable / Disable Button in Runtime?

So, I have two buttons. The first is “Download Text,” and the second is “Speak.”

Now I do not want the Speak button to be active until the text is loaded.

I managed to set the value in EditText with the Load Text onClickListener button. Inside the same method, I called

btnSpeak.setEnabled(true);

I initialized it as,

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

All encoding

@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 a status check and language assignment for TTS for future reference. I get a toast like “Button should work,” but it doesn't activate. Why is this so? What job?

I have a .xml file like,

<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 enable it here and then disable and enable it at runtime?

+5
source share
6 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!");
    }

}
+11
source

setClickable

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

, isEnabled()

//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();
}
+5

Check that 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);
}

Hope this helps you.

+3
source

It may be too late to answer, and you have 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();
+2
source

which you can achieve by disabling and enabling the visibility of the button

here is an example

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

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;
        }
    }
});
0
source

All Articles