Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent theme.dialog activity to allow outside touch?

Tags:

java

android

I have an activity that is using the Theme.Dialog style. It's a popup for my quiz game, for the wrong answer. But I have a problem. User can click outside the popup dialog themed activity and click on the next question. How to prevent that? I blocked back button and that works fine. Also, when a user clicks on the popup or outside of it, it starts counting the ON time again. My popup stays ON for 2500 ms. How to prevent that also?

So, basically I don't want to allow any click outside my popup and don't want to reset my delay time when someone clicks on the screen.

Here's the code of the popup window:

public class WrongAnswer extends Activity{
    MediaPlayer sound;
    TextView wrong;
    String correctAnswer, correct;

    public final int delayTime = 2500;
    private Handler myHandler = new Handler();

    public void onUserInteraction(){
        myHandler.removeCallbacks(closePopup);
        myHandler.postDelayed(closePopup, delayTime);
    }
    private Runnable zatvoriPopup = new Runnable(){
        public void run(){
            finish();
        }
    };

    @Override
    public void onBackPressed() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);


        setContentView(R.layout.wrong);


        Bundle extras = getIntent().getExtras(); 
        if(extras !=null) {
           tacno = extras.getString("correctAnswer");
        }

        inicijalizujVarijable();

        myHandler.postDelayed(closePopup, delayTime);


            }

    private void inicijalizujVarijable() {
        wrong = (TextView) findViewById(R.id.tvWrong);
        wrong.setText("Wrong answer!\nCorrect answer is:\n\n" + correct);
    }
    }

My activity in manifest:

<activity
            android:name="com.myquiz.myquizgame.WrongAnswer"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Dialog"
            android:screenOrientation="portrait"
             >
            <intent-filter>
                <action android:name="com.myquiz.myquizgame.WRONGANSWER" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
like image 481
marjanbaz Avatar asked Feb 04 '26 05:02

marjanbaz


1 Answers

If I got you correctly, this question had been already asked. Here is the answer, and according to it you need to call a setter method on activity, that will close your activity dialog:

this.setFinishOnTouchOutside(false);

I hope it will help you.

Second, every time when user touches WrongAnswer activity — you start new delayed task and cancel previous one here:

public void onUserInteraction() {
    myHandler.removeCallbacks(zatvoriPopup);
    myHandler.postDelayed(zatvoriPopup, delayTime);
}

that's why you have problems with timer

like image 157
Клаус Шварц Avatar answered Feb 05 '26 20:02

Клаус Шварц