0

Need to handle click event on drawableTop of TextView, written following code,

    tvSocialMedia.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_UP) {
                if(event.getRawX() <= tvSocialMedia.getTotalPaddingTop()) {
                    // your action for drawable click event
                    tvEmail.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.email,0, 0);
                    tvPhone.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.phone,0, 0);
                    tvAddress.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.address,0, 0);
                    tvSocialMedia.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.social_media_select,0, 0);
                    usage.setText(getResources().getString(R.string.social_media));
                    return true;
                }
            }
            return true;
        }
    });

But code inside if(event.getAction() == MotionEvent.ACTION_UP) is not getting executed. Correct me if I have written wrong condition in if statement.

musica
  • 1,373
  • 3
  • 15
  • 34
  • question is bit unclear to me :| whats the view you are using to call `onClickSocial` – Charuක Jan 24 '17 at 06:32
  • onClickSocial is a method to handle TextView click(R.id.tvSocialMedia) – musica Jan 24 '17 at 06:35
  • 1
    What is the meaning to write `setOnTouchListener` into `onClickSocial`. You are setting `onTouch` event when `textview` is clicked. – Paresh P. Jan 24 '17 at 06:36
  • So `event.getRawX() <= tvSocialMedia.getTotalPaddingTop()` this condition does not returning true? – Paresh P. Jan 24 '17 at 07:17
  • if(event.getAction() == MotionEvent.ACTION_UP) is not returning true, also tried MotionEvent.ACTION_DOWN – musica Jan 24 '17 at 07:22
  • I don't know what going on but why don't you use GestureDetector. like [https://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html] – Paresh P. Jan 24 '17 at 07:30

1 Answers1

0

changes to handle event on drawableTop:

1) from if(event.getAction() == MotionEvent.ACTION_UP) to if(event.getAction() == MotionEvent.ACTION_DOWN)

2)from if(event.getRawX() <= tvSocialMedia.getTotalPaddingTop()) to if(event.getRawY() >= tvPhone.getTop() - tvPhone.getTotalPaddingTop())

3) return false

tvPhone.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                if(event.getRawY() >= tvPhone.getTop() - tvPhone.getTotalPaddingTop()) {
                    // your action for drawable click event
                    tvEmail.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.email,0, 0);
                    tvPhone.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.phone_select,0, 0);
                    tvAddress.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.address,0, 0);
                    tvSocialMedia.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.social_media,0, 0);
                    usage.setText(getResources().getString(R.string.phone_no));
                    return true;
                }
            }
            return false;
        }
    });
musica
  • 1,373
  • 3
  • 15
  • 34