How to add OnTouchListener and OnClickListener at the same time in Linear Layout?

How to add an event OnTouchListenerand OnclickListenerat a time in LinearLayout?

Here is my code but not working

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 use only OnclickListener, it works, and when I use only the method onTouch, it works, but both at the same time do not work.

+4
source share
2 answers

Since the Touch Event is more general, it is called first and then triggered onClick, since your onTouch returns true, the event is consumed and onClicknever reached.

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

+7
source
 llTimeTable.setOnTouchListener(new View.OnTouchListener() {

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

        //Your code.......
            return false;
        }
    });

, false TouchListener becuase, true, .

+3

All Articles