0

Is it possible to read users action in the dialog?

Example: On button bttn1 click, dialog opens. If user clicks No in the dialog, nothing happens. If user clicks Yes, then bttn1 becomes unavailable and bttn2 changes its state from setEnabled(false) to setEnabled(true).

General question but I am partially reffering to Bluetooth setup documentation where startActivityForResult() is used.

As current answer provides, it is about using startActivityForResult() and onActivityResult() but I still have no idea how to implement this in such condition.

I am not able to implement this to overcome such problem.

Community
  • 1
  • 1
Lisek
  • 753
  • 2
  • 11
  • 31

1 Answers1

1

Not sure if this is what you want, but you can create a dialog and listen to positive and negative button clicks.

new AlertDialog.Builder(this)
.setMessage("Deny or Grant?")
.setNegativeButton("Deny", new DialogInterface.OnClickListener() {    

    @Override
    public void onClick(DialogInterface arg0, int arg1) {
        // Do something on deny click.
    }
})
.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface arg0, int arg1) {
        // Do something on grant click.    
    }
})
.create().show();

EDIT:

To detect if the user has granted or denied bluetooth permission, you should override onActivityResult in your activity and check the resultCode. From the documentation:

A dialog will be displayed, requesting user permission to make the device discoverable, as shown in Figure 2. If the user responds "Yes," then the device will become discoverable for the specified amount of time. Your activity will then receive a call to the onActivityResult()) callback, with the result code equal to the duration that the device is discoverable. If the user responded "No" or if an error occurred, the result code will be RESULT_CANCELED.

Sapan Diwakar
  • 10,480
  • 6
  • 33
  • 43
  • The thing is I want to refer to the already existing log. In my situation: http://developer.android.com/images/bt_enable_request.png – Lisek Dec 05 '13 at 16:29
  • If it is just for bluetooth, you can check if the user granted access like by checking if the bluetooth is enabled. http://stackoverflow.com/a/7672404/812269 – Sapan Diwakar Dec 05 '13 at 16:32
  • Granting access should make one of my button to become enabled for a limited time only. The thing is, code proceeds without waiting for users decision in the dialog. So checking the discoverability of the device right away after granting access is not possible as it skips the `if(mBluetoothAdapter.getScanMode() == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE)`. – Lisek Dec 05 '13 at 16:39