Issue
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.
Solution
changes to handle event on drawableTop:
1) from
if(event.getAction() == MotionEvent.ACTION_UP)
toif(event.getAction() == MotionEvent.ACTION_DOWN)
2)from
if(event.getRawX() <= tvSocialMedia.getTotalPaddingTop())
toif(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;
}
});
Answered By - musica
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.