Cheryl Simon
Cheryl Simon

Reputation: 46844

Android: Force dialog themed activity to be modal

A button on our screen causes an activity to be shown that has a "dialog" theme. We are having an issue were if you click the button quickly twice in a row, the dialog activity is opened twice.

Typically I would expect that when a new activity is started, the underlying activity is immediately stopped, and thus accepts no further input.

However, since the dialog themed activity does not take over the whole screen, I think the underlying activity is only paused, not stopped and thus the buttons are still accessible.

Which brings me to my question... Is there a way to force the dialog themed activity into a modal state where the user can't click the buttons on the activity below?

I could probably manually accomplish this by disabling everything in onPause, and reenabling it in onResume, but this seems like a lot of work! Anyone have an easier solution?

Upvotes: 3

Views: 1939

Answers (2)

Dent
Dent

Reputation: 500

Another solution is to not launch your activity with a dialog theme but a standard one. In your XML for the activity specify, say a textview, that occupies the entire screen. However make the textview transparent (or semi or colored or ....) and clickable.

Then, in the same xml file, make your "dialog" so that it displays on top of the textview. Now it looks like a dialog, the activity behind it can still be seen, but clicks outside of your dialog are are consumed by the transparent textview. For example:

<TextView android:id="@+id/ViewHider"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#aa000000"
    android:enabled="true"
    android:clickable="true"
    ></TextView>

<!-- Then put your "dialog" xmal here -->

Upvotes: 0

Patrick Kafka
Patrick Kafka

Reputation: 9892

Along the lines of disabling things (which seens hacky and wrong), but if there isn't a real solution. The disabling could be done with a simple return in the button click event. As long as you reset the bool when the dialog returns or in onResume

boolean clicked;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button b = (Button)findViewById(R.id.Button01);
    b.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (clicked)
                return;
            clicked = true;
            // show dialog
        }
    });
}

Upvotes: 1

Related Questions