Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Enable / Disable a button in Runtime?

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??

like image 730
iMan Avatar asked Apr 23 '13 11:04

iMan


People also ask

How do I make my Android button disabled?

setAlpha(. 5f); button. setClickable(false);

How make button not clickable in android programmatically?

setClickable(false); Animation translateAnimation=new TranslateAnimation(0, 0, 0,150 ); translateAnimation.

Can buttons be disabled?

A disabled button is unusable and un-clickable. The disabled attribute can be set to keep a user from clicking on the button until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the button clickable again.


1 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!");
    }

}
like image 175
MR. Kumar Avatar answered Sep 21 '22 20:09

MR. Kumar