Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Add OnTouchListener and OnClickListener at a time in Linear Layout?

How can I add a event OnTouchListener and OnclickListener at a time in LinearLayout?

Here is my code but doesn't work

final LinearLayout llTimeTable=(LinearLayout) findViewById(R.id.llSehriIftar);
    llTimeTable.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MainActivity.this, Ramadandate.class);
            startActivity(intent);

        }
    });
    llTimeTable.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:

            llTimeTable.setBackgroundColor(Color.rgb(51, 51, 255));
                break;

            case MotionEvent.ACTION_UP:

                // set color back to default
                llTimeTable.setBackgroundColor(Color.rgb(76, 106, 225));

                break;
            }
            return true;
        }
    });

But when I only use OnclickListener it works, and when I use only onTouch method it works, but both at the same time doesn't work.

like image 638
Mostasim Billah Avatar asked Dec 11 '22 22:12

Mostasim Billah


1 Answers

Since Touch Event is more general, it is called first, then the onClick gets fired, however, since your onTouch returns true, the event is consumed and onClick is never reached.

Simply change onTouch to return false and your onClick will be called.

like image 162
Antwan Kakki Avatar answered May 02 '23 06:05

Antwan Kakki