17

I would like to create a CustomButton which has a predefined onClick. In fact, my object would do the same job than

CustomButton mButton = getViewById(..);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
  show_something()
}

Is there a way to embed the Listener into the CustomButton object that inherits from Button ? What I would like is to create a CustomButton in my layout XML file, and not having to mention this button in my activity, which would give:

main.xml:

<LinearLayout xmlns:"...">
     <com.mypackage.view.CustomButton
         (attributes)/>
</LinearLayout>

CustomButton.java:

class CustomButton extends Button implements... {

@Override
OnClick (or something like that, that's the core of my question, what to put here)
}

myActivity.java

public class myActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    }
}

Thanks a lot.

Parth Doshi
  • 4,200
  • 15
  • 79
  • 129
Flavian Hautbois
  • 2,940
  • 6
  • 28
  • 45

4 Answers4

29

You were really close:

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class CustomButton extends Button implements OnClickListener{

    public CustomButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public CustomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomButton(Context context) {
        super(context);
        init();
    }

    private void init(){
        setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // Do something
    }

}
Blundell
  • 75,855
  • 30
  • 208
  • 233
4

In your button class just implement:

@Override
public void onClick(View v) {
    showSomething();
}

Or if you want more granular control:

@Override
public boolean onTouchEvent(MotionEvent event) {
    showSomething();
}

You can either implement your click logic in the method by checking the event.getAction(), or send it to a GestureDetector to figure out when a click has been performed.

TofferJ
  • 4,678
  • 1
  • 37
  • 49
2

Use that code:

class CustomButton extends Button implements View.OnClickListener{
     CustomButton(Context c, AttributeSet attrs) {
         ...
         setOnClickListener(this);
         ...
     }

    @override
    public void onClick(View v){
        //add your code here
    }
}
woodshy
  • 4,085
  • 3
  • 22
  • 21
0

When you are using Kotlin:

class TemporyActionButton constructor(
  context: Context,
  attrs: AttributeSet
) : RelativeLayout(context, attrs), View.OnClickListener {

  private val button: MaterialButton
  private var event: CustomOnClickListener? = null

  init {
    inflate(context, attrs)

    button = findViewById(R.id.button_action)
    button.setOnClickListener(this)

    context.theme.obtainStyledAttributes(
      attrs, R.styleable.TemporyActionButton, 0, 0
    ).use {
      // styling
    }
  }

  override fun onClick(v: View) {
    event?.customOnClick(v)
  }

  fun setCustomClickListener(event: CustomOnClickListener) {
    this.event = event
  }

  private fun inflate(context: Context, attrs: AttributeSet) {
    LayoutInflater.from(context).inflate(R.layout.layout_button_action, this, true)
    layoutParams = LinearLayout.LayoutParams(context, attrs)
  }
}

fun interface CustomOnClickListener {
  fun customOnClick(view: View)
}

Andre Thiele
  • 3,202
  • 3
  • 20
  • 43