0

I am trying build simple library for button, inside library folder I created below class

 public class SimpleImageButton extends AppCompatImageView implements AppCompatImageView.OnClickListener{

    public Context mContext;
    Activity activity;

    public SimpleImageButton (Context context) {
        super(context);
        mContext = context;
        setCustomTypeface(context, null);

    }

    public SimpleImageButton (Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        setCustomTypeface(context, attrs);

    }

    public SimpleImageButton (Context context, AttributeSet attrs, int defStyleAttr) 
    {
        super(context, attrs, defStyleAttr);
        mContext = context;
        setCustomTypeface(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void setCustomTypeface(Context context, AttributeSet attrs)  {
        if(isInEditMode())
            return;
        TypedArray a = context.obtainStyledAttributes(attrs, 
        android.support.v7.appcompat.R.styleable.TextAppearance);
        setBackground(ContextCompat.getDrawable(mContext, 
    R.drawable.applogo_ads));
        a.recycle();

    }
   public void onClick(View view) {
        // here i have some functions to execute
    }
}

and my Mainclass in App folder

   SimpleImageButton imgBtn= (SimpleImageButton )findViewById(R.id.clickButton);

imgBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               imgBtn.onClick(view);
              // without this line how can i reach to onclick() of simpleImageButton class
            }
        });

so it is woking fine when i click button. but i want to make library button to work directly, without onClick function inside main activity, on clicking button should directly redirect to SimpleImageButton class onclcik method

I am very new to stack overflow , if any mistakes in the grammar / way of asking question please never mind. thank you.

Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64
Usha
  • 74
  • 1
  • 10
  • Use Call back listener in your library .. onClick of listener just send call back to parent activity or fragment – Muhammad Hassaan Sep 12 '18 at 09:56
  • i want to acheive something like i should not write anything in main activity for click action !!!!!! onclicking that button it shoud directly perform function that is present inside SimpleImageButton class onclick() method – Usha Sep 12 '18 at 10:01

2 Answers2

1

Use setOnClickListener(this) inside your view's constructor.

public SimpleImageButton (Context context) {
        super(context);
        mContext = context;
        setCustomTypeface(context, null);
        setOnClickListener(this);
    }
Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64
  • 1
    Furthermore, you still need to be able to set the click listener in the consumer. Here's how: https://stackoverflow.com/a/66733216/294884 – Fattie Mar 21 '21 at 14:01
1

I just override the this dispatchTouchEvent and dispatchKeyEvent and create own public function setOnClickListener(OnClickListener listener) to set on click listener

public class SimpleImageButton extends AppCompatImageView{
        //...
         private OnClickListener listener;
        
            @Override
            public boolean dispatchTouchEvent(MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (listener != null) listener.onClick(this);
                }
                return super.dispatchTouchEvent(event);
            }
        
            @Override
            public boolean dispatchKeyEvent(KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_UP && (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER || event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    if (listener != null) listener.onClick(this);
                }
                return super.dispatchKeyEvent(event);
            }
            public void setOnClickListener(OnClickListener listener) {
               this.listener = listener;
            }
    }

Now I hope it will be clickable just like other buttons.

Azhar Ali
  • 1,961
  • 3
  • 13
  • 24