Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable a whole activity from user action

Tags:

android

Is there a simple way to disable a user interacting with an activity. To be done when there is an action running (and a spinning progress bar in the title bar)

EDIT: As it seems I was not clear enough I meant to say: while I already have a spinning progress bar, the user is still able to push any button on the activity, I want to disable the user from being able to do that while the task is running. I do not want to however disable each item on the screen one by one.

Thanks, Jason

like image 732
Jason Avatar asked Nov 25 '10 20:11

Jason


2 Answers

In order to block user touch events, use:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); 

To get touch events back, use:

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); 

EDIT: If you want to add a feature of disable and greyed out display, you need to add in your xml layout file a linear layout that fills the parent. Set its background to #B0000000 and its visibilty to Gone. Than programicly set its visibility to Visible.

like image 137
Uriel Frankel Avatar answered Oct 21 '22 02:10

Uriel Frankel


If you need to disable event processing for a period of time (for instance, while you run an animation, show a waiting dialog), you can override the activity's dispatch functions.

To disable touch/clicks on any buttons, add these members/functions to your activity:

protected boolean enabled = true;  public void enable(boolean b) {     enabled = b; }  @Override public boolean dispatchTouchEvent(MotionEvent ev) {     return enabled ?          super.dispatchTouchEvent(ev) :          true;         } 

Then just call enable(true/false) when you need to disable and enable the activity's normal event handling.

like image 21
ricosrealm Avatar answered Oct 21 '22 04:10

ricosrealm