Hello. The title is not very clear what is required, so I explain: There is a ButtonBack class, inherited from Button. It implements the OnClickListener interface.

In the activity class, I declare ButtonBack:

ButtonBack mButton = (ButtonBack)findViewById....; mButton.setOnClickListener(new OnClickListener { @Override public void onClick(View view) { // тут нужно вызвать метод onClick из класса ButtonBack mButton.setBackground(....); } }); 

How do I call onClick from ButtonBack?

  • Do you really want to call onClick from ButtonBack or from the parent Button class? Just your comment and so inside the onClick method from an instance of ButtonBack class - alexoander
  • @alexoander ButtonBack is inherited from Button. ButtonBack is implemented onClickListener. In the activity of the button mButton I want to call onClick, which is implemented in ButtonBack - user214046
  • Hmm, everything turned out to be much easier. There was no point in implementing onClick in ButtonBack. I made a simple method and called it in the activity where I needed it. - user214046
  • 2
    Well, since you found the answer yourself - issue it as an answer. And if with the code it would be great - alexoander

1 answer 1

In fact, everything is very simple. It was necessary to create a method in the ButtonBack class and call it in the activity class. Those:

 public class ButtonBack extends Button { public ButtonBack(Context context) { super(context); } public ButtonBack(Context context, AttributeSet attributeSet) { super(context, attributeSet); } public void changeBackground() { this.setBackground(getResources().getDrawable(R.drawable.yellow_oval_button)); } } 

And in the activity call it:

  mButtonBack = (ButtonBack)findViewById(R.id.image_game_button_back); mButtonBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mButtonBack.changeBackground(); ImageGameActivity.this.finish(); } });