How do I prevent theme.dialog activity from allowing outside touch?
I have an activity that uses the Theme.Dialog style. Here is the popup of my quiz game for incorrect answers. But I have a question. The user can click outside of the pop-up dialog topic Activity and click the next question. How can this be prevented? I blocked the back button and it worked great.
Also, when the user clicks on the pop-up window or its outside, it starts calculating the open time again. My popup stays open for 2500 milliseconds. How can it be prevented?
So, basically I don’t want to allow any clicks outside of my popup, nor do I want to reset my delay time when someone taps the screen.
The code for the pop-up window is as follows:
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 the list:
<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>
Solution
If I heard correctly, this question has already been asked. This is answer, according to which you need to call the setter method on the activity, which will close your activity dialog:
this.setFinishOnTouchOutside(false);
Hope this helps.
Second, every time the user touches the WrongAnswer activity — you start a new deferred task and cancel the previous task here:
public void onUserInteraction() {
myHandler.removeCallbacks(zatvoriPopup);
myHandler.postDelayed(zatvoriPopup, delayTime);
}
That’s why there’s something wrong with your timer