Reputation: 39
I've got a Activty which is displayed after a info-button is pressed. Now I want to close/ destroy the activty, when the user touches the screen, so that the user can get back to the main-activty again, without pressing the back-button of the device. How does the code look like.
I tried this, but it has no effect:
if(onTouchEvent(null)){
finish();
}
Thanks.
EDIT:
This is the whole code I have:
public class InfoDialog extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info_dialog); }
I use this Class just to show a short Information about the App with in the android-style of Theme.Dialog However, I want to add one function, to destroy/ close this activty, when the touchscreen is pressed, so that there is no need to press the back-button.
Upvotes: 1
Views: 4922
Reputation: 1208
Try this...
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.mainLayoutId);
mainLayout.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int eid = event.getAction();
switch (eid) {
case MotionEvent.ACTION_DOWN:
finish();
break;
}
return true;
}
});
Upvotes: 1
Reputation: 2824
Give an id to that view which displays your info. And then set a onTouch listener on that view and if use activity then finish the activity or if used visibility then make it invisible.
main = (RelativeLayout) findViewById(R.id.main);
main.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
case MotionEvent.ACTION_DOWN:
finish();
}
}
or
info = (RelativeLayout) findViewById(R.id.info);
info = (RelativeLayout) findViewById(R.id.info);
info.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
case MotionEvent.ACTION_DOWN:
info.setVisibilty(View.GONE);
}
}
Upvotes: 0
Reputation: 334
You should override onTouch(View v, MotionEvent event) something like that:
onTouch(View v, MotionEvent event){
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
finish();
}
}
Upvotes: 1