Mauin
Mauin

Reputation: 513

Touch events for Views on top of each other

I'm trying to create a layout where two views (in this case ImageButtons) are exactly on top of each other. Both should handle touch events to toggle the alpha of the buttons.

However only the button on top is receiving the touch event (but does not do anything since it's alpha is 0). The buttonBelow does not receive any Touch events when I press on the buttons.

The code i used to create the views and OnTouchListeners:

        // Create 2 Buttons on top of each other
        final ImageButton buttonBelow = new ImageButton(getActivity());//ImageButton
        buttonBelow.setImageResource(R.drawable.ic_action_locate);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        buttonBelow.setLayoutParams(layoutParams);

        final ImageButton buttonAbove = new ImageButton(getActivity());//ImageButton
        buttonAbove.setImageResource(R.drawable.ic_action_delete);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        buttonAbove.setLayoutParams(params);
        buttonAbove.setAlpha(0.0f);

        root.addView(buttonBelow);
        root.addView(buttonAbove);

        buttonBelow.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP && v.getAlpha() > 0) {
                    Log.e("Button", "Pressed below");
                    buttonBelow.setAlpha(0.0f);
                    buttonAbove.setAlpha(1.0f);
                    return true;
                }
                return false;
            }
        });

        buttonAbove.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP && v.getAlpha() > 0) {
                    Log.e("Button", "Pressed above");
                    buttonBelow.setAlpha(1.0f);
                    buttonAbove.setAlpha(0.0f);
                    return true;
                }
                return false;
            }
        });

How can the buttonBelow receive the touch event?

Upvotes: 0

Views: 1320

Answers (3)

Carlos Robles
Carlos Robles

Reputation: 10947

Make your class implement View.OnTouchListenerYou, and use the same listener for the two buttoms, and then make your decisions inside that:

class YourClass implements View.OnTouchListener {

    public void onCreate(Bundle savedInstanceState) {



        //...

           buttonBelow.setOnTouchListener(this);

           buttonAbove.setOnTouchListener(this);

        }

        //...

    public boolean onTouch(View v, MotionEvent event) {
          if (event.getAction() == MotionEvent.ACTION_UP) {
            if (buttonBelow.getAlpha() > 0) {
              buttonBelow.setAlpha(0.0f);
              buttonAbove.setAlpha(1.0f);

            }
            else {
              buttonBelow.setAlpha(1.0f);
              buttonAbove.setAlpha(0.0f);

           }


         return true;
         }
     return false;
   }

}

Upvotes: 1

Theuser1234
Theuser1234

Reputation: 15

you can't do that because the button above is like blocking the one below but you can make the on below the top one opposite of the top one using x and y

buttonAbove.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP && v.getAlpha() > 0) {
                Log.e("Button", "Pressed above");
                int y = event.getY();
            int x = event.getX();
                buttonBelow.setAlpha(x);
                buttonAbove.setAlpha(y);
                return true;
            }
            return false;
        }
    });

or by making both in y but using if statement inside the touch click like this:

                if(y > 10)
                buttonBelow.setAlpha(y);
                buttonAbove.setAlpha(0);}
                else{
                buttonBelow.setAlpha(x);
                buttonAbove.setAlpha(y)
                  }

but the best using the one up there

Upvotes: 0

Kapil Vats
Kapil Vats

Reputation: 5515

you can remove both the views(ImageButton) and add it again to relative layout in opposite order

Upvotes: 0

Related Questions