Issue
I have extended the Button class and not implemented onClick
in extended button. And I am using the extended button every where and setting the onClick
listener in each activity.
How can I detect the button is clicked in the Extended class.
public class ButtonExtended extends Button {
public ButtonExtended(Context context) {
super(context);
}
public ButtonExtended(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ButtonExtended(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public ButtonExtended(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
//onClick(){ here i want to detect the click }
}
In this ButtonExtended
class, I want to detect, if the button is clicked just for the logging purpose. How can I achieve this?
Solution
You can override performClick()
behavior to get the clicked state.
public class ButtonExtended extends AppCompatButton{
public ButtonExtended(Context context) {
super(context);
}
public ButtonExtended(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ButtonExtended(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean performClick() {
Log.d("Button","performClick");
return super.performClick();
}
}
performClick()
will get called first before the the listener. Do not omit return super.performClick()
in method performClick()
this consume the event and your listener will not get called.
Answered By - ADM
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.