Android: onClickListener for relativelayout

I have a relativelayout and are encoded onTouchListenerto highlight the background as follows:

    relative3.setOnTouchListener(new OnTouchListener() 
    {
        @Override
        public boolean onTouch(View arg0, MotionEvent event) 
        {
            if(event.getAction()==MotionEvent.ACTION_DOWN )
            {
                relative3.setBackgroundColor(getResources().getColor(R.color.tran_grey));
            }

            if((event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL))
            {
                relative3.setBackgroundColor(getResources().getColor(android.R.color.transparent));
            }
            return true;
        }           
    }); 

and perform an action on onClick

    relative3.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            custom_toast("Redirecting...");
        }
    }); 

Question:

Relativity can show highlighting ACTION_DOWNand return to transparency on ACTION_UP. However, it is clicked, the toast just does not appear as encoded.

How can this be changed? Thanks!

+4
source share
3 answers

Make sure the layout has the property android:clickable="true"

The correct way to change the color is to use the xml background image with the pressed and normal state. If you do this in code, use getActionMasked () instead of getAction ()

+2

false, OnTouchListener , .

relative3.setOnTouchListener(new OnTouchListener() 
{
    @Override
    public boolean onTouch(View arg0, MotionEvent event) 
    {
        if(event.getAction()==MotionEvent.ACTION_DOWN )
        {
            relative3.setBackgroundColor(getResources().getColor(R.color.tran_grey));
        }

        if((event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL))
        {
            relative3.setBackgroundColor(getResources().getColor(android.R.color.transparent));
        }
        // change to false.
        return false;
    }           
}); 

: http://developer.android.com/reference/android/view/View.OnTouchListener.html#onTouch (android.view.View, android.view.MotionEvent)

0

return false onTouch, OnTouchListener, . click onClick OnClickListener.

0

All Articles