2

I can detect user interaction in an activity with this code:

@Override
public void onUserInteraction() {
    super.onUserInteraction();
}

But Android Dialog has no method like that. And I couldn't find the way to handle it.

How can I do that?

Burak
  • 5,706
  • 20
  • 70
  • 110

1 Answers1

0

You can use AppCompatActivity styled as dialog with style instead of regular Android Dialog

<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
...
</style>

Then you could use functionality of Activity class

You also should use following onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(android.support.v7.appcompat.R.layout.abc_alert_dialog_material);
}

To make it behave like dialog you manually need to find all the views you need to attach click listeners to each button.

Here are some IDs you may find useful:

@BindView(android.support.v7.appcompat.R.id.topPanel)
protected ViewGroup mTopPanel;
@BindView(android.support.v7.appcompat.R.id.contentPanel)
protected ViewGroup mContentPanel;
@BindView(android.support.v7.appcompat.R.id.customPanel)
protected ViewGroup mCustomPanel;
@BindView(android.support.v7.appcompat.R.id.custom)
protected ViewGroup mCustomViewContainer;
@BindView(android.support.v7.appcompat.R.id.textSpacerNoButtons)
protected View mTextSpacer;
@BindView(android.support.v7.appcompat.R.id.buttonPanel)
protected ButtonBarLayout mButtonPanel;

@BindView(android.R.id.message)
protected TextView mMessage;
@BindView(android.R.id.button1)
protected Button mPositiveButton;
@BindView(android.R.id.button2)
protected Button mNegativeButton;
@BindView(android.R.id.button3)
protected Button mNeutralButton;

(Butterknife annotations included, if you are not using Butterknife just use findViewById with each annotation from @BindView(id))

mhenryk
  • 551
  • 5
  • 13