M.A.Murali
M.A.Murali

Reputation: 10148

how to disable ok button in alert box in android

in my app i will show a alert box with message and ok button. in the background i hit some api and saving it in to database(i do it in Async Task). my requirement is i want to disable the ok button till the background process complete. After the completion of background process i need to enable the ok button in alert box.

my alert box creation code:

    helpBuilder1 = new AlertDialog.Builder(Home.this);
    helpBuilder1.setTitle("Welcome");
    helpBuilder1.setMessage("Thank you for waiting while App Installs");

    mOkButton = helpDialog1.getButton(AlertDialog.BUTTON_POSITIVE);
    mOkButton.setEnabled(false);

    if(mOkButton.isEnabled()==false){
        Log.e("Home.java - ShowPopUp", "button is disablled");
        new First_Time_Sync().execute();
    }

    mOkButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            helpDialog1.dismiss();
            showSilde_show_PopUp();

        }
    });

    helpDialog1 = helpBuilder1.create();
    helpDialog1.show(); 

please help me.

Upvotes: 0

Views: 1376

Answers (2)

stefan
stefan

Reputation: 10345

unfortunately there seems to be no straight forward API way. however this is my idea of more of a custom solution: [Update while typing: Rajdeeps solution sounds reasonable, try this first ;-) If it doesn't help, try this one.]

create a custom AlertDialog layout with your button and add it to your AlertDialog with

LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.yourlayout,(ViewGroup) findViewById(R.id.yourLayoutsOuterLinearLayout));
builder.setView(view);

the layout can look something like that:

<LinearLayout
  android:id="@+id/yourLayoutsOuterLinearLayout"
  android:layout_height:...>
  <Button
    android:id="@+id/myButton"
    android:...>
  </Button>
</LinearLayout>

Upvotes: 0

Rajdeep Dua
Rajdeep Dua

Reputation: 11230

Use getButton() on the AlertDialog to get hold of the underlying ok button

Button mOkButton = helpDialog1.getButton();
mOkButton.setEnabled(false)`

Upvotes: 1

Related Questions