Hi All,

I have a set of requirements that I'm not sure how to solve with
Android.

1) Initially show a set of views to the user.
2) After something happens in the app, set a timer for a few seconds
and fade the views so they are invisible.
3) If the user single taps on the application, fade the views back so
they are visible.
4) While the user interacts with the views, they will remain visible.
5) When the user stops interacting, fade the views away again after
the timeout.

So, I started playing with the Skeleton Application template. I saw
that Activity has an onUserInteraction method. After experiementation
I found that it is fired less frequently than I would like. It
definitely fires when the first touch happens, but it won't fire again
until the finger is lifted and put back down again. If the user is
interacting with a slider, or doing something silly like slowly
stroking a button in tiny little circles, the timeout will expire and
the view will fade out.

Here's some code for you to ponder. I apologize for the state of this.


public class SkeletonActivity extends Activity {

    static final private int BACK_ID = Menu.FIRST;
    static final private int CLEAR_ID = Menu.FIRST + 1;

    private EditText mEditor;

    private Handler mHandler;

    public SkeletonActivity() {
    }

    /** Called with the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mHandler = new Handler();

        // Inflate our UI from its XML layout description.
        setContentView(R.layout.skeleton_activity);

        // Find the text editor view inside the layout, because we
        // want to do various programmatic things with it.
        mEditor = (EditText) findViewById(R.id.editor);

        // Hook up button presses to the appropriate event handler.
        ((Button)
findViewById(R.id.back)).setOnClickListener(mBackListener);
        ((Button)
findViewById(R.id.clear)).setOnClickListener(mClearListener);

        mEditor.setText(getText(R.string.main_label));

        mButtonLayout = (LinearLayout)findViewById(R.id.buttonLayout);

        mHandler.postDelayed(mTimeoutHandler, 2000);

    }

    private Runnable mTimeoutHandler = new Runnable() {

        @Override
        public void run() {
            AlphaAnimation fade = new AlphaAnimation(1,0);
            fade.setDuration(2000);
            fade.setFillAfter(true);

            fade.setAnimationListener(new AnimationListener() {

                @Override
                public void onAnimationStart(Animation arg0) { }

                @Override
                public void onAnimationRepeat(Animation arg0) { }

                @Override
                public void onAnimationEnd(Animation arg0) {
                    mButtonLayout.setVisibility(View.INVISIBLE);
                }
            });
            mButtonLayout.startAnimation(fade);
            mButtonLayout.setEnabled(false);
        }

    };



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mHandler.removeCallbacks(mTimeoutHandler);
        mHandler.postDelayed(mTimeoutHandler, 2000);
        return super.onTouchEvent(event);
    }

    @Override
    public void onUserInteraction() {
        Log.e("UI","UserInteraction");
        if (!mButtonLayout.isEnabled()) {
            mButtonLayout.setEnabled(true);
            AlphaAnimation fade = new AlphaAnimation(0,1);
            fade.setDuration(100);
            fade.setFillAfter(true);
            mButtonLayout.setVisibility(View.VISIBLE);
            mButtonLayout.startAnimation(fade);
        }
        mHandler.removeCallbacks(mTimeoutHandler);
        mHandler.postDelayed(mTimeoutHandler, 2000);
    }

...

The rest of the code is identical to the standard Skeleton Application
complete with the Back and Clear buttons and all I did was set the
linear layout around the buttons and imageview to have an different
id:

android:id="@+id/buttonLayout"

So, my deep questions are: Is there a way to intercept the flow of
messages to an activity at a lower layer than the touch events? If
not, is there a way to intercept all of the touch events for a given
view heirarchy so I can know for sure if the user is currently
interacting with something?

And a final question, for bonus points, how do I stop the views in the
buttonLayout from being clicked when they are invisible? Maybe it's
just that they are getting the touch events because my code is making
the view visible in the onUserInteraction?

Thanks in advance for any help.

Cheers.

-Neal

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to