Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnTouchListener on invisible view

so all I want to do is to touch on a certain part of the screen where an invisible view is hidden.

After that touch the view should fade In. The Problem: The Listener isn't triggered whe I touch the view.

Things I've tried: view.setImageAlpha(0), view.setAlpha(0), view.setVisibility(View.INVISIBLE) When I set alpha=1 the listener works fine.

Thanks in advance.

Edit: Thanks to @Viswanath L for the great solution which he mentioned in his post as second workaround.

I just wanted to give a little code example for that in case someone doesn't know how this works:

What I want: I want to touch the right corner of the screen and at this spot, a view(myView) should fade in.

   fadeInListener = new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                System.out.println("TRIGGERED");
//The screen size here is 1080x1920(vertical)
                if (event.getRawX() > 800 && event.getRawY() > 1640) {
                     final AlphaAnimation fadeIn = new AlphaAnimation(0, 1);
                     fadeIn.setDuration(500);
                     fadeIn.setFillAfter(true);
                     myView.startAnimation(fadeIn);
                }
            }
            return false;
        }
    };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    layout.setOnTouchListener(fadeInListener);
}

The layout is:

this.layout = (RelativeLayout) findViewById(R.id.layout);

And myView should be placed in the right corner and what's very important:

myView.setVisibility(View.Gone);

Hope this helps.

like image 324
SoundOfTheTuner Avatar asked Jan 10 '23 00:01

SoundOfTheTuner


2 Answers

INVISBLE means you are trying to add a listener to a view which is not there. You can add the listener to a visible view only.

WORKAROUND

1) Try to make a dummy view which is visible but having the same color as background.

2) Try to set the listener for parent and check the position (whether the position does belongs to INVISIBLE view).

like image 157
Viswanath Lekshmanan Avatar answered Jan 22 '23 21:01

Viswanath Lekshmanan


onTouchListener does not trigger for an invisible View. You can make 2 exactly the same views one under another the one below having the map. It will be visible and so it can be touched but it will be obscured by the view above like they do here: http://blahti.wordpress.com/2012/06/26/images-with-clickable-areas/

like image 44
Alexander Kulyakhtin Avatar answered Jan 22 '23 21:01

Alexander Kulyakhtin