Search code examples
androidbuttononclickonactivityresult

Disable Button that triggered an ActivityforResult


My code goes like this: - A button triggers one Activity on result, meaning that the activity should give back a result. - When the activity finishes and returns to the activity where the button was clicked, I would like to disable this button.

I created the buttons dynamically, this is why I have to use them through View v on the onClick method.

 public void onClick(View v) {
    b= (Button)v;

    Intent ir_a_emociones = new Intent(this,emociones_votar.class);
    ir_a_emociones.putExtra("alumno",alumno);
    ir_a_emociones.putExtra("asignatura",b.getText().toString());
    startActivityForResult(ir_a_emociones, 55);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 55) {
        if (resultCode == RESULT_OK) {
            Toast.makeText(this, "¡Gracias!", Toast.LENGTH_SHORT).show();
           ***--> here the button that triggered the intent should be disabled.***
        }
    }
}

There it is the onclick and the onActivityResult. I don't know how to referenciate back the button. B is a button, but as far as I know, it just let me get the references inside onClick.


Solution

  • You can setEnabled(boolean value) of button on activity result. For more detail open Android documentation link

    private Button b;
    // 
    public void onClick(View v) {
        b= (Button)v;
    
        Intent ir_a_emociones = new Intent(this,emociones_votar.class);
        ir_a_emociones.putExtra("alumno",alumno);
        ir_a_emociones.putExtra("asignatura",b.getText().toString());
        startActivityForResult(ir_a_emociones, 55);
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 55) {
            if (resultCode == RESULT_OK) {
                Toast.makeText(this, "¡Gracias!", Toast.LENGTH_SHORT).show();
               if(b!= null){
                 b.setEnable(false) // false :: Disable button, true :: Enable button
               }
            }
        }
    }